Redis适用于高并发的递增、递减功能
生活随笔
收集整理的這篇文章主要介紹了
Redis适用于高并发的递增、递减功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
遞增指令:incr(默認從0開始)
遞減指令:decr(默認從0開始,遞減會出現負數,這點跟memcache不一樣,mc到0)
如下:
附上shardedJedisPool和JedisCluster的兩種實現方式:
shardedJedisPool:
@Overridepublic Long decr(String key) {ShardedJedis jedis = null;Long result = 0l;try {jedis = shardedJedisPool.getResource();result = jedis.decr(key);} catch (Exception e) {log.error("redis decr error and key = " + key, e);}return result;}@Overridepublic Long incr(String key) {ShardedJedis jedis = null;Long result = 0l;try {jedis = shardedJedisPool.getResource();result = jedis.incr(key);} catch (Exception e) {log.error("redis incr error and key = " + key, e);}return result;}JedisCluster:
@Overridepublic Long decr(String key) {Long result = 0l;try {result = jedisCluster.decr(key);} catch (Exception e) {log.error("jedisCluster decr error and key = " + key, e);}return result;}@Overridepublic Long incr(String key) {Long result = 0l;try {result = jedisCluster.incr(key);} catch (Exception e) {log.error("jedisCluster incr error and key = " + key, e);}return result;}適用場景:
高并發生成訂單號,秒殺類的業務邏輯等。。
轉載于:https://www.cnblogs.com/jager/p/5849269.html
總結
以上是生活随笔為你收集整理的Redis适用于高并发的递增、递减功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UIAppearanceContaine
- 下一篇: Vs 2015 调试ASP.NET Co