当前位置:网站首页>【Redis实现秒杀业务④】一人一单,不可重复购买
【Redis实现秒杀业务④】一人一单,不可重复购买
2022-06-24 20:01:00 【步尔斯特】
对于秒杀水果,一个用户只能秒杀一种水果的一单。
业务流程
并发问题
正常
并发问题
加锁保证线程安全
判断订单是否存在,为了保证线程安全问题,我们可以加锁,synchronized。
如果加在方法上,是对this加锁,也就是说,所有的用户进来都要加锁,这样不符合我们一人一单的预期。
我们以用户ID加锁,减少锁的范围,提升性能。
userId.toString().intern()
保证唯一【返回字符串对象的规范表示】
@Transactional
public Result createVoucherOrder(Long voucherId) {
// 一人一单【前文配置的过滤器中,如果登录的用户,会在ThreadLocal中添加用户信息】
Long userId = UserHolder.getUser().getId();
// 对执行代码块加锁,intern()方法避免资源浪费
synchronized (userId.toString().intern()) {
// 查询订单
int count = query().eq("user_id", userId).eq("voucher_id", voucherId).count();
// 判断是否存在
if (count > 0) {
// 用户已经购买过了
return Result.fail("用户已经购买过一次!");
}
// 扣减库存
boolean success = seckillVoucherService.update()
.setSql("stock = stock - 1") // set stock = stock - 1
.eq("voucher_id", voucherId).gt("stock", 0) // where id = ? and stock > 0
.update();
if (!success) {
// 扣减失败
return Result.fail("库存不足!");
}
集群问题
如果在集群的环境下,会有多个JVM存在,会存在多个锁监视器,这样加锁就会完全失效,还是会产生线程安全问题,所以,下一篇文章我们使用redis实现分布式锁来保证线程安全。
边栏推荐
- Encryption and encoding resolution
- A small program written this week
- C WinForm maximizes occlusion of the taskbar and full screen display
- December 6, 2019 what happens after the browser enters a URL
- Paint rounded rectangle
- Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
- Adding, deleting, modifying and checking in low build code
- Realization of MNIST handwritten numeral recognition
- Go crawler framework -colly actual combat (IV) -- Zhihu answer crawl (I)
- Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
猜你喜欢
UE4 WebBrowser chart cannot display problems
Human body transformation vs digital Avatar
融合模型权限管理设计方案
wx小程序跳转页面
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
Ott marketing is booming. How should businesses invest?
Color gradient gradient color collection
[distributed system design profile (2)] kV raft
Tiktok wallpaper applet v1.0.2 function, new arrival function
Working principle analysis of kubernetes architecture core components
随机推荐
VIM use command
vim使用命令
Usage of assert
Several ways for wechat applet to jump to the page are worth collecting
ServerSocket and socket connection
Uniapp encapsulated incentive advertisement, screen insert advertisement and banner advertisement
JMeter socket connection sends data
How can I persuade leaders to use DDD to construct the liver project?
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
ros(25):rqt_image_view报错Unable to load plugin for transport ‘compressed‘, error string
More pictures | explain the Nacos parameters in detail!
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
Android SQLite database
Analysis report on operation trend and investment strategy of global and Chinese tetrahydrofurfuryl propionate industry from 2022 to 2028
Signal integrity (SI) power integrity (PI) learning notes (I) introduction to signal integrity analysis
Go crawler framework -colly actual combat (II) -- Douban top250 crawling
Single blind box removal, social blind box and friend blind box program source code
Go crawler framework -colly actual combat (I)
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
Only positive integers can be entered in the text box