Redis 分布式锁

最近项目又用到分布式锁,下面把自己写的redis分布式锁代码贡献出来。


public class RedisManager {

    private static final String SET_IF_NOT_EXIST = "NX";//仅在键不存在时设置键。
    private static final String SET_WITH_EXPIRE_TIME = "EX";//PX表示超时时间是毫秒设置,EX表示超时时间是为 second 秒
    private static final Long RELEASE_SUCCESS = 1L;
    /**
     * 单线程独享变量
     **/
    private ThreadLocal<String> threadLocal = new ThreadLocal<>();

    @Autowired
    private MergeJedisCmd jedisCmd;

    /***
     * @param  key  键
     * @return string
     * */
    public String get(String key) {
        return jedisCmd.get(key);
    }


    /***
     * @param  key  键
     * @param value  值
     * @param  expiredTime  秒
     * @return true  false
     * 将 key 的值设为 value ,当且仅当 key 不存在。
     * 若给定的 key 已经存在,则 SETNX 不做任何动作。
     * */
    public boolean set(String key, int expiredTime, String value) {
        String result = jedisCmd.setex(key, expiredTime, value);
        return result == null ? false : true;
    }


    /***
     * @param  key  键
     * @param value  值
     * @return true  false
     * 将 key 的值设为 value ,当且仅当 key 不存在。
     * 若给定的 key 已经存在,则 SETNX 不做任何动作。
     * */
    public boolean set(String key, String value) {
        return jedisCmd.setnx(key, value) > 0;
    }

    /**
     * 尝试获取分布式锁
     *
     * @param lockKey    锁
     * @param expireTime 超期时间  300(秒)
     * @return 是否获取成功
     */
    public boolean lock(String lockKey, long expireTime) {
        String uuid = UUID.randomUUID().toString();
        String value = jedisCmd.set(lockKey, uuid, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime);
        if (value != null && value.equals("OK")) {
            threadLocal.set(uuid);
            return true;
        }
        return false;
    }

    /**
     * 释放分布式锁
     * git
     *
     * @param lockKey 锁
     * @return 是否释放成功
     */
    public boolean unLock(String lockKey) {
        String key = jedisCmd.get(lockKey);
        //可能会存在锁失效的时候
        if (key == null) {
            return true;
        }
        //保持原子性  (Get —>判断 -> del )操作
        String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end ";
        Object object = jedisCmd.eval(script, Collections.singletonList(lockKey), Collections.singletonList(threadLocal.get()));
        if (RELEASE_SUCCESS.equals(object)) {
            return true;
        }
        return false;
    }

由于MergeJedisCmd 是自己项目中封装了,所以大家可以用redis 客户端的原类。

1.MergeJedisCmd extends JedisCmd, BinaryJedisCmd

 2.JedisCmd extends JedisCommands, MultiKeyCommands, ScriptingCommands

3.BinaryJedisCmd extends BinaryJedisCommands, MultiKeyBinaryCommands, BinaryScriptingCommands

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值