4次优化,我把Redis性能“压榨”到极致!
Java技术迷
共 3832字,需浏览 8分钟
·
2021-07-17 22:23
我们有个这样的需求:每天每一个抢购商品只能买一次,并且全场抢购商品总购买次数不允许超过5次。那么,整个商品限购的流程大概如下图所示:
[{
"orderId": "2020020622000001",
"orderTime": "1581001673012",
"productId": "599055114591",
"userId": "860000000000001",
"merchantCode": "A045"
}, {
"orderId": "2020020622000001",
"orderTime": "1581001673012",
"productId": "599055114592",
"userId": "860000000000001",
"merchantCode": "A045"
}]
命令1:hset mall:sale:freq:ctrl:8600000000000015990551145911(hash结构,field表示购买的商品ID,value表示购买次数)
命令2:hset mall:sale:freq:ctrl:8600000000000015990551145922
命令3:expire mall:sale:freq:ctrl:8600000000000013127(设置过期时间)
命令4:set mall:total:freq:ctrl:8600000000000013
命令5:expire mall:total:freq:ctrl:8600000000000013127(设置过期时间)
如下图所示:
第1次优化
hmset mall:sale:freq:ctrl:860000000000001 599055114591 1 599055114592 2
expire mall:sale:freq:ctrl:860000000000001 3127
set mall:total:freq:ctrl:860000000000001 3
expire mall:total:freq:ctrl:860000000000001 3127
第2次优化
hmset mall:sale:freq:ctrl:860000000000001 599055114591 1 599055114592 2
expire mall:sale:freq:ctrl:860000000000001 3127
setex mall:total:freq:ctrl:860000000000001 3127 3
第3次优化
-- 这两条命令的key都是一样的,肯定在同一个slot上
pipeline(
hmset mall:sale:freq:ctrl:860000000000001 599055114591 1 599055114592 2
expire mall:sale:freq:ctrl:860000000000001 3127
)
-- mall:total:freq:ctrl:860000000000001和mall:sale:freq:ctrl:860000000000001两条命令不在同一个slot上,所以需要单独执行下面这条命令
setex mall:total:freq:ctrl:860000000000001 3127 3
第4次优化
pipeline(
hmset mall:sale:freq:ctrl:${860000000000001} 599055114591 1 599055114592 2
expire mall:sale:freq:ctrl:${860000000000001} 3127
setex mall:total:freq:ctrl:${860000000000001} 3127 3
)
注意事项
评论