关于StringRedisTemplate 和 RedisTemplate 的加解锁

//单redis 通过StringRedisTemplate 进行解锁
private StringRedisTemplate stringRedisTemplate;
/**
     * 等待锁 获取到锁返回true
     * @param key 加锁的key
     * @param value 加锁的值
     * @param waitTime 要等待的时间毫秒
     * @return
     */
    public boolean waitLock(String key,String value,long waitTime){
        long start = System.currentTimeMillis();
        if(waitTime<=0){waitTime = timeout;}
        SecureRandom random = new SecureRandom();
        while (true){
            if(System.currentTimeMillis()-start > waitTime){
                return false;
            }
            Boolean absent = stringRedisTemplate.opsForValue().setIfAbsent(key, value, waitTime, TimeUnit.MILLISECONDS);
            if(absent!=null && absent == true){
                return true;
            }
            try{//休眠 10 -100毫秒
                Thread.sleep(random.nextInt(90)+10);
            }catch (Exception e){e.printStackTrace();}
        }
    }
/**
     * 解锁
     * @param key
     * @param value
     * @return
     */
    public boolean unlock(String key,String value){
        try{
            String script = "if redis.call('get', KEYS[1]) == KEYS[2] then return redis.call('del', KEYS[1]) else return 0 end";
            RedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
            List<String> keys = Arrays.asList(key, value);
            Object result = stringRedisTemplate.execute(redisScript, keys);
            return RELEASE_SUCCESS.equals(result);
        }catch (Exception e){
            logger.error("redis 解锁错误:",e);
        }
        return false;
    }

//分片式redis 
private RedisTemplate redisTemplate;
/**
     * 等待锁 获取到锁返回true
     * @param key 加锁的key
     * @param value 加锁的值
     * @param waitTime 要等待的时间毫秒
     * @return
     */
    public boolean waitLock(String key,String value,long waitTime){
        long start = System.currentTimeMillis();
        if(waitTime<=0){waitTime = timeout;}
        SecureRandom random = new SecureRandom();
        while (true){
            if(System.currentTimeMillis()-start > waitTime){
                return false;
            }
            Boolean absent = redisTemplate.opsForValue().setIfAbsent(key, value, waitTime, TimeUnit.MILLISECONDS);
            if(absent!=null && absent == true){
                return true;
            }
            try{//休眠 10 -100毫秒
                Thread.sleep(random.nextInt(90)+10);
            }catch (Exception e){e.printStackTrace();}
        }
    }
    /**
     * 解锁
     * @param key
     * @param value
     * @return
     */
    public boolean unlock(String key,String value){
        try{
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            RedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
            Long status = (Long) this.redisTemplate.execute(redisScript,Collections.singletonList(key),value);
            return RELEASE_SUCCESS.equals(status);
        }catch (Exception e){
            logger.error("redis 解锁错误:",e);
        }
        return false;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值