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

Code practice when the queue reaches the maximum length

Bulk copy baby upload prompt garbled, how to solve?

Post responsibilities of safety officer and environmental protection officer

JMeter distributed pressure measurement

深度学习——词汇embedded、Beam Search

Activiti5.22.0 extension supports domestic databases, taking gbase database as an example

Plato Farm全新玩法,套利ePLATO稳获超高收益

HCIP第十四天笔记

Functions that should be selected for URL encoding and decoding

Comprehensive care analysis lyriq Ruige battery safety design
随机推荐
185. All employees with the top three highest wages in the Department (mandatory)
spark:地区广告点击量排行统计(小案例)
196. Delete duplicate email addresses
阶乘末尾0的数量
Win10/win11 lossless expansion of C disk space, cross disk consolidation of C and e disks
水仙花数(DAY 78)
Worthington果胶酶的特性及测定方案
力扣(LeetCode)207. 课程表(2022.07.26)
spark学习笔记(六)——sparkcore核心编程-RDD行动算子
Role of thread.sleep (0)
Pytoch loss function summary
在线问题反馈模块实战(十五):实现在线更新反馈状态功能
深入理解Mysql索引底层数据结构与算法
Learn the recycling mechanism of recyclerview again
win10/win11无损扩大C盘空间,跨盘合并C、E盘
OpenTelemetry 在服务网格架构下的最佳实践
How to uniquely identify a user SQL in Youxuan database cluster
正方形数组的数目(DAY 81)
JMeter distributed pressure measurement
周全的照护 解析LYRIQ锐歌电池安全设计