当前位置:网站首页>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()));
}
边栏推荐
猜你喜欢

Details of impala implementation plan

win10/win11无损扩大C盘空间,跨盘合并C、E盘

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

FactoryBean的getObject调用时机

After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized

On the prototype of constructor

“date: write error: No space left on device”解决

【树链剖分】模板题

Code practice when the queue reaches the maximum length

图解用户登录验证流程,写得太好了!
随机推荐
docker 创建mysql 8.x容器,支持mac ,arm架构芯片
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
Worthington果胶酶的特性及测定方案
Yilingsi T35 FPGA drives LVDS display screen
After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized
177. 第N高的薪水(简单)
unity游戏,隐私协议最简单解决方案!仅3行代码就搞定!(转载)
Detailed explanation of const usage in C language
二叉树(DAY 82)
Explain详解
30分钟彻底弄懂 synchronized 锁升级过程
Hcip day 14 notes
JMeter distributed pressure measurement
最大连续子序列(DAY 77)
Submodule cache cache failure
Annotation summary of differences between @autowired and @resource
C语言const用法详解
185. 部门工资前三高的所有员工(必会)
水仙花数(DAY 78)
[learning notes, dog learning C] string + memory function