首页 > 其他分享 >秒杀案例

秒杀案例

时间:2023-06-04 10:33:41浏览次数:60  
标签:String springframework 案例 秒杀 import org redisTemplate

package com.atguigu.seckill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/seckill")
public class SecKController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public String test(){
        redisTemplate.opsForValue().set("name", "lucky");
        String name = (String)redisTemplate.opsForValue().get("name");
        System.out.println(name);
        return name;
    }

    //秒杀过程
    @GetMapping("/getThing")
    public boolean getThing(@RequestParam String uid, @RequestParam String prodid){

        //1 uid和prodid非空判断
        if(uid == null || prodid == null){
            return  false;
        }

        //2、拼接key
        //2.1 秒杀库存key
        String kcKey = "sk:" + prodid +":qt";
        //2.2 秒杀成功用户key
        String userKey = "sk:" + prodid + ":user";
        //3 获取库存,如果库存null,秒杀没有开始
        Object kc = redisTemplate.opsForValue().get(kcKey);
        if(kc == null){
            System.out.println("秒杀还没开始,请等待");
            return false;
        }
        String kcnum = String.valueOf(kc);

        //4 判断用户是否重复秒杀操作
        if(redisTemplate.opsForSet().isMember(userKey, uid)){
            System.out.println("已经秒杀成功,不能重复秒杀");
            return  false;
        }
        //5 判断如果商品数量<=0,秒杀结束
        if(Integer.parseInt(kcnum)<=0){
            System.out.println("秒杀已结束");
            return  false;
        }
        //6 秒杀过程
        //6.1 秒杀到,库存减一
        redisTemplate.opsForValue().decrement(kcKey);
        //6.2 把秒杀成功用户添加到清单里面
        redisTemplate.opsForSet().add(userKey, uid);
        System.out.println("秒杀成功了");
        return true;
    }

}

 

标签:String,springframework,案例,秒杀,import,org,redisTemplate
From: https://www.cnblogs.com/fxzm/p/17455285.html

相关文章

  • xgplayer的使用案例
    Tomyself:引用的相关类库见个人文件上传列表=>xgplayer.zip<template><divclass="video-player"><divclass="title"><a-icontype="close"class="close"@click="closeVideo"/>&......
  • AntDesign中a-menu的使用案例
    <template><divclass="nav-bar":class="{collapsed:collapsed}"><divclass="collapse-btn"@click="toggleCollapsed"><a-icon:type="collapsed?'menu-unfold':'......
  • 6.6 数组排序案例分析
    冒泡排序classArrayUtil{publicstaticvoidsort(intdata[]){for(intx=0;x<data.length;x++){for(inty=0;y<data.length-x-1;y++){//注意这里的-x-1含义;if(data[y]<data[y+1]){......
  • 方法递归的案例:文件搜索
        ......
  • 多线程安全的案例展示与解决方案
    一、概念1.什么是线程安全当多个线程访问一个对象时,如果不用考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方进行任何其他的协调操作,调用这个对象的行为都可以获得正确的结果,那这个对象是线程安全的。通俗来说就是:不管业务中遇到怎么的多个线......
  • Vue——属性指令、style和class、条件渲染、列表渲染、事件处理、数据双向绑定、过滤
    vm对象<body><divid="app"><h1>{{name}}</h1><button@click="handleClick">点我</button></div></body><script>//1写在data或method中的属性或方法,从vm中直接可以点出来//2method的函数中,如......
  • NLP自然语言处理—主题模型LDA案例:挖掘人民网留言板文本数据|附代码数据
    全文链接:http://tecdat.cn/?p=2155最近我们被客户要求撰写关于NLP自然语言处理的研究报告,包括一些图形和统计输出。随着网民规模的不断扩大,互联网不仅是传统媒体和生活方式的补充,也是民意凸显的地带。领导干部参与网络问政的制度化正在成为一种发展趋势,这种趋势与互联网发展的时......
  • 5.24 面向对象案例分析六
    classBook{//类的名称要以class开头,否则报错,并且提示不到这行代码!!!privateintbid;privateStringtitle;privatedoubleprice;privatestaticintcount=0;publicBook(Stringtitle,doubleprice){count++;this.bid=count;......
  • 5.23 面向对象案例分析五
    用static,引入计数器案例classUser{privateStringuid;privateStringpassword;privatestaticintcount=0;publicUser(){this("NOID","mldn");}publicUser(Stringuid){this(uid,"mldnjava&q......
  • 5.21 面向对象案例分析三
    狗的一个类,包括名字,颜色,年龄;典型的java类的一段代码classDog{privateStringname;privateStringcolor;privateintage;publicDog(){}publicDog(Stringname,Stringcolor,intage){this.name=name;this.color=color;......