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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM