zoukankan      html  css  js  c++  java
  • redis加减原子操作

    public class RedisAutomaticUtils {
    
        /**
         * redis加减原子操作
         */
        private static int optAtomic(StringRedisTemplate redisTemplate, String key, boolean isAdd, Integer initValue, Integer changeValue,
                                     long timeout, TimeUnit unit) {
            RedisAtomicInteger counter;
            if (initValue == null) {
                counter = new RedisAtomicInteger(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()));
            } else {
                counter = new RedisAtomicInteger(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()), initValue);
            }
            if (NumberUtil.isEmpty(timeout) || unit == null)
                counter.expire(3, TimeUnit.DAYS); // 默认有效期3天
            else
                counter.expire(timeout, unit);
            return isAdd ? counter.addAndGet(changeValue) : counter.addAndGet(-changeValue);
        }
    
        private static long optAtomicLong(StringRedisTemplate redisTemplate, String key, boolean isAdd, Long initValue, Long changeValue,
                                         long timeout, TimeUnit unit) {
            RedisAtomicLong counter;
            if (initValue == null) {
                counter = new RedisAtomicLong(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()));
            } else {
                counter = new RedisAtomicLong(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()), initValue);
            }
            if (NumberUtil.isEmpty(timeout) || unit == null)
                counter.expire(3, TimeUnit.DAYS); // 默认有效期3天
            else
                counter.expire(timeout, unit);
            return isAdd ? counter.addAndGet(changeValue) : counter.addAndGet(-changeValue);
        }
    
        /**
         * redis原子锁来保证自增和自减的并发不会出错
         */
        public static int optUpValue(StringRedisTemplate redisTemplate, boolean isAdd, String keyValue, Integer initValue, Integer changeValue,
                                     long timeout, TimeUnit unit) {
            int count;
            if (Boolean.TRUE.equals(redisTemplate.hasKey(keyValue))) {
                count = optAtomic(redisTemplate, keyValue, isAdd, null, changeValue, timeout, unit);
            } else {
                //key值不存在的话就获取初始值
                count = optAtomic(redisTemplate, keyValue, isAdd, initValue == null ? 0 : initValue, changeValue, timeout, unit);
            }
            return count;
        }
    
        public static long optUpValueLong(StringRedisTemplate redisTemplate, boolean isAdd, String keyValue, Long initValue, Long changeValue,
                                         long timeout, TimeUnit unit) {
            long count;
            if (Boolean.TRUE.equals(redisTemplate.hasKey(keyValue))) {
                count = optAtomicLong(redisTemplate, keyValue, isAdd, null, changeValue, timeout, unit);
            } else {
                //key值不存在的话就获取初始值
                count = optAtomicLong(redisTemplate, keyValue, isAdd, initValue == null ? 0 : initValue, changeValue, timeout, unit);
            }
            return count;
        }
    
    }
    随笔看心情
  • 相关阅读:
    UVA 11925 Generating Permutations 生成排列 (序列)
    UVA 1611 Crane 起重机 (子问题)
    UVA 11572 Unique snowflakes (滑窗)
    UVA 177 PaperFolding 折纸痕 (分形,递归)
    UVA 11491 Erasing and Winning 奖品的价值 (贪心)
    UVA1610 PartyGame 聚会游戏(细节题)
    UVA 1149 Bin Packing 装箱(贪心)
    topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)
    UVA 1442 Cave 洞穴 (贪心+扫描)
    UVA 1609 Foul Play 不公平竞赛 (构(luan)造(gao)+递归)
  • 原文地址:https://www.cnblogs.com/stromgao/p/15726173.html
Copyright © 2011-2022 走看看