@Component
public class RedisDistributedLock {
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 锁的过期时间,单位毫秒
private static final long LOCK_EXPIRE_TIME = 30000;
// 获取锁的 Lua 脚本
private static final String LOCK_SCRIPT =
"if redis.call('setnx', KEYS[1], ARGV[1]) == 1 then " +
"redis.call('pexpire', KEYS[1], ARGV[2]); " +
"return true; " +
"else return false; " +
"end";
// 释放锁的 Lua 脚本
private static final String UNLOCK_SCRIPT =
"if redis.call('get', KEYS[1]) == ARGV[1] then " +
"redis.call('del', KEYS[1]); " +
"return true; " +
"else return false; " +
"end";
// 获取分布式锁
public boolean lock(String key, String value) {
String[] keys = {key};
String[] args = {value, String.valueOf(LOCK_EXPIRE_TIME)};
RedisScript<Boolean> script = new DefaultRedisScript<>(LOCK_SCRIPT, Boolean.class);
Boolean result = redisTemplate.execute(script, Arrays.asList(keys), args);
return result != null && result;
}
// 释放分布式锁
public boolean unlock(String key, String value) {
String[] keys = {key};
String[] args = {value};
RedisScript<Boolean> script = new DefaultRedisScript<>(UNLOCK_SCRIPT, Boolean.class);
Boolean result = redisTemplate.execute(script, Arrays.asList(keys), args);
return result != null && result;
}
}