简单的Redis并发计数工具

Redis并发计数工具

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Redis并发计数工具
 *
 * @author caochong
 * @version 1.0
 */
public class RedisCountLock {

    private final AtomicInteger reentryCountAtomic = new AtomicInteger(0);
    private final RedisTemplate<String, Integer> redisTemplate;
    private final String NUM_KEY;
    private int maxWaitNum = 20;
    private long retryWaitTime = 100L;

    public RedisCountLock(RedisTemplate<String, Integer> redisTemplate, String NUM_KEY) {
        this.redisTemplate = redisTemplate;
        this.NUM_KEY = NUM_KEY;
    }

    public RedisCountLock(RedisTemplate<String, Integer> redisTemplate, String NUM_KEY,
                          int maxWaitNum, long retryWaitTime) {
        this.redisTemplate = redisTemplate;
        this.NUM_KEY = NUM_KEY;
        this.maxWaitNum = maxWaitNum;
        this.retryWaitTime = retryWaitTime;
    }

    /**
     * 增加计数
     */
    public void incr() throws InterruptedException {
        ValueOperations<String, Integer> ops = redisTemplate.opsForValue();
        do {
            Integer currentVal = ops.get(NUM_KEY);
            if (currentVal != null) {
                if (currentVal < maxWaitNum) {
                    long updateVal = Optional.ofNullable(ops.increment(NUM_KEY, 1)).orElse(currentVal + 1L);
                    if (updateVal - 1 <= currentVal) {
                        break;
                    } else {
                        // System.out.println("补偿:" + currentVal + " - " + updateVal + " - " + reentryCountAtomic.get());
                        // 补偿redis计数
                        ops.increment(NUM_KEY, -1);
                    }
                }
                Thread.sleep(retryWaitTime + ThreadLocalRandom.current().nextInt(-50, 50));
            } else {
                ops.setIfAbsent(NUM_KEY, 0);
            }
        } while (true);
        reentryCountAtomic.incrementAndGet(); // 重入次数
    }

    /**
     * 减计数
     */
    public void decr() {
        int reentryCount = reentryCountAtomic.getAndDecrement(); // 扣减重入次数
        if (reentryCount <= 0) {
            // 重入补偿
            System.out.println("扣减补偿:" + reentryCount + " - " + reentryCountAtomic.get());
            reentryCountAtomic.incrementAndGet();
        } else {
            // 扣成功减计数
            ValueOperations<String, Integer> ops = redisTemplate.opsForValue();
            ops.increment(NUM_KEY, -1);
        }
    }

    public int reentryCount() {
        return reentryCountAtomic.get();
    }
}

通过标识分组的分布式重入互斥锁(缺少续期,可自行追加)

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.concurrent.TimeUnit;

/**
 * Redis锁
 *
 * @author caoch
 */
public class RedisLock {
    private static final Logger logger = LoggerFactory.getLogger(RedisLock.class);

    private final RedisTemplate<String, Object> redisTemplate;

    private final int lockType;

    private final String key;

    private volatile boolean isOpen = false;

    // 互斥锁特有:时间版本
    private volatile String lockTypeVersion = "";
    private volatile long lockTimeVersion = 0;
    private volatile long lockExTime = 0;

    public RedisLock(RedisTemplate<String, Object> redisTemplate, String key) {
        this.redisTemplate = redisTemplate;
        this.key = key;
        this.lockType = 0;
    }

    public RedisLock(RedisTemplate<String, Object> redisTemplate, String key, int lockType) {
        this.redisTemplate = redisTemplate;
        this.key = key;
        this.lockType = lockType;
    }

    /**
     * redis等待锁
     *
     * @param lockExTime  锁时长,单位:秒
     * @param sleepTime   重试间隔,单位:毫秒
     * @param maxWaitTime 最大等待时长,单位:毫秒
     * @return 是否获取到锁
     * @throws InterruptedException 中断异常
     */
    public boolean tryLock(long lockExTime, long sleepTime,
                           long maxWaitTime) throws InterruptedException {
        if (this.lockType != 0) {
            throw new RuntimeException("Lock method not supported: tryLock");
        }
        long keyTime = System.currentTimeMillis();
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        do {
            Object obj = ops.get(key);
            if (obj != null) {
                if (keyTime == Long.parseLong(obj.toString())) {
                    isOpen = true;
                    return true;
                }
                if (sleepTime > 0L) {
                    Thread.sleep(sleepTime);
                }
                long currentTime = System.currentTimeMillis();
                if (currentTime - keyTime > maxWaitTime) {
                    return false;
                }
            } else {
                ops.setIfAbsent(key, keyTime, lockExTime, TimeUnit.SECONDS);
            }
        } while (true);
    }

    /**
     * 解锁
     */
    public void unlock() {
        if (this.lockType != 0) {
            throw new RuntimeException("Lock method not supported: unlock");
        }
        if (isOpen) {
            redisTemplate.delete(key);
        }
    }

    /**
     * redis等待互斥锁(基于次数的)
     *
     * @param currentType 互斥类型
     * @param lockExTime  锁时长,单位:秒
     * @param sleepTime   重试间隔,单位:毫秒
     * @param maxWaitTime 最大等待时长,单位:毫秒
     * @return 是否获取到锁
     * @throws InterruptedException 中断异常
     */
    public boolean tryMutexLock(final String currentType, long lockExTime, long sleepTime,
                                long maxWaitTime) throws InterruptedException {
        if (this.lockType != 1) {
            throw new RuntimeException("Lock method not supported: tryMutexLock");
        }
        if (currentType == null || currentType.startsWith("-")) {
            throw new RuntimeException("Type is error about MutexLock: " + currentType);
        }

        ValueOperations<String, Object> ops = redisTemplate.opsForValue();

        long keyTime = System.currentTimeMillis();
        long lockVersion = timeVersion(keyTime);
        long currentLockNum = 1;
        String currentStr = currentType + ":" + lockVersion;
        String reentryKey = "";
        do {
            Boolean aBoolean = ops.setIfAbsent(key + ":LOCK", lockVersion, 10, TimeUnit.SECONDS);
            if (Boolean.TRUE.equals(aBoolean)) {
                String str;
                try {
                    Object obj = ops.get(key);
                    if (obj == null) {
                        ops.setIfAbsent(key, currentStr + ":" + 1, lockExTime, TimeUnit.SECONDS);
                        break;
                    }
                    str = obj.toString();
                    if (str.isEmpty()) {
                        ops.set(key, currentStr + ":" + 1, lockExTime, TimeUnit.SECONDS);
                        break;
                    }

                    if (str.equals(reentryKey)) {
                        // 重入
                        Object objT = ops.get(key);
                        if (objT != null && objT.equals(reentryKey)) {
                            ops.set(key, currentStr + ":" + currentLockNum, lockExTime, TimeUnit.SECONDS);
                            logger.info("检测到重入加入, 重入锁值: {}, 当前锁值: {}:{}", str, currentStr, currentLockNum);
                            break;
                        }

                        continue;
                    }
                } finally {
                    redisTemplate.delete(key + ":LOCK");
                }

                if (!str.contains(":")) {
                    logger.error("RedisLock is ERROR, Format exception: {}", str);
                    throw new RuntimeException("RedisLock is ERROR, Format exception: " + str);
                }

                int i = str.indexOf(":");
                int j = str.lastIndexOf(":");
                String type = str.substring(0, i);
                long time = Long.parseLong(str.substring(i + 1, j));
                int rNum = Integer.parseInt(str.substring(j + 1));

                // 非同类型锁直接等待。同类型新锁时间版本增加,必须再抢锁,但可以快速抢
                if (!currentType.equals(type) || lockVersion <= time) {
                    // 计时器
                    if (sleepTime > 0L) {
                        Thread.sleep(sleepTime);
                    }
                    currentLockNum = 1;
                } else {
                    reentryKey = str;
                    currentLockNum = rNum + 1;
                    // System.out.println("检测到可重入:" + reentryKey);
                }

                long timeMillis = System.currentTimeMillis();
                lockVersion = timeVersion(timeMillis);
                if (timeMillis - keyTime > maxWaitTime) {
                    return false;
                }
                currentStr = currentType + ":" + lockVersion;
            }
        } while (true);

        // 结束
        isOpen = true;
        lockTypeVersion = currentType;
        lockTimeVersion = lockVersion;
        this.lockExTime = lockExTime;
        logger.info("创建互斥锁:{}", currentStr);
        return true;
    }

    /**
     * 互斥锁解锁(基于次数的)
     */
    public void unMutexLock() {
        if (this.lockType != 1) {
            throw new RuntimeException("Lock method not supported: unMutexLock");
        }
        if (isOpen) {
            ValueOperations<String, Object> ops = redisTemplate.opsForValue();
            String unLockStr = "-:0:0";
            do {
                Boolean aBoolean = ops.setIfAbsent(key + ":LOCK", lockTimeVersion, 10, TimeUnit.SECONDS);
                if (Boolean.TRUE.equals(aBoolean)) {
                    try {
                        Object obj = ops.get(key);
                        if (obj == null) {
                            // 找不到锁了,理论上不会
                            // 如果存在,锁版本时间相同,且被解锁,但无新入(BUG)
                            logger.error("检测到解锁意外为空, 待解锁锁值: {}:{}", lockTypeVersion, lockTimeVersion);
                            return;
                        }
                        String str = obj.toString();
                        if (str.isEmpty()) {
                            // 找不到锁了,理论上不会,除非值异常
                            logger.error("检测到解锁空值, 待解锁锁值: {}:{}", lockTypeVersion, lockTimeVersion);
                            return;
                        }

                        if (!str.contains(":")) {
                            logger.error("RedisLock is ERROR, Format exception: {}", str);
                            throw new RuntimeException("RedisLock is ERROR, Format exception: " + str);
                        }

                        int i = str.indexOf(":");
                        int j = str.lastIndexOf(":");
                        String type = str.substring(0, i);
                        int rNum = Integer.parseInt(str.substring(j + 1));

                        // 同类型锁才允许释放
                        if (lockTypeVersion.equals(type)) {
                            int unLockNum = rNum - 1;
                            if (unLockNum <= 0) {
                                redisTemplate.delete(key);
                                logger.info("解锁成功(解锁):" + str + " " + unLockStr);
                            } else {
                                unLockStr = lockTypeVersion + ":" + lockTimeVersion + ":" + unLockNum;
                                ops.set(key, unLockStr, lockExTime, TimeUnit.SECONDS);
                                logger.info("解锁成功(更新锁):" + str + " " + unLockStr);
                            }
                            return;
                        } else {
                            // 锁已经被意外更新,理论上不会
                            // 如果存在,锁版本时间相同,且被解锁,但有新类型插入(可能是BUG)
                            logger.warn("检测到解锁意外更新, 当前锁值: {}, 待解锁锁值: {}:{}",
                                    str, lockTypeVersion, lockTimeVersion);
                            return;
                        }
                    } finally {
                        redisTemplate.delete(key + ":LOCK");
                    }
                }
            } while (true);
        }
    }


    private static long timeVersion(long timeMillis) {
        long nanoTime = System.nanoTime();
        return (timeMillis * 100_000L + (nanoTime % 10_000_000L) / 100);
    }

    /**
     * redis等待互斥锁(基于时间的,只要求时间序列的)
     *
     * @param currentType 互斥类型
     * @param lockExTime  锁时长,单位:秒
     * @param sleepTime   重试间隔,单位:毫秒
     * @param maxWaitTime 最大等待时长,单位:毫秒
     * @return 是否获取到锁
     * @throws InterruptedException 中断异常
     */
    public boolean tryMutexTimeLock(final String currentType, long lockExTime, long sleepTime,
                                    long maxWaitTime) throws InterruptedException {
        if (this.lockType != 2) {
            throw new RuntimeException("Lock method not supported: tryMutexTimeLock");
        }
        if (currentType == null || currentType.startsWith("-")) {
            throw new RuntimeException("Type is error about MutexLock: " + currentType);
        }

        ValueOperations<String, Object> ops = redisTemplate.opsForValue();

        long keyTime = System.currentTimeMillis();
        long currentTime = timeVersion(keyTime);
        String currentStr = currentType + ":" + currentTime;
        String reentryKey = "";
        do {
            Object obj = ops.get(key);
            if (obj == null) {
                ops.setIfAbsent(key, currentStr, lockExTime, TimeUnit.SECONDS);
                continue;
            }
            String str = obj.toString();
            if (str.isEmpty() || str.equals(reentryKey)) {
                // 重入
                Boolean aBoolean = ops.setIfAbsent(key + ":LOCK", currentTime, 10, TimeUnit.SECONDS);
                if (Boolean.TRUE.equals(aBoolean)) {
                    try {
                        ops.set(key, currentStr, lockExTime, TimeUnit.SECONDS);
                        // System.out.println("检测到重入加入:" + reentryKey);
                    } finally {
                        redisTemplate.delete(key + ":LOCK");
                    }
                }
                continue;
            }
            // 结束
            if (str.equals(currentStr)) {
                isOpen = true;
                lockTypeVersion = currentType;
                lockTimeVersion = currentTime;
                // System.out.println("创建锁:" + lockTypeVersion + ":" + lockTimeVersion);
                return true;
            }

            if (!str.contains(":")) {
                logger.error("RedisLock is ERROR, Format exception: {}", str);
                throw new RuntimeException("RedisLock is ERROR, Format exception: " + str);
            }

            int i = str.indexOf(":");
            String type = str.substring(0, i);
            long time = Long.parseLong(str.substring(i + 1));

            // 非同类型锁直接等待。同类型新锁时间版本增加,必须再抢锁,但可以快速抢
            if (!currentType.equals(type) || currentTime <= time) {
                // 计时器
                if (sleepTime > 0L) {
                    Thread.sleep(sleepTime);
                }
            } else {
                reentryKey = str;
                // System.out.println("检测到可重入:" + reentryKey);
            }

            long timeMillis = System.currentTimeMillis();
            currentTime = timeVersion(timeMillis);
            if (timeMillis - keyTime > maxWaitTime) {
                return false;
            }
            currentStr = currentType + ":" + currentTime;
        } while (true);
    }

    /**
     * 互斥锁解锁(基于时间的,只要求时间序列的)
     */
    public void unMutexTimeLock() {
        if (this.lockType != 2) {
            throw new RuntimeException("Lock method not supported: unMutexTimeLock");
        }
        if (isOpen) {
            ValueOperations<String, Object> ops = redisTemplate.opsForValue();
            String unLockStr = "-:0";
            do {
                Object obj = ops.get(key);
                if (obj == null) {
                    // 找不到锁了,理论上不会
                    // 如果存在,锁版本时间相同,且被解锁,但无新入(时间BUG)
                    logger.error("检测到解锁意外为空, 待解锁锁值: {}:{}", lockTypeVersion, lockTimeVersion);
                    break;
                }
                String str = obj.toString();
                if (str.isEmpty()) {
                    // 找不到锁了,理论上不会,除非值异常
                    logger.error("检测到解锁空值, 待解锁锁值: {}:{}", lockTypeVersion, lockTimeVersion);
                    break;
                }
                if (str.equals(unLockStr)) {
                    // 抢占成功,解锁
                    Boolean aBoolean = ops.setIfAbsent(key + ":LOCK", lockTimeVersion, 10, TimeUnit.SECONDS);
                    if (Boolean.TRUE.equals(aBoolean)) {
                        try {
                            redisTemplate.delete(key);
                            // System.out.println("抢占成功解锁:" + str + " " + unLockStr);
                        } finally {
                            redisTemplate.delete(key + ":LOCK");
                        }
                        break;
                    }
                }

                if (!str.contains(":")) {
                    logger.error("RedisLock is ERROR, Format exception: {}", str);
                    throw new RuntimeException("RedisLock is ERROR, Format exception: " + str);
                }

                int i = str.indexOf(":");
                String type = str.substring(0, i);
                long time = Long.parseLong(str.substring(i + 1));

                // 同类型锁才允许释放
                if (lockTypeVersion.equals(type) || ("-" + lockTypeVersion).equals(type)) {
                    if (lockTimeVersion < time) {
                        // 锁版本时间小于现有的锁, 不做处理
                        // System.out.println("检测到解锁时间小于:" + str + ", " + time + " > " + lockTimeVersion);
                        break;
                    } else {
                        if (lockTimeVersion > time) {
                            // 锁版本时间小于现有的锁, 不做处理
                            // System.out.println("检测到解锁时间大于:" + str + ", " + time + " > " + lockTimeVersion);
                        }
                        // 完全相同或大于,则抢占解锁
                        // 此时只有同类型的会更新时间版本,但别的都进不了
                        Boolean aBoolean = ops.setIfAbsent(key + ":LOCK", lockTimeVersion, 10, TimeUnit.SECONDS);
                        if (Boolean.TRUE.equals(aBoolean)) {
                            try {
                                unLockStr = "-" + lockTypeVersion + ":" + lockTimeVersion;
                                ops.set(key, unLockStr, 10, TimeUnit.SECONDS);
                                // System.out.println("抢占解锁:" + str + " " + unLockStr);
                            } finally {
                                redisTemplate.delete(key + ":LOCK");
                            }
                        }
                    }
                } else {
                    // 锁已经被意外更新,理论上不会
                    // 如果存在,锁版本时间相同,且被解锁,但有新类型插入(可能是BUG)
                    logger.warn("检测到解锁意外更新, 当前锁值: {}, 待解锁锁值: {}:{}",
                            str, lockTypeVersion, lockTimeVersion);
                    break;
                }
            } while (true);
        }
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容