diff --git a/redisson/src/main/java/org/redisson/RedissonMapCache.java b/redisson/src/main/java/org/redisson/RedissonMapCache.java index 74acf6d48..e3ca5dcf4 100644 --- a/redisson/src/main/java/org/redisson/RedissonMapCache.java +++ b/redisson/src/main/java/org/redisson/RedissonMapCache.java @@ -1595,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; " + @@ -1627,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; " + @@ -1658,6 +1664,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; " + diff --git a/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java b/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java index 793140a04..0846ce820 100644 --- a/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java +++ b/redisson/src/test/java/org/redisson/RedissonMapCacheTest.java @@ -644,6 +644,39 @@ public class RedissonMapCacheTest extends BaseMapTest { map.destroy(); } + @Test + public void testReplaceValueTTLIdleUpdate() throws InterruptedException { + RMapCache map = null; + SimpleValue val1; + try { + map = redisson.getMapCache("simple"); + map.put(new SimpleKey("1"), new SimpleValue("2"), 2, TimeUnit.SECONDS, 1, TimeUnit.SECONDS); + + 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(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(); + + Thread.sleep(750); + + // 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 testScheduler() throws InterruptedException { RMapCache map = redisson.getMapCache("simple3");