
原理 单一实例下:使用setnx命令(在key不存在时,创建并设置value 返回1,key存在时,会返回0)来获取锁。 应用 以扣库存为例,使用锁能够有效保证不会被超卖; 基础版方案:数据库锁 SELECT stock from goods where ID =1 for update; update goods set stock=stock-1 where id=1 and stock=10 // 正常情况下看你业务需求,如果能够在落库前确认库存的话,上面这句话后面的stock=10能...
Geek,Agile
Markdown supported · ⌘↵ to post
No responses yet.