redis的key或者计数器有没有递减的功能?
ringa_lee
ringa_lee 2017-04-21 11:17:22
0
3
732

比如现在想实现类似一个秒杀的功能,10个用户抢2个商品,redis或者memcache或者其它的nosql产品,有没有类似的功能,就是我有个计数器(库存,比如等于2)然后,开始抢商品的时候,前2个用户,已经将库存减到0了,当第3个人来抢的时候,计数器不会变为-1,而是还是为0,并且这个操作会返回一个操作失败,或者类似于mysql的返回影响行数为0的这种反馈信息,让我知道的?

ringa_lee
ringa_lee

ringa_lee

reply all(3)
迷茫

reids has DECR (atomic decrement), and memcache also has the decrement function. The difference between them is that when redis’s atomic decrement reaches 0, it will become a negative number, while memcache’s will always be 0

小葫芦

No. It will become -1, I'm afraid you have to make your own judgment.

伊谢尔伦

Optimistic lock solution

Do not check the lock, pass it directly, but check the version number after passing it, and do not operate it correctly, otherwise roll back (requires transaction support)

One problem is that maybe users in orders 2 and 3 didn’t get it, but users in order 4 and 5 got it (in this case, you’ll know after you write a demo and test it)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template