当前位置:网站首页>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()));
}
边栏推荐
- 延时队列的几种实现姿势?日常必备技能!
- Detailed explanation of const usage in C language
- Common events of window objects
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- spark:计算不同分区中相同key的平均值(入门级-简单实现)
- 食物链(DAY 79)
- 客户案例 | 关注老年用户体验,银行APP适老化改造要避虚就实
- 185. All employees with the top three highest wages in the Department (mandatory)
- spark学习笔记(四)——sparkcore核心编程-RDD
- 1.28亿美元!芬兰量子计算公司IQM获世界基金支持
猜你喜欢

记录一次,php程序访问系统文件访问错误的问题

Practice of online problem feedback module (XV): realize the function of online updating feedback status

Annotation summary of differences between @autowired and @resource

带你了解什么是 Web3.0

太强了,一个注解搞定接口返回数据脱敏

It's too strong. An annotation handles the data desensitization returned by the interface

Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议

字节一面:TCP 和 UDP 可以使用同一个端口吗?

vector 转 svg 方法

Fastboot刷机
随机推荐
DNS记录类型及相关名词解释
【常用搜索问题】111
window对象的常见事件
二叉树(北京邮电大学机试题)(DAY 85)
数据库使用安全策略
索引最佳实践
shell awk
Yilingsi T35 FPGA drives LVDS display screen
How many implementation postures of delay queue? Daily essential skills!
unity游戏,隐私协议最简单解决方案!仅3行代码就搞定!(转载)
196. Delete duplicate email addresses
Fastboot刷机
Activiti5.22.0扩展支持达国产数据库,以GBase据库为例
Worthington果胶酶的特性及测定方案
阶乘末尾0的数量
“date: write error: No space left on device”解决
177. 第N高的薪水(简单)
Plato Farm全新玩法,套利ePLATO稳获超高收益
深入理解Mysql索引底层数据结构与算法
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi