简易的redis分布式锁 RedisLockUtil

实现的诉求,多台服务器上,执行相同的方法,一次只能执行一个,如果多个请求了执行方法,那么排队等待执行。

方案:采用三方服务redis的key做唯一标识控制。超时后。

复制代码
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import com.ccjr.commons.utils.SpringUtils;

import lombok.extern.slf4j.Slf4j;


/**
 * @desc REDIS分布式锁工具类
 * @author 陈惟鲜 chenweixian
 * @date 2020年8月19日 下午4:20:46
 *
 */
@Slf4j
public class RedisLockUtil {
    
    @SuppressWarnings("unchecked")
    private static RedisTemplate<String, Object> redisTemplate = (RedisTemplate<String, Object>) SpringUtils.getBean("redisTemplate");
    
    private static long lockTimeout = 5*1000L; //锁定时间30s

    /**
     * 加锁
     *
     * @param key
     * @param value 当前时间+ 超时时间
     * @return
     */
    public static boolean lock(String key, long currvalue) {
        String value = currvalue+lockTimeout+"";
        //未加锁,则加锁
        if (redisTemplate.opsForValue().setIfAbsent(key, value)) {
            return true;
        }
        
        do {
            //获取锁解锁时间
            String currentValue = (String)redisTemplate.opsForValue().get(key);
            //如果锁过期
            if (!StringUtils.isEmpty(currentValue) && Long.parseLong(currentValue) < System.currentTimeMillis()) {
                //获得上一个锁的时间
                String olcValue = (String)redisTemplate.opsForValue().getAndSet(key, value);
                if (!StringUtils.isEmpty(olcValue) && olcValue.equals(currentValue)) {
                    return true;
                }
            }
            try {
//                log.info("等待500 ms key:{},value:{},cur:{}, 剩余:{}",key,value, System.currentTimeMillis(), Long.parseLong(value)-System.currentTimeMillis());
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }while(Long.parseLong(value) > System.currentTimeMillis());

        return false;
    }

    /**
     * 解锁
     *
     * @param key
     * @param value
     */
    public static void unlock(String key, long currvalue) {
        try {
            String value = currvalue+lockTimeout+"";
            String currentVlue = (String)redisTemplate.opsForValue().get(key);
            if (!StringUtils.isEmpty(currentVlue) && currentVlue.equals(value)) {
                redisTemplate.opsForValue().getOperations().delete(key);
            }
        } catch (Exception e) {
            log.error("【redis分布式锁】 解锁异常" + e.getMessage(), e);
        }
    }
}
复制代码

 

测试:

复制代码
    @Test
    public void testRedisLock() {
        String key = "testRedisLock";
        for (int i=0;i<100;i++) {
            long currvalue = System.currentTimeMillis();
            try {
                System.out.println(i + "A="+ RedisLockUtil.lock(key, currvalue));

                System.out.println("做了些事情开始。。");
//                try {
//                    Thread.sleep(4000);
//                } catch (InterruptedException e) {}
                System.out.println("做了些事情结束。。");
            }
            finally {
                currvalue = System.currentTimeMillis();
                System.out.println(i + "B="+ RedisLockUtil.lock(key, currvalue));
                RedisLockUtil.unlock(key, currvalue);
                System.out.println(i + "C="+ RedisLockUtil.lock(key, currvalue));
            }
        }
    }
复制代码

 

posted on   陈惟鲜的博客  阅读(1657)  评论(0编辑  收藏  举报

编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
历史上的今天:
2020-01-20 docker 安装 redis
2020-01-20 docker 安装 tomcat ,dubbo-admin
2020-01-20 docker 安装 zookeeper
2020-01-20 docker 安装 activemq

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示