RCache expire methods adopted. #195

pull/337/head
Nikita 9 years ago
parent f89653c01c
commit 59ee030461

@ -55,7 +55,6 @@ import io.netty.util.concurrent.Promise;
* @param <K> key
* @param <V> value
*/
// TODO override expire methods
public class RedissonCache<K, V> extends RedissonMap<K, V> implements RCache<K, V> {
private static final RedisCommand<MapScanResult<Object, Object>> EVAL_HSCAN = new RedisCommand<MapScanResult<Object, Object>>("EVAL", new NestedMultiDecoder(new ObjectMapReplayDecoder(), new MapScanResultReplayDecoder()), ValueType.MAP);
@ -337,4 +336,28 @@ public class RedissonCache<K, V> extends RedissonMap<K, V> implements RCache<K,
return commandExecutor.writeAsync(getName(), RedisCommands.DEL_SINGLE, getName(), getTimeoutSetName());
}
@Override
public Future<Boolean> 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.<Object>asList(getName(), getTimeoutSetName()), timeUnit.toSeconds(timeToLive));
}
@Override
public Future<Boolean> 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.<Object>asList(getName(), getTimeoutSetName()), timestamp);
}
@Override
public Future<Boolean> clearExpireAsync() {
return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
"redis.call('persist', KEYS[2]); "
+ "return redis.call('persist', KEYS[1]); ",
Arrays.<Object>asList(getName(), getTimeoutSetName()));
}
}

@ -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<String, String> 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<String, String> 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<String, String> 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<Integer, String> map = redisson.getCache("simple12");

Loading…
Cancel
Save