Skip to content

Commit

Permalink
RCache.containsValueAsync optimization. #195
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Dec 7, 2015
1 parent 0693c3b commit e4cf818
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/main/java/org/redisson/RedissonCache.java
Expand Up @@ -36,7 +36,6 @@
import org.redisson.client.protocol.decoder.MapScanResult; import org.redisson.client.protocol.decoder.MapScanResult;
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder; import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder; import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectSetReplayDecoder;
import org.redisson.client.protocol.decoder.TTLMapValueReplayDecoder; import org.redisson.client.protocol.decoder.TTLMapValueReplayDecoder;
import org.redisson.command.CommandAsyncExecutor; import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.decoder.MapGetAllDecoder; import org.redisson.connection.decoder.MapGetAllDecoder;
Expand Down Expand Up @@ -64,7 +63,8 @@ public class RedissonCache<K, V> extends RedissonMap<K, V> implements RCache<K,
private static final RedisCommand<Object> EVAL_PUT = EVAL_REPLACE; private static final RedisCommand<Object> EVAL_PUT = EVAL_REPLACE;
private static final RedisCommand<Object> EVAL_PUT_TTL = new RedisCommand<Object>("EVAL", 6, ValueType.MAP, ValueType.MAP_VALUE); private static final RedisCommand<Object> EVAL_PUT_TTL = new RedisCommand<Object>("EVAL", 6, ValueType.MAP, ValueType.MAP_VALUE);
private static final RedisCommand<List<Object>> EVAL_GET_TTL = new RedisCommand<List<Object>>("EVAL", new TTLMapValueReplayDecoder<Object>(), 5, ValueType.MAP_KEY, ValueType.MAP_VALUE); private static final RedisCommand<List<Object>> EVAL_GET_TTL = new RedisCommand<List<Object>>("EVAL", new TTLMapValueReplayDecoder<Object>(), 5, ValueType.MAP_KEY, ValueType.MAP_VALUE);
private static final RedisCommand<List<Object>> EVAL_CONTAINS = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5); private static final RedisCommand<List<Object>> EVAL_CONTAINS_KEY = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_KEY);
private static final RedisCommand<List<Object>> EVAL_CONTAINS_VALUE = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_VALUE);


private static final RedisCommand<Map<Object, Object>> EVAL_HGETALL = new RedisCommand<Map<Object, Object>>("EVAL", new ObjectMapReplayDecoder(), ValueType.MAP); private static final RedisCommand<Map<Object, Object>> EVAL_HGETALL = new RedisCommand<Map<Object, Object>>("EVAL", new ObjectMapReplayDecoder(), ValueType.MAP);
private static final RedisCommand<Long> EVAL_FAST_REMOVE = new RedisCommand<Long>("EVAL", 2, ValueType.MAP_KEY); private static final RedisCommand<Long> EVAL_FAST_REMOVE = new RedisCommand<Long>("EVAL", 2, ValueType.MAP_KEY);
Expand Down Expand Up @@ -102,14 +102,14 @@ public Future<Integer> sizeAsync() {
public Future<Boolean> containsKeyAsync(Object key) { public Future<Boolean> containsKeyAsync(Object key) {
Promise<Boolean> result = newPromise(); Promise<Boolean> result = newPromise();


Future<List<Object>> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS, Future<List<Object>> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS_KEY,
"local value = redis.call('hexists', KEYS[1], ARGV[1]); " + "local value = redis.call('hexists', KEYS[1], ARGV[1]); " +
"local expireDate = 92233720368547758; " + "local expireDate = 92233720368547758; " +
"if value == 1 then " + "if value == 1 then " +
"expireDate = redis.call('zscore', KEYS[2], ARGV[1]); " "expireDate = redis.call('zscore', KEYS[2], ARGV[1]); "
+ "if expireDate ~= false then " + "if expireDate ~= false then "
+ "expireDate = tonumber(expireDate) " + "expireDate = tonumber(expireDate) "
+ "end; " + + "end; " +
"end;" + "end;" +
"return {expireDate, value}; ", "return {expireDate, value}; ",
Arrays.<Object>asList(getName(), getTimeoutSetName()), key); Arrays.<Object>asList(getName(), getTimeoutSetName()), key);
Expand All @@ -121,25 +121,28 @@ public Future<Boolean> containsKeyAsync(Object key) {


@Override @Override
public Future<Boolean> containsValueAsync(Object value) { public Future<Boolean> containsValueAsync(Object value) {
return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 6), Promise<Boolean> result = newPromise();
"local expireSize = redis.call('zcard', KEYS[2])"
+ "local s = redis.call('hgetall', KEYS[1]);" + Future<List<Object>> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS_VALUE,
"local s = redis.call('hgetall', KEYS[1]);" +
"for i, v in ipairs(s) do " "for i, v in ipairs(s) do "
+ "if ARGV[2] == v and i % 2 == 0 then " + "if i % 2 == 0 and ARGV[1] == v then "
+ "if expireSize > 0 then " + "local key = s[i-1];"
+ "local key = s[i-1];" + "local expireDate = redis.call('zscore', KEYS[2], key); "
+ "local expireDate = redis.call('zscore', KEYS[2], key); " + "if expireDate == false then "
+ "if expireDate ~= false and expireDate <= ARGV[1] then " + "expireDate = 92233720368547758 "
+ "redis.call('zrem', KEYS[2], key); " + "else "
+ "redis.call('hdel', KEYS[1], key); " + "expireDate = tonumber(expireDate) "
+ "return false;" + "end; "
+ "end;" + "return {expireDate, 1}; "
+ "end;"
+ "return true "
+ "end " + "end "
+ "end;" + + "end;" +
"return false", "return {92233720368547758, 0};",
Arrays.<Object>asList(getName(), getTimeoutSetName()), System.currentTimeMillis(), value); Arrays.<Object>asList(getName(), getTimeoutSetName()), value);

addExpireListener(result, future, new BooleanReplayConvertor(), false);

return result;
} }


@Override @Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/redisson/client/protocol/RedisCommand.java
Expand Up @@ -177,6 +177,10 @@ public RedisCommand(String name, MultiDecoder<R> replayMultiDecoder, Convertor<R
this.convertor = convertor; this.convertor = convertor;
} }


public RedisCommand(String name, MultiDecoder<R> replayMultiDecoder, int objectParamIndex, ValueType inParamType) {
this(name, replayMultiDecoder, objectParamIndex, inParamType, null);
}

public RedisCommand(String name, MultiDecoder<R> replayMultiDecoder, int inParamIndex) { public RedisCommand(String name, MultiDecoder<R> replayMultiDecoder, int inParamIndex) {
this(name, null, replayMultiDecoder, null, inParamIndex); this(name, null, replayMultiDecoder, null, inParamIndex);
} }
Expand Down

0 comments on commit e4cf818

Please sign in to comment.