RedissonAtomicLong.getAndSetAsync & RedissonAtomicLong.getAndAddAsync optimization. #360

pull/365/head
Nikita 9 years ago
parent 8558d3416e
commit 04b21e92a1

@ -17,8 +17,11 @@ package org.redisson;
import java.util.Collections;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.convertor.SingleConvertor;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.core.RAtomicLong;
@ -88,13 +91,13 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
}
@Override
public Future<Long> getAndAddAsync(long delta) {
return commandExecutor.evalWriteAsync(getName(),
StringCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local v = redis.call('get', KEYS[1]) or 0; "
+ "redis.call('set', KEYS[1], v + ARGV[1]); "
+ "return tonumber(v)",
Collections.<Object>singletonList(getName()), delta);
public Future<Long> getAndAddAsync(final long delta) {
return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, new RedisStrictCommand<Long>("INCRBY", new SingleConvertor<Long>() {
@Override
public Long convert(Object obj) {
return ((Long) obj) - delta;
}
}), getName(), delta);
}
@ -105,10 +108,7 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
@Override
public Future<Long> getAndSetAsync(long newValue) {
return commandExecutor.evalWriteAsync(getName(),
StringCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], ARGV[1]); return tonumber(v)",
Collections.<Object>singletonList(getName()), newValue);
return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.GETSET, getName(), newValue);
}
@Override

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

@ -19,8 +19,11 @@ import java.util.Collections;
import org.reactivestreams.Publisher;
import org.redisson.api.RAtomicLongReactive;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.RedisStrictCommand;
import org.redisson.client.protocol.convertor.SingleConvertor;
import org.redisson.command.CommandReactiveExecutor;
import reactor.rx.Streams;
@ -64,22 +67,19 @@ public class RedissonAtomicLongReactive extends RedissonExpirableReactive implem
}
@Override
public Publisher<Long> getAndAdd(long delta) {
return commandExecutor.evalWriteReactive(getName(),
StringCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local v = redis.call('get', KEYS[1]) or 0; "
+ "redis.call('set', KEYS[1], v + ARGV[1]); "
+ "return tonumber(v)",
Collections.<Object>singletonList(getName()), delta);
public Publisher<Long> getAndAdd(final long delta) {
return commandExecutor.writeReactive(getName(), StringCodec.INSTANCE, new RedisStrictCommand<Long>("INCRBY", new SingleConvertor<Long>() {
@Override
public Long convert(Object obj) {
return ((Long) obj) - delta;
}
}), getName(), delta);
}
@Override
public Publisher<Long> getAndSet(long newValue) {
return commandExecutor.evalWriteReactive(getName(),
StringCodec.INSTANCE, RedisCommands.EVAL_LONG,
"local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], ARGV[1]); return tonumber(v)",
Collections.<Object>singletonList(getName()), newValue);
return commandExecutor.writeReactive(getName(), LongCodec.INSTANCE, RedisCommands.GETSET, getName(), newValue);
}
@Override

Loading…
Cancel
Save