RMapCache.remainTimeToLive(key) method added. #1210

pull/1253/merge
Nikita 7 years ago
parent 6f32edceff
commit 27fb531c0c

@ -970,6 +970,47 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
System.currentTimeMillis(), ttlTimeout, maxIdleTimeout, maxIdleDelta, encodeMapKey(key), encodeMapValue(value)); System.currentTimeMillis(), ttlTimeout, maxIdleTimeout, maxIdleDelta, encodeMapKey(key), encodeMapValue(value));
return future; return future;
} }
@Override
public long remainTimeToLive(K key) {
return get(remainTimeToLiveAsync(key));
}
@Override
public RFuture<Long> remainTimeToLiveAsync(K key) {
checkKey(key);
return commandExecutor.evalWriteAsync(getName(key), codec, RedisCommands.EVAL_LONG,
"local value = redis.call('hget', KEYS[1], ARGV[2]); "
+ "if value == false then "
+ "return -2; "
+ "end; "
+ "local t, val = struct.unpack('dLc0', value); "
+ "local expireDate = 92233720368547758; " +
"local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); "
+ "if expireDateScore ~= false then "
+ "expireDate = tonumber(expireDateScore) "
+ "end; "
+ "if t ~= 0 then "
+ "local expireIdle = redis.call('zscore', KEYS[3], ARGV[2]); "
+ "if expireIdle ~= false then "
+ "expireDate = math.min(expireDate, tonumber(expireIdle)) "
+ "end; "
+ "end; "
+ "if expireDate == 92233720368547758 then "
+ "return -1; "
+ "end;"
+ "if expireDate > tonumber(ARGV[1]) then "
+ "return ARGV[1] - expireDate; "
+ "else "
+ "return -2; "
+ "end; "
+ "return val; ",
Arrays.<Object>asList(getName(key), getTimeoutSetNameByKey(key), getIdleSetNameByKey(key)),
System.currentTimeMillis(), encodeMapKey(key));
}
String getTimeoutSetNameByKey(Object key) { String getTimeoutSetNameByKey(Object key) {
return prefixName("redisson__timeout__set", getName(key)); return prefixName("redisson__timeout__set", getName(key));

@ -260,5 +260,14 @@ public interface RMapCache<K, V> extends RMap<K, V>, RMapCacheAsync<K, V> {
* @param listenerId - listener id * @param listenerId - listener id
*/ */
void removeListener(int listenerId); void removeListener(int listenerId);
/**
* Remaining time to live of map entry associated with a <code>key</code>.
*
* @return time in milliseconds
* -2 if the key does not exist.
* -1 if the key exists but has no associated expire.
*/
long remainTimeToLive(K key);
} }

@ -220,4 +220,14 @@ public interface RMapCacheAsync<K, V> extends RMapAsync<K, V> {
*/ */
@Override @Override
RFuture<Integer> sizeAsync(); RFuture<Integer> sizeAsync();
/**
* Remaining time to live of map entry associated with a <code>key</code>.
*
* @return time in milliseconds
* -2 if the key does not exist.
* -1 if the key exists but has no associated expire.
*/
RFuture<Long> remainTimeToLiveAsync(K key);
} }

@ -57,6 +57,19 @@ public class RedissonMapCacheTest extends BaseMapTest {
return redisson.getMapCache("test", options); return redisson.getMapCache("test", options);
} }
@Test
public void testRemainTimeToLive() {
RMapCache<String, String> map = redisson.getMapCache("test");
map.put("1", "2", 2, TimeUnit.SECONDS);
assertThat(map.remainTimeToLive("1")).isLessThan(1900);
map.put("3", "4");
assertThat(map.remainTimeToLive("3")).isEqualTo(-1);
assertThat(map.remainTimeToLive("0")).isEqualTo(-2);
map.put("5", "6", 20, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
assertThat(map.remainTimeToLive("1")).isLessThan(9900);
}
@Test @Test
public void testWriterPutIfAbsentTTL() { public void testWriterPutIfAbsentTTL() {
Map<String, String> store = new HashMap<>(); Map<String, String> store = new HashMap<>();

Loading…
Cancel
Save