/** * 占用锁,并设置唯一锁id * @param lockKey 锁key * @return 锁id */public String lock(String lockKey, Integer timeout) { //获得jedis实例 Jedis jedis = redisUtil.getJedis(); //锁id(必须拥有此id才能释放锁) Strin
/**
* 占用锁,并设置唯一锁id
* @param lockKey 锁key
* @return 锁id
*/
public String lock(String lockKey, Integer timeout) {
//获得jedis实例
Jedis jedis = redisUtil.getJedis();
//锁id(必须拥有此id才能释放锁)
String lockId = UUID.randomUUID().toString();
//占用锁同时设置失效时间 px:过期时间单位为毫秒 EX:过期时间为秒
String isSuccees = jedis.set(lockKey, lockId, "NX","PX", timeout);
//占用锁成功返回锁id,否则返回null
if("OK".equals(isSuccees)){
return lockId;
}else{
return null;
}
}
/**
* 释放锁拥有唯一锁id
* @param lockKey 锁key
* @param lockId 加锁id
*/
public void unlock(String lockKey,String lockId) {
if(lockId != null){
//获得jedis实例
Jedis jedis = redisUtil.getJedis();
//执行Lua代码删除lockId匹配的锁
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(lockId));
}
}
看上去好像没什么问题。