RedissonLock refactoring

pull/337/head
Nikita 9 years ago
parent 41ee17d2e1
commit 2ca2358abe

@ -183,20 +183,18 @@ public class RedissonLock extends RedissonExpirable implements RLock {
internalLockLeaseTime = unit.toMillis(leaseTime); internalLockLeaseTime = unit.toMillis(leaseTime);
return commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, return commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local v = redis.call('get', KEYS[1]); " + "if (redis.call('exists', KEYS[1]) == 0) then " +
"if (v == false) then " + "redis.call('hset', KEYS[1], ARGV[2], 1); " +
" redis.call('set', KEYS[1], cjson.encode({['o'] = ARGV[1], ['c'] = 1}), 'px', ARGV[2]); " + "redis.call('pexpire', KEYS[1], ARGV[1]); " +
" return nil; " + "return nil; " +
"else " + "end; " +
" local o = cjson.decode(v); " + "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
" if (o['o'] == ARGV[1]) then " + "redis.call('hincrby', KEYS[1], ARGV[2], 1); " +
" o['c'] = o['c'] + 1; " + "redis.call('pexpire', KEYS[1], ARGV[1]); " +
" redis.call('set', KEYS[1], cjson.encode(o), 'px', ARGV[2]); " + "return nil; " +
" return nil; " + "end; " +
" end;" + "return redis.call('pttl', KEYS[1]);",
" return redis.call('pttl', KEYS[1]); " + Collections.<Object>singletonList(getName()), internalLockLeaseTime, getLockName());
"end",
Collections.<Object>singletonList(getName()), getLockName(), internalLockLeaseTime);
} }
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
@ -272,26 +270,24 @@ public class RedissonLock extends RedissonExpirable implements RLock {
@Override @Override
public void unlock() { public void unlock() {
Boolean opStatus = commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, Boolean opStatus = commandExecutor.evalWrite(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"local v = redis.call('get', KEYS[1]); " + "if (redis.call('exists', KEYS[1]) == 0) then " +
"if (v == false) then " + "redis.call('publish', KEYS[2], ARGV[1]); " +
" redis.call('publish', KEYS[2], ARGV[2]); " + "return 1; " +
" return 1; " + "end;" +
"else " + "if (redis.call('hexists', KEYS[1], ARGV[3]) == 0) then " +
" local o = cjson.decode(v); " + "return nil;" +
" if (o['o'] == ARGV[1]) then " + "end; " +
" o['c'] = o['c'] - 1; " + "local counter = redis.call('hincrby', KEYS[1], ARGV[3], -1); " +
" if (o['c'] > 0) then " + "if (counter > 0) then " +
" redis.call('set', KEYS[1], cjson.encode(o), 'px', ARGV[3]); " + "redis.call('pexpire', KEYS[1], ARGV[2]); " +
" return 0;"+ "return 0; " +
" else " + "else " +
" redis.call('del', KEYS[1]);" + "redis.call('del', KEYS[1]); " +
" redis.call('publish', KEYS[2], ARGV[2]); " + "redis.call('publish', KEYS[2], ARGV[1]); " +
" return 1;"+ "return 1; "+
" end" + "end; " +
" end;" + "return nil;",
" return nil; " + Arrays.<Object>asList(getName(), getChannelName()), unlockMessage, internalLockLeaseTime, getLockName());
"end",
Arrays.<Object>asList(getName(), getChannelName()), getLockName(), unlockMessage, internalLockLeaseTime);
if (opStatus == null) { if (opStatus == null) {
throw new IllegalMonitorStateException("attempt to unlock read lock, not locked by current thread by node id: " throw new IllegalMonitorStateException("attempt to unlock read lock, not locked by current thread by node id: "
+ id + " thread-id: " + Thread.currentThread().getId()); + id + " thread-id: " + Thread.currentThread().getId());
@ -331,34 +327,16 @@ public class RedissonLock extends RedissonExpirable implements RLock {
@Override @Override
public boolean isHeldByCurrentThread() { public boolean isHeldByCurrentThread() {
Boolean opStatus = commandExecutor.evalRead(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, return commandExecutor.read(getName(), LongCodec.INSTANCE, RedisCommands.HEXISTS, getName(), getLockName());
"local v = redis.call('get', KEYS[1]); " +
"if (v == false) then " +
" return 0; " +
"else " +
" local o = cjson.decode(v); " +
" if (o['o'] == ARGV[1]) then " +
" return 1; " +
" else" +
" return 0; " +
" end;" +
"end",
Collections.<Object>singletonList(getName()), getLockName());
return opStatus;
} }
@Override @Override
public int getHoldCount() { public int getHoldCount() {
Long opStatus = commandExecutor.evalRead(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, Long res = commandExecutor.read(getName(), LongCodec.INSTANCE, RedisCommands.HGET, getName(), getLockName());
"local v = redis.call('get', KEYS[1]); " + if (res == null) {
"if (v == false) then " + return 0;
" return 0; " + }
"else " + return res.intValue();
" local o = cjson.decode(v); " +
" return o['c']; " +
"end",
Collections.<Object>singletonList(getName()));
return opStatus.intValue();
} }
@Override @Override

Loading…
Cancel
Save