Redis分布式锁解锁案例讲解
Redis分布式锁解锁案例
相关视频教程(来自动力节点):https://www.bilibili.com/video/BV1Uz4y1X72A
相关资料下载:www.bjpowernode.com/?cnblogs
1、解锁代码
还是先展示代码,再带大家慢慢解释为什么这样实现:
public class RedisTool { private static final Long RELEASE_SUCCESS = 1L; /** * 释放分布式锁 * @param jedis Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(Jedis jedis, String lockKey, String requestId) { String script = "if redis.call("get", KEYS[1]) == ARGV[1] then return redis.call("del", KEYS[1]) else return 0 end"; Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } }