|
package memcached;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;
public class TestJedisLock {
Jedis jedis = new Jedis("127.0.0.1", 6379);
public static void main(String[] args) {
TestJedisLock t = new TestJedisLock();
System.out.println(t.testLock());
}
public boolean testLock() {
String pk = "mypk";
String lockPk = "lock" + pk;
String lockValue = String.valueOf(System.currentTimeMillis()) + "111";
try {
if(jedis.exists(lockPk)) { // other one locked already
System.out.println("Already locked pk:" + pk);
return false;
}
jedis.watch(lockPk);// make sure below operation is particle
jedis.multi();
Transaction trans = new Transaction(jedis.getClient());
trans.set(lockPk, lockValue);
trans.expire(lockPk, 60*60);
List<Object> ret = trans.exec();
if(ret == null) {
System.out.println("Concurrent lock fail for pk:" + pk);
return false;
}
String lockedValue = jedis.get(lockPk);
return lockValue.equals(lockedValue); // locked success
} catch (Exception e) {
System.out.println("Exception in lock for pk:" + pk);
return false;
}
}
}
|
|
|
|
|
|
|