RBucket.compareAndSet & getAndSet optimization

pull/365/head
Nikita 9 years ago
parent 8e06adb95d
commit 4a3f378ab5

@ -19,7 +19,6 @@ import java.util.Collections;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.redisson.client.codec.Codec; import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;
import org.redisson.core.RBucket; import org.redisson.core.RBucket;
@ -28,8 +27,6 @@ import io.netty.util.concurrent.Future;
public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> { public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
private static final RedisCommand<Object> EVAL_GETSET = new RedisCommand<Object>("EVAL", 4);
protected RedissonBucket(CommandAsyncExecutor connectionManager, String name) { protected RedissonBucket(CommandAsyncExecutor connectionManager, String name) {
super(connectionManager, name); super(connectionManager, name);
} }
@ -46,22 +43,11 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
@Override @Override
public Future<Boolean> compareAndSetAsync(V expect, V update) { public Future<Boolean> compareAndSetAsync(V expect, V update) {
if (expect == null && update == null) { if (expect == null && update == null) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN, return trySetAsync(null);
"return redis.call('exists', KEYS[1]) == 0 then "
+ "return 1 "
+ "else "
+ "return 0 end",
Collections.<Object>singletonList(getName()));
} }
if (expect == null) { if (expect == null) {
return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_BOOLEAN_WITH_VALUES, return trySetAsync(update);
"if redis.call('exists', KEYS[1]) == 0 then "
+ "redis.call('set', KEYS[1], ARGV[1]); "
+ "return 1 "
+ "else "
+ "return 0 end",
Collections.<Object>singletonList(getName()), update);
} }
if (update == null) { if (update == null) {
@ -98,11 +84,7 @@ public class RedissonBucket<V> extends RedissonExpirable implements RBucket<V> {
Collections.<Object>singletonList(getName())); Collections.<Object>singletonList(getName()));
} }
return commandExecutor.evalWriteAsync(getName(), codec, EVAL_GETSET, return commandExecutor.writeAsync(getName(), codec, RedisCommands.GETSET, getName(), newValue);
"local v = redis.call('get', KEYS[1]); "
+ "redis.call('set', KEYS[1], ARGV[1]); "
+ "return v",
Collections.<Object>singletonList(getName()), newValue);
} }
@Override @Override

@ -199,7 +199,7 @@ public interface RedisCommands {
RedisStrictCommand<Void> DEL_VOID = new RedisStrictCommand<Void>("DEL", new VoidReplayConvertor()); RedisStrictCommand<Void> DEL_VOID = new RedisStrictCommand<Void>("DEL", new VoidReplayConvertor());
RedisCommand<Object> GET = new RedisCommand<Object>("GET"); RedisCommand<Object> GET = new RedisCommand<Object>("GET");
RedisCommand<Object> GETSET = new RedisCommand<Object>("GETSET"); RedisCommand<Object> GETSET = new RedisCommand<Object>("GETSET", 2);
RedisCommand<Void> SET = new RedisCommand<Void>("SET", new VoidReplayConvertor(), 2); RedisCommand<Void> SET = new RedisCommand<Void>("SET", new VoidReplayConvertor(), 2);
RedisCommand<Boolean> SETPXNX = new RedisCommand<Boolean>("SET", new BooleanNotNullReplayConvertor(), 2); RedisCommand<Boolean> SETPXNX = new RedisCommand<Boolean>("SET", new BooleanNotNullReplayConvertor(), 2);
RedisCommand<Boolean> SETNX = new RedisCommand<Boolean>("SETNX", new BooleanReplayConvertor(), 2); RedisCommand<Boolean> SETNX = new RedisCommand<Boolean>("SETNX", new BooleanReplayConvertor(), 2);

Loading…
Cancel
Save