diff --git a/src/main/java/org/redisson/RedissonCache.java b/src/main/java/org/redisson/RedissonCache.java index 5106d0a69..592190e43 100644 --- a/src/main/java/org/redisson/RedissonCache.java +++ b/src/main/java/org/redisson/RedissonCache.java @@ -36,7 +36,6 @@ import org.redisson.client.protocol.convertor.LongReplayConvertor; import org.redisson.client.protocol.decoder.MapScanResult; import org.redisson.client.protocol.decoder.ObjectListReplayDecoder; import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder; -import org.redisson.client.protocol.decoder.ObjectSetReplayDecoder; import org.redisson.client.protocol.decoder.TTLMapValueReplayDecoder; import org.redisson.command.CommandAsyncExecutor; import org.redisson.connection.decoder.MapGetAllDecoder; @@ -64,7 +63,8 @@ public class RedissonCache extends RedissonMap implements RCache EVAL_PUT = EVAL_REPLACE; private static final RedisCommand EVAL_PUT_TTL = new RedisCommand("EVAL", 6, ValueType.MAP, ValueType.MAP_VALUE); private static final RedisCommand> EVAL_GET_TTL = new RedisCommand>("EVAL", new TTLMapValueReplayDecoder(), 5, ValueType.MAP_KEY, ValueType.MAP_VALUE); - private static final RedisCommand> EVAL_CONTAINS = new RedisCommand>("EVAL", new ObjectListReplayDecoder(), 5); + private static final RedisCommand> EVAL_CONTAINS_KEY = new RedisCommand>("EVAL", new ObjectListReplayDecoder(), 5, ValueType.MAP_KEY); + private static final RedisCommand> EVAL_CONTAINS_VALUE = new RedisCommand>("EVAL", new ObjectListReplayDecoder(), 5, ValueType.MAP_VALUE); private static final RedisCommand> EVAL_HGETALL = new RedisCommand>("EVAL", new ObjectMapReplayDecoder(), ValueType.MAP); private static final RedisCommand EVAL_FAST_REMOVE = new RedisCommand("EVAL", 2, ValueType.MAP_KEY); @@ -102,14 +102,14 @@ public class RedissonCache extends RedissonMap implements RCache containsKeyAsync(Object key) { Promise result = newPromise(); - Future> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS, + Future> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS_KEY, "local value = redis.call('hexists', KEYS[1], ARGV[1]); " + "local expireDate = 92233720368547758; " + "if value == 1 then " + "expireDate = redis.call('zscore', KEYS[2], ARGV[1]); " + "if expireDate ~= false then " + "expireDate = tonumber(expireDate) " - + "end; " + + + "end; " + "end;" + "return {expireDate, value}; ", Arrays.asList(getName(), getTimeoutSetName()), key); @@ -121,25 +121,28 @@ public class RedissonCache extends RedissonMap implements RCache containsValueAsync(Object value) { - return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand("EVAL", new BooleanReplayConvertor(), 6), - "local expireSize = redis.call('zcard', KEYS[2])" - + "local s = redis.call('hgetall', KEYS[1]);" + + Promise result = newPromise(); + + Future> future = commandExecutor.evalReadAsync(getName(), codec, EVAL_CONTAINS_VALUE, + "local s = redis.call('hgetall', KEYS[1]);" + "for i, v in ipairs(s) do " - + "if ARGV[2] == v and i % 2 == 0 then " - + "if expireSize > 0 then " - + "local key = s[i-1];" - + "local expireDate = redis.call('zscore', KEYS[2], key); " - + "if expireDate ~= false and expireDate <= ARGV[1] then " - + "redis.call('zrem', KEYS[2], key); " - + "redis.call('hdel', KEYS[1], key); " - + "return false;" - + "end;" - + "end;" - + "return true " + + "if i % 2 == 0 and ARGV[1] == v then " + + "local key = s[i-1];" + + "local expireDate = redis.call('zscore', KEYS[2], key); " + + "if expireDate == false then " + + "expireDate = 92233720368547758 " + + "else " + + "expireDate = tonumber(expireDate) " + + "end; " + + "return {expireDate, 1}; " + "end " + "end;" + - "return false", - Arrays.asList(getName(), getTimeoutSetName()), System.currentTimeMillis(), value); + "return {92233720368547758, 0};", + Arrays.asList(getName(), getTimeoutSetName()), value); + + addExpireListener(result, future, new BooleanReplayConvertor(), false); + + return result; } @Override diff --git a/src/main/java/org/redisson/client/protocol/RedisCommand.java b/src/main/java/org/redisson/client/protocol/RedisCommand.java index 15a1203e1..4dc0571a3 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommand.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommand.java @@ -177,6 +177,10 @@ public class RedisCommand { this.convertor = convertor; } + public RedisCommand(String name, MultiDecoder replayMultiDecoder, int objectParamIndex, ValueType inParamType) { + this(name, replayMultiDecoder, objectParamIndex, inParamType, null); + } + public RedisCommand(String name, MultiDecoder replayMultiDecoder, int inParamIndex) { this(name, null, replayMultiDecoder, null, inParamIndex); }