当前位置:网站首页>Redis seckill demo
Redis seckill demo
2022-07-01 15:35:00 【y_ w_ x_ k】
Scheme 1 : Method plus synchronized that will do , Thread safety scheme , But the efficiency of execution is low , Not suitable for large concurrent occasions
Option two : Use optimistic lock
controller:
@RestController
public class SecondKill {
@Autowired
SecondKillSev secondKillSev;
@RequestMapping(value = "/buy",method = RequestMethod.POST)
public Mes buy(@RequestParam("proid") String proid){
String usedId=new Random().nextInt(1000)+"";
Mes mes = secondKillSev.doSecondKill(usedId, proid);
return mes;
}
}service:
@Service
public class SecondKillSev {
Mes message;
public SecondKillSev(){
}
@Autowired
StringRedisTemplate stringRedisTemplate;
/**
* Execute seckill
* Scheme 1 :synchronized, Thread safety , But the efficiency of execution is low , Not suitable for large concurrent occasions
* Option two : Monitor transactions with optimistic locks
*/
public Mes doSecondKill(String uid, String proid){
//1. Judge uid and proid Non empty
if(uid==null || proid==null){
return Mes.getMes("400","uid and proid Can't be empty ");
}
//2. Connect redis
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
//3. Splicing key, stock key, Successful user key
String kcKey="kcKey:"+proid;
String userKey="userKey:"+proid;
// Optimistic lock solves oversold problem
//stringRedisTemplate The default is not to open things
stringRedisTemplate.setEnableTransactionSupport(true);
List<Object> txResults = stringRedisTemplate.execute(new SessionCallback<List<Object>>() {
public List<Object> execute(RedisOperations operations) throws DataAccessException {
// Monitor inventory
operations.watch(kcKey);
//4. Get inventory , If the inventory is null, The second kill has not yet started
String kc=ops.get(kcKey);
if(kc==null){
message= Mes.getMes("400"," The stock is null, The second kill hasn't started yet ");
return null;
}
//5. Judge whether the user repeats the second kill
if(operations.opsForSet().isMember(userKey, uid)){
message=Mes.getMes("400"," You can't repeat the second kill ");
return null;
}
//6. Judge the quantity of goods , If the quantity is less than 1, Then the second kill is over
if(Integer.parseInt(kc)<=0){
message=Mes.getMes("400"," The second kill is over ");
return null;
}
//7. Second kill process , stock -1, Successful user added to manifest
// With a transaction
operations.multi();
operations.opsForValue().decrement(kcKey);
operations.opsForSet().add(userKey,uid);
List exec = operations.exec();
if(exec==null || exec.size()==0){
message=Mes.getMes("400"," Seckill failure ");
return null;
}else{
return exec;
}
}
});
if(txResults==null ||txResults.size()==0){
return message;
}
return Mes.getMes("200"," Seckill success ");
}
}
But this scheme also has a disadvantage , That is, if the quantity of goods is 500, But the number of threads is 1000, That is, when the number of visits is not very large , There will be the problem that the inventory is not sold out , That is, inventory leftover problems , So it can be used lua Script to solve ;
Option three :lua Script
local userid=KEYS[1];
local prodid=KEYS[2];
local qtkey="sk:"..prodid..":qt";
local userskey="sk:"..prodid..":user";
local userExists=redis.call("sismember",userskey,userid);
if tonumber(userExists)==1 then
return 2;
end
local num=redis.call("get",qtkey);
if tonumber(num)<=0 then
return 0;
else
redis.call("decr",qtkey);
redis.call("sadd",userskey,userid);
end
return 1
Code implementation :
@Service
public class SeckillByScript {
static String secKillScript1 = "local userid=KEYS[1];\n" +
"local prodid=KEYS[2];\n" +
"local qtkey=\"sk:\"..prodid..\":qt\";\n" +
"local userskey=\"sk:\"..prodid..\":user\";\n" +
"local userExists=redis.call(\"sismember\",userskey,userid);" +
"if tonumber(userExists)==1 then\n" +
" return 2;\n" +
"end\n" +
"local num=redis.call(\"get\",qtkey);\n" +
"if tonumber(num)<=0 then\n" +
" return 0;\n" +
"else\n" +
" redis.call(\"decr\",qtkey);\n" +
" redis.call(\"sadd\",userskey,userid);\n" +
" end\n" +
" return 1";
public boolean doSecKill(String userid, String prodid) {
JedisPool jedisPool = JedisPoolUtil.getJedisPool();
Jedis jedis = jedisPool.getResource();
String sha1 = jedis.scriptLoad(secKillScript1);
Object result = jedis.evalsha(sha1, 2, userid, prodid);
String reString = String.valueOf(result);
if ("0".equals(reString)) {
System.out.println(" Empty !");
return false;
} else if ("1".equals(reString)) {
System.out.println(" Panic buying ");
return true;
} else if ("2".equals(reString)) {
System.out.println(" The user has robbed !");
return false;
} else {
System.out.println(" Panic buying exception ");
return false;
}
}
}
边栏推荐
- 精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
- 【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
- Beilianzhuguan joined the dragon lizard community to jointly promote carbon neutralization
- 【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
- 异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
- 入侵检测模型(An Intrusion-Detection Model)
- C#/VB.NET 合并PDF文档
猜你喜欢

STM32ADC模拟/数字转换详解

张驰课堂:六西格玛数据的几种类型与区别

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Zhang Chi Consulting: lead lithium battery into six sigma consulting to reduce battery capacity attenuation

Stm32f4-tft-spi timing logic analyzer commissioning record

Survey of intrusion detection systems:techniques, datasets and challenges

S32K1xx 微控制器的硬件设计指南

【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》

Introduction to MySQL audit plug-in

【目标跟踪】|STARK
随机推荐
如何实现时钟信号分频?
ABAP-屏幕切换时,刷新上一个屏幕
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
[target tracking] | template update time context information (updatenet) "learning the model update for Siamese trackers"
Wechat official account subscription message Wx open subscribe implementation and pit closure guide
MySQL高级篇4
Photoshop插件-HDR(二)-脚本开发-PS插件
【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
C#/VB.NET 合并PDF文档
[leetcode] 16. The sum of the nearest three numbers
JS中箭头函数和普通函数的区别
Day 3 of rhcsa study
Qt+pcl Chapter 9 point cloud reconstruction Series 2
做空蔚来的灰熊,以“碰瓷”中概股为生?
Flink 系例 之 TableAPI & SQL 与 Kafka 消息获取
智能运维实战:银行业务流程及单笔交易追踪
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
《性能之巅第2版》阅读笔记(五)--file-system监测
Go zero actual combat demo (I)
异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)