diff --git a/src/main/java/org/redisson/RedissonCache.java b/src/main/java/org/redisson/RedissonCache.java index f83f9c22d..30afddd0d 100644 --- a/src/main/java/org/redisson/RedissonCache.java +++ b/src/main/java/org/redisson/RedissonCache.java @@ -55,7 +55,6 @@ import io.netty.util.concurrent.Promise; * @param key * @param value */ -// TODO override expire methods public class RedissonCache extends RedissonMap implements RCache { private static final RedisCommand> EVAL_HSCAN = new RedisCommand>("EVAL", new NestedMultiDecoder(new ObjectMapReplayDecoder(), new MapScanResultReplayDecoder()), ValueType.MAP); @@ -337,4 +336,28 @@ public class RedissonCache extends RedissonMap implements RCache expireAsync(long timeToLive, TimeUnit timeUnit) { + return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, + "redis.call('pexpire', KEYS[2], ARGV[1]); " + + "return redis.call('pexpire', KEYS[1], ARGV[1]); ", + Arrays.asList(getName(), getTimeoutSetName()), timeUnit.toSeconds(timeToLive)); + } + + @Override + public Future expireAtAsync(long timestamp) { + return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, + "redis.call('pexpireat', KEYS[2], ARGV[1]); " + + "return redis.call('pexpireat', KEYS[1], ARGV[1]); ", + Arrays.asList(getName(), getTimeoutSetName()), timestamp); + } + + @Override + public Future clearExpireAsync() { + return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, + "redis.call('persist', KEYS[2]); " + + "return redis.call('persist', KEYS[1]); ", + Arrays.asList(getName(), getTimeoutSetName())); + } + } diff --git a/src/test/java/org/redisson/RedissonCacheTest.java b/src/test/java/org/redisson/RedissonCacheTest.java index be407a7a7..f6e21d5d4 100644 --- a/src/test/java/org/redisson/RedissonCacheTest.java +++ b/src/test/java/org/redisson/RedissonCacheTest.java @@ -265,6 +265,44 @@ public class RedissonCacheTest extends BaseTest { MatcherAssert.assertThat(cache.keySet(), Matchers.contains("0", "2", "3")); } + @Test + public void testExpire() throws InterruptedException { + RCache cache = redisson.getCache("simple"); + cache.put("0", "8"); + + cache.expire(100, TimeUnit.MILLISECONDS); + + Thread.sleep(500); + + Assert.assertEquals(0, cache.size()); + } + + @Test + public void testExpireAt() throws InterruptedException { + RCache cache = redisson.getCache("simple"); + cache.put("0", "8"); + + cache.expireAt(System.currentTimeMillis() + 100); + + Thread.sleep(500); + + Assert.assertEquals(0, cache.size()); + } + + @Test + public void testClearExpire() throws InterruptedException { + RCache cache = redisson.getCache("simple"); + cache.put("0", "8"); + + cache.expireAt(System.currentTimeMillis() + 100); + + cache.clearExpire(); + + Thread.sleep(500); + + Assert.assertEquals(1, cache.size()); + } + @Test public void testEntrySet() throws InterruptedException { RCache map = redisson.getCache("simple12");