search

Home  >  Q&A  >  body text

redis的key或者计数器有没有递减的功能?

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

ringa_leeringa_lee2798 days ago751

reply all(3)I'll reply

  • 迷茫

    迷茫2017-04-21 11:19:22

    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

    reply
    0
  • 高洛峰

    高洛峰2017-04-21 11:19:22

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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-21 11:19:22

    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)

    reply
    0
  • Cancelreply