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;
        }

    适用场景:

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

  • 相关阅读:
    异常
    gateway 网关
    Hystrix 图形化界面
    Hystrix 服务限流
    linux-CenOS修复内核或修改内核系统启动顺序
    Linux-CentOS不能yum update/upgrade.除非是初始环境
    Linux-CentOS配置网卡
    安装SQLSERVER2008提示错误SQL100MSI
    金蝶采购标志
    Pycharm社区版安装外部库文件
  • 原文地址:https://www.cnblogs.com/jager/p/5849269.html
Copyright © 2011-2022 走看看