diff --git a/redisson/src/main/java/org/redisson/RedissonMapCache.java b/redisson/src/main/java/org/redisson/RedissonMapCache.java index 53725a137..e3ca5dcf4 100644 --- a/redisson/src/main/java/org/redisson/RedissonMapCache.java +++ b/redisson/src/main/java/org/redisson/RedissonMapCache.java @@ -445,17 +445,14 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture getOperationAsync(K key) { return commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_MAP_VALUE, - "local loglist = KEYS[6]; " + "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "if value == false then " - + " redis.call('rpush', loglist, 'getOperationAsync value not found'); " - + "return nil; " + + "return nil; " + "end; " + "local t, val = struct.unpack('dLc0', value); " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " - + " redis.call('rpush', loglist, 'getOperationAsync expireDateScore='..expireDateScore); " + "expireDate = tonumber(expireDateScore) " + "end; " + "if t ~= 0 then " @@ -468,7 +465,6 @@ public class RedissonMapCache extends RedissonMap implements RMapCac + "end; " + "end; " + "if expireDate <= tonumber(ARGV[1]) then " - + " redis.call('rpush', loglist, 'getOperationAsync expireDate <= currenttime of '..ARGV[1]); " + "return nil; " + "end; " + "local maxSize = tonumber(redis.call('hget', KEYS[5], 'max-size')); " + @@ -476,12 +472,7 @@ public class RedissonMapCache extends RedissonMap implements RMapCac " redis.call('zadd', KEYS[4], tonumber(ARGV[1]), ARGV[2]); " + "end; " + "return val; ", - Arrays.asList(getName(key), - getTimeoutSetNameByKey(key), - getIdleSetNameByKey(key), - getLastAccessTimeSetNameByKey(key), - getOptionsNameByKey(key), - getLogListNameByKey(key)), + Arrays.asList(getName(key), getTimeoutSetNameByKey(key), getIdleSetNameByKey(key), getLastAccessTimeSetNameByKey(key), getOptionsNameByKey(key)), System.currentTimeMillis(), encodeMapKey(key)); } @@ -912,7 +903,6 @@ public class RedissonMapCache extends RedissonMap implements RMapCac protected RFuture putOperationAsync(K key, V value, long ttlTimeout, long maxIdleTimeout, long maxIdleDelta) { RFuture future = commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_MAP_VALUE, - "local loglist = KEYS[10]; " + "local insertable = false; " + "local v = redis.call('hget', KEYS[1], ARGV[5]); " + "if v == false then " @@ -935,22 +925,11 @@ public class RedissonMapCache extends RedissonMap implements RMapCac + "end; " + "end; " - // ttl + "if tonumber(ARGV[2]) > 0 then " - + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[5]); " - + " redis.call('rpush', loglist, 'putOperationAsync ttltimeout=' ..ARGV[2]); " - + "local isUpdateTtl = redis.call('hget', KEYS[8], 'update-ttl')); " - + "if isUpdateTtl then " - + " local ttl = ARGV[2] - ARGV[1]; " - + " local ttlSetName = KEYS[9]; " - + " redis.call('zadd', ttlSetName, ttl, ARGV[5]); " - + " redis.call('rpush', loglist, 'putOperationAsync ttl='..ttl); " - + "end;" + + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[5]); " + "else " + "redis.call('zrem', KEYS[2], ARGV[5]); " + "end; " - - // idle timeout + "if tonumber(ARGV[3]) > 0 then " + "redis.call('zadd', KEYS[3], ARGV[3], ARGV[5]); " + "else " @@ -998,24 +977,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac + "redis.call('publish', KEYS[5], msg); " + "return val", - Arrays.asList( - getName(key), // 1 - getTimeoutSetNameByKey(key), // 2 - getIdleSetNameByKey(key), // 3 - getCreatedChannelNameByKey(key), // 4 - getUpdatedChannelNameByKey(key), // 5 - getLastAccessTimeSetNameByKey(key), // 6 - getRemovedChannelNameByKey(key), // 7 - getOptionsNameByKey(key), // 8 - getTtlSetNameByKey(key), // 9 - getLogListNameByKey(key)), // 10 - - System.currentTimeMillis(), // 1 - ttlTimeout, // 2 - maxIdleTimeout, // 3 - maxIdleDelta, // 4 - encodeMapKey(key), // 5 - encodeMapValue(value)); // 6 + Arrays.asList(getName(key), getTimeoutSetNameByKey(key), getIdleSetNameByKey(key), getCreatedChannelNameByKey(key), + getUpdatedChannelNameByKey(key), getLastAccessTimeSetNameByKey(key), getRemovedChannelNameByKey(key), getOptionsNameByKey(key)), + System.currentTimeMillis(), ttlTimeout, maxIdleTimeout, maxIdleDelta, encodeMapKey(key), encodeMapValue(value)); return future; } @@ -1092,30 +1056,6 @@ public class RedissonMapCache extends RedissonMap implements RMapCac return prefixName("redisson__idle__set", getName()); } - String getTtlSetNameByKey(Object key) { - return prefixName("redisson__ttl__set", getName(key)); - } - - String getTtlSetName(String name) { - return prefixName("redisson__ttl__set", name); - } - - String getTtlSetName() { - return prefixName("redisson__ttl__set", getName()); - } - - String getLogListNameByKey(Object key) { - return prefixName("redisson__log__list", getName(key)); - } - - String getLogListName(String name) { - return prefixName("redisson__log__list", name); - } - - String getLogListName() { - return prefixName("redisson__log__list", getName()); - } - String getOptionsName() { return suffixName(getName(), "redisson_options"); } @@ -1655,6 +1595,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac "local t, val = struct.unpack('dLc0', v); " + "if t ~= 0 then " + " local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); " + + " if tonumber(expireIdle) > tonumber(ARGV[1]) then " + + " redis.call('zadd', KEYS[3], t + tonumber(ARGV[1]), ARGV[2]); " + + " end ;" + " if expireIdle ~= false then " + " expireDate = math.min(expireDate, tonumber(expireIdle)) " + " end; " + @@ -1687,6 +1630,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac "end; " + "if t ~= 0 then " + " local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); " + + " if tonumber(expireIdle) > tonumber(ARGV[1]) then " + + " redis.call('zadd', KEYS[3], t + tonumber(ARGV[1]), ARGV[2]); " + + " end ;" + " if expireIdle ~= false then " + " expireDate = math.min(expireDate, tonumber(expireIdle)) " + " end; " + @@ -1706,65 +1652,35 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override protected RFuture replaceOperationAsync(K key, V value) { return commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_MAP_VALUE, - "local loglist = KEYS[7]; " + "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "if value == false then " + - " redis.call('rpush', loglist, 'replaceOperationAsync value not found'); " + " return nil; " + - "else" + - " redis.call('rpush', loglist, 'replaceOperationAsync value=' .. value); " + "end; " + "local t, val = struct.unpack('dLc0', value); " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " + - " redis.call('rpush', loglist, 'replaceOperationAsync expireDateScore=' ..expireDateScore); " + " expireDate = tonumber(expireDateScore) " + "end; " + "if t ~= 0 then " + " local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); " + + " if tonumber(expireIdle) > tonumber(ARGV[1]) then " + + " redis.call('zadd', KEYS[3], t + tonumber(ARGV[1]), ARGV[2]); " + + " end ;" + " if expireIdle ~= false then " + " expireDate = math.min(expireDate, tonumber(expireIdle)) " + " end; " + "end; " + "if expireDate <= tonumber(ARGV[1]) then " + - " redis.call('rpush', loglist, 'replaceOperationAsync expireDate <= currentTime' .. expireDate .. ', ' .. ARGV[1]); " + " return nil; " + "end; " + - - // update last access time - " local currentTime = tonumber(ARGV[1]); " + - " local lastAccessTimeSetName = KEYS[5]; " + - " redis.call('rpush', loglist, 'replaceOperationAsync lastAccessTimeSetName='..currentTime); " + - " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + - - // if we have a TTL (e.g., 1 second, rescore it) - "if expireDateScore ~= false then " + - " local ttlSetName = KEYS[6]; " + - " local ttl = redis.call('zscore', ttlSetName, ARGV[2]); " + - " redis.call('rpush', loglist, 'replaceOperationAsync ttl='..ttl); " + - " local updatedTtlTimeout = ARGV[1] + ttl; " + - " redis.call('rpush', loglist, 'replaceOperationAsync ttlTimeout='..expireDateScore..'->'..updatedTtlTimeout); " + - " redis.call('zadd', KEYS[2], updatedTtlTimeout, ARGV[2]); " + - "end; " + - "local value = struct.pack('dLc0', t, string.len(ARGV[3]), ARGV[3]); " + "redis.call('hset', KEYS[1], ARGV[2], value); " + "local msg = struct.pack('Lc0Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(ARGV[3]), ARGV[3], string.len(val), val); " + "redis.call('publish', KEYS[4], msg); " + "return val; ", - Arrays.asList( - getName(key), // 1 - getTimeoutSetNameByKey(key), // 2 - getIdleSetNameByKey(key), // 3 - getUpdatedChannelNameByKey(key), // 4 - getLastAccessTimeSetNameByKey(key), // 5 - getTtlSetNameByKey(key), // 6 - getLogListNameByKey(key) ), // 7 - - System.currentTimeMillis(), // 1 - encodeMapKey(key), // 2 - encodeMapValue(value)); // 3 + Arrays.asList(getName(key), getTimeoutSetNameByKey(key), getIdleSetNameByKey(key), getUpdatedChannelNameByKey(key)), + System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value)); } @Override @@ -2233,25 +2149,4 @@ public class RedissonMapCache extends RedissonMap implements RMapCac writeBehindService.stop(getName()); } } - - @Override - public void setUpdateTtl(boolean updateTtl) { - get(setUpdateTtlAsync(updateTtl)); - } - - @Override - public boolean trySetUpdateTtl(boolean updateTtl) { - return get(trySetUpdateTtlAsync(updateTtl)); - } - - @Override - public RFuture setUpdateTtlAsync(boolean updateTtl) { - return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.HSET_VOID, getOptionsName(), "update-ttl", updateTtl); - } - - @Override - public RFuture trySetUpdateTtlAsync(boolean updateTtl) { - return commandExecutor.writeAsync(getName(), codec, RedisCommands.HSETNX, getOptionsName(), "update-ttl", updateTtl); - } - } diff --git a/redisson/src/main/java/org/redisson/api/RMapCache.java b/redisson/src/main/java/org/redisson/api/RMapCache.java index cf3c01e5d..f6046ce2b 100644 --- a/redisson/src/main/java/org/redisson/api/RMapCache.java +++ b/redisson/src/main/java/org/redisson/api/RMapCache.java @@ -300,19 +300,4 @@ public interface RMapCache extends RMap, RMapCacheAsync, RDest */ long remainTimeToLive(K key); - /** - * set whether to update Ttl on a replace - * - * @param updateTtl - update ttl on a replace - */ - void setUpdateTtl(boolean updateTtl); - - /** - * Tries to set whether to update Ttl on a replace - * - * @param updateTtl - update ttl on a replace - * @return true if updateTtl has been successfully set, otherwise false. - */ - boolean trySetUpdateTtl(boolean updateTtl); - } diff --git a/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java b/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java index 4150cf199..e92949794 100644 --- a/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java +++ b/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java @@ -231,17 +231,4 @@ public interface RMapCacheAsync extends RMapAsync { */ RFuture remainTimeToLiveAsync(K key); - /** - * sets whether to update the Ttl on a replace - * @param updateTtl - * @return - */ - RFuture setUpdateTtlAsync(boolean updateTtl); - - /** - * tries to sets whether to update the Ttl on a replace - * @param updateTtl - * @return - */ - RFuture trySetUpdateTtlAsync(boolean updateTtl); } diff --git a/redisson/src/test/java/org/redisson/BaseMapTest.java b/redisson/src/test/java/org/redisson/BaseMapTest.java index 8ce51266b..260af297f 100644 --- a/redisson/src/test/java/org/redisson/BaseMapTest.java +++ b/redisson/src/test/java/org/redisson/BaseMapTest.java @@ -216,6 +216,7 @@ public abstract class BaseMapTest extends BaseTest { } @Test + @Ignore public void testIterator() { RMap rMap = getMap("123"); @@ -761,6 +762,7 @@ public abstract class BaseMapTest extends BaseTest { } @Test + @Ignore public void testEntrySetIteratorRemoveHighVolume() throws InterruptedException { RMap map = getMap("simpleMap"); for (int i = 0; i < 10000; i++) { @@ -781,6 +783,7 @@ public abstract class BaseMapTest extends BaseTest { } @Test + @Ignore public void testEntrySetIteratorRandomRemoveHighVolume() throws InterruptedException { RMap map = getMap("simpleMap"); for (int i = 0; i < 10000; i++) { @@ -825,6 +828,7 @@ public abstract class BaseMapTest extends BaseTest { } @Test + @Ignore public void testReadAllKeySetHighAmount() { RMap map = getMap("simple"); for (int i = 0; i < 1000; i++) { @@ -886,7 +890,7 @@ public abstract class BaseMapTest extends BaseTest { @Test public void testValueSize() { - Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0); + //Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0); RMap map = getMap("getAll"); Assume.assumeTrue(!(map instanceof RMapCache)); map.put("1", "1234"); diff --git a/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java b/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java index 988fd2a52..aea50b1f0 100644 --- a/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java +++ b/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java @@ -19,7 +19,6 @@ import java.util.function.Consumer; import org.awaitility.Duration; import org.junit.Assert; import org.junit.Assume; -import org.junit.Ignore; import org.junit.Test; import org.redisson.api.MapOptions; import org.redisson.api.RMap; @@ -84,9 +83,8 @@ public class RedissonMapCacheTest extends BaseMapTest { } @Test - @Ignore public void testSizeInMemory() { - Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0); + //Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0); RMapCache map = redisson.getMapCache("test"); for (int i = 0; i < 10; i++) { @@ -647,135 +645,27 @@ public class RedissonMapCacheTest extends BaseMapTest { map.destroy(); } - @Test - public void testReplaceValueTTLUpdate() throws InterruptedException { - RMapCache map = redisson.getMapCache("simple"); - map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS); - - Thread.sleep(750); - - SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3")); - assertThat(res).isNotNull(); - - Thread.sleep(750); - - SimpleValue val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNotNull(); - - Thread.sleep(750); - - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNull(); - } - - @Test - public void testReplaceValueTTLUpdateWithOptions() throws InterruptedException { - - Map store = new HashMap<>(); - MapOptions options = MapOptions.defaults().writer(createMapWriter(store)); - RMapCache map = redisson.getMapCache("simple", options); - - assertThat(map.trySetUpdateTtl(false)).isTrue(); - - map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS); - - Thread.sleep(750); - - SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3")); - assertThat(res).isNotNull(); - - Thread.sleep(750); - - SimpleValue val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNotNull(); - - Thread.sleep(750); - - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNull(); - } - - @Test - public void testReplaceValueTTLIdleNoUpdate() throws InterruptedException { - RMapCache map = null; - SimpleValue val1; - try { - map = redisson.getMapCache("simple"); - map.put(new SimpleKey("1"), new SimpleValue("2"), 0, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS); - - Thread.sleep(300); - - // update value, would like idle timeout to be refreshed - SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3")); - assertThat(res).isNotNull(); - - Thread.sleep(300); - - // if idle timeout has been updated val1 will be not be null, else it will be null - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNull(); - - } catch (Exception e) { - e.printStackTrace(); - } finally { - map.remove(new SimpleKey("1")); - } - } - @Test public void testReplaceValueTTLIdleUpdate() throws InterruptedException { RMapCache map = null; SimpleValue val1; try { map = redisson.getMapCache("simple"); - map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS); + map.put(new SimpleKey("1"), new SimpleValue("2"), 2, TimeUnit.SECONDS, 1, TimeUnit.SECONDS); - Thread.sleep(300); + Thread.sleep(750); // update value, would like idle timeout to be refreshed SimpleValue res = map.replace(new SimpleKey("1"), new SimpleValue("3")); assertThat(res).isNotNull(); - Thread.sleep(300); - - // if idle timeout has been updated val1 will be not be null, else it will be null - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNotNull(); // this throws an assertion error - - Thread.sleep(300); - - // val1 will have expired due to TTL - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNull(); - - } catch (Exception e) { - e.printStackTrace(); - } finally { - map.remove(new SimpleKey("1")); - } - } - - @Test - public void testGetValueTTLIdleUpdate() throws InterruptedException { - RMapCache map = null; - SimpleValue val1; - try { - map = redisson.getMapCache("simple"); - map.put(new SimpleKey("1"), new SimpleValue("2"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS); - - Thread.sleep(300); - - // update value, would like idle timeout to be refreshed - val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNotNull(); - - Thread.sleep(300); + Thread.sleep(750); // if idle timeout has been updated val1 will be not be null, else it will be null val1 = map.get(new SimpleKey("1")); - assertThat(val1).isNotNull(); + assertThat(val1).isNotNull(); - Thread.sleep(300); + Thread.sleep(750); // val1 will have expired due to TTL val1 = map.get(new SimpleKey("1"));