当前位置:网站首页>redis秒杀案例,跟着b站尚硅谷老师学习
redis秒杀案例,跟着b站尚硅谷老师学习
2022-07-27 01:37:00 【进击的北极熊】
一、使用redis的事务解决秒杀问题,但是会产生库存遗留问题
二、使用lua脚本解决秒杀问题
0、使用lua脚本的优势

1、具体使用的service层代码
package com.bear.service;
import com.bear.config.JedisPoolUtil;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.io.IOException;
/** * <简述> * <详细描述> * * @author LiuShanshan * @version $Id$ */
@Service
public class SewckillServiceTwo {
String secKillScript ="local userid=KEYS[1];\r\n" +
"local prodid=KEYS[2];\r\n" +
"local qtkey='sk:'..prodid..\":qt\";\r\n" +
"local usersKey='sk:'..prodid..\":usr\";\r\n" +
"local userExists=redis.call(\"sismember\",usersKey,userid);\r\n" +
"if tonumber(userExists)==1 then \r\n" +
" return 2;\r\n" +
"end\r\n" +
"local num= redis.call(\"get\" ,qtkey);\r\n" +
"if tonumber(num)<=0 then \r\n" +
" return 0;\r\n" +
"else \r\n" +
" redis.call(\"decr\",qtkey);\r\n" +
" redis.call(\"sadd\",usersKey,userid);\r\n" +
"end\r\n" +
"return 1" ;
public boolean doSecKill(String prodid,String uid) throws IOException {
JedisPool jedispool = JedisPoolUtil.getJedisPoolInstance();
Jedis jedis=jedispool.getResource();
//String sha1= .secKillScript;
String sha1= jedis.scriptLoad(secKillScript);
Object result= jedis.evalsha(sha1, 2, uid,prodid);
String reString=String.valueOf(result);
if ("0".equals( reString ) ) {
System.err.println("已抢空!!");
}else if("1".equals( reString ) ) {
System.out.println("抢购成功!!!!");
}else if("2".equals( reString ) ) {
System.err.println("该用户已抢过!!");
}else{
System.err.println("抢购异常!!");
}
jedis.close();
return true;
}
}
2、controller层
/** * 秒杀测试使用lua脚本 * @return */
@GetMapping("/seckill/test")
public Boolean seckillTest() throws IOException {
// 固定商品id ;// 自动生成的用户id
return sewckillServiceTwo.doSecKill("1001", Long.toString(System.currentTimeMillis()));
}
边栏推荐
- Leetcode 207. curriculum (July 26, 2022)
- Safe-arc/warner power supply maintenance xenon lamp power supply maintenance analysis
- 队列达到最大长度代码实战
- 图解用户登录验证流程,写得太好了!
- OpenTelemetry 在服务网格架构下的最佳实践
- The diagram of user login verification process is well written!
- SAFE-ARC/WARNER电源维修XENON氙灯电源维修分析
- 【1206. 设计跳表】
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- shell awk
猜你喜欢

Common events of window objects

185. All employees with the top three highest wages in the Department (mandatory)

Boom 3D全新2022版音频增强应用程序App

window对象的常见事件

阶乘末尾0的数量

be based on. NETCORE development blog project starblog - (16) some new functions (monitoring / statistics / configuration / initialization)

Graphic SQL, this is too vivid!

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

1.28亿美元!芬兰量子计算公司IQM获世界基金支持

30分钟彻底弄懂 synchronized 锁升级过程
随机推荐
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
[从零开始学习FPGA编程-54]:高阶篇 - 基于IP核的FPGA开发-PLL锁相环IP核的原理与配置(Altera)
flask_restful中reqparse解析器继承
Activiti5.22.0扩展支持达国产数据库,以GBase据库为例
Boom 3D new 2022 audio enhancement app
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
记录一次,php程序访问系统文件访问错误的问题
食物链(DAY 79)
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
Hcip 13th day notes
字节一面:TCP 和 UDP 可以使用同一个端口吗?
185. All employees with the top three highest wages in the Department (mandatory)
Annotation summary of differences between @autowired and @resource
A test class understands beanutils.copyproperties
自己梳理的LocalDateTime的工具类
Deep learning vocabulary embedded, beam search
深度学习——词汇embedded、Beam Search
Safe-arc/warner power supply maintenance xenon lamp power supply maintenance analysis
Social wechat applet of fanzhihu forum community
Bulk copy baby upload prompt garbled, how to solve?