Posted on 2014-01-15 19:00
云云 阅读(13383)
评论(1) 编辑 收藏
public static boolean acquireLock(String lock) {
// 1. 通过SETNX试图获取一个lock
boolean success = false;
Jedis jedis = pool.getResource();
long value = System.currentTimeMillis() + expired + 1;
System.out.println(value);
long acquired = jedis.setnx(lock, String.valueOf(value));
//SETNX成功,则成功获取一个锁
if (acquired == 1)
success = true;
//SETNX失败,说明锁仍然被其他对象保持,检查其是否已经超时
else {
long oldValue = Long.valueOf(jedis.get(lock));
//超时
if (oldValue < System.currentTimeMillis()) {
String getValue = jedis.getSet(lock, String.valueOf(value));
// 获取锁成功
if (Long.valueOf(getValue) == oldValue)
success = true;
// 已被其他进程捷足先登了
else
success = false;
}
//未超时,则直接返回失败
else
success = false;
}
pool.returnResource(jedis);
return success;
}
//释放锁
public static void releaseLock(String lock) {
Jedis jedis = pool.getResource();
long current = System.currentTimeMillis();
// 避免删除非自己获取得到的锁
if (current < Long.valueOf(jedis.get(lock)))
jedis.del(lock);
pool.returnResource(jedis);
}
//--------------------------
public Long acquireLock(final String lockName,final long expire){
return redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) {
byte[] lockBytes = redisTemplate.getStringSerializer().serialize(lockName);
boolean locked = connection.setNX(lockBytes, lockBytes);
connection.expire(lockBytes, expire);
if(locked){
return 1L;
}
return 0L;
}
});
}
//原子操作 -----------------------
public String getAndSet(final String key,final String value){
return redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection)
throws DataAccessException {
byte[] result = connection.getSet(redisTemplate.getStringSerializer().serialize(key),
redisTemplate.getStringSerializer().serialize(value));
if(result!=null){
return new String(result);
}
return null;
}
});
}