zoukankan      html  css  js  c++  java
  • Redis适用于高并发的递增、递减功能

    递增指令:incr(默认从0开始)

    递减指令:decr(默认从0开始,递减会出现负数,这点跟memcache不一样,mc到0)

    如下:

    附上shardedJedisPool和JedisCluster的两种实现方式:

    shardedJedisPool:

    @Override
        public 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;
        }
    
        @Override
        public 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:

    @Override
        public 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;
        }
    
        @Override
        public 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;
        }

    适用场景:

      高并发生成订单号,秒杀类的业务逻辑等。。

  • 相关阅读:
    topcoder srm 495 div1
    topcoder srm 500 div1
    topcoder srm 485 div1
    topcoder srm 490 div1
    IDEWorkspaceChecks.plist文件是干什么用的?
    博客推荐
    如何使用U盘安装macOS high Sierra?
    小程序--模板消息调研
    小程序--剖析小程序上传文件
    小程序--小程序开发过程中遇到的问题以及解决方案
  • 原文地址:https://www.cnblogs.com/jager/p/5849269.html
Copyright © 2011-2022 走看看