RMapCache.fastPut with TTL added

pull/365/head
Nikita 9 years ago
parent 6454ec28d4
commit 729046db88

@ -71,6 +71,7 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
private static final RedisCommand<Object> EVAL_REMOVE = new RedisCommand<Object>("EVAL", 4, ValueType.MAP_KEY, ValueType.MAP_VALUE); private static final RedisCommand<Object> EVAL_REMOVE = new RedisCommand<Object>("EVAL", 4, ValueType.MAP_KEY, ValueType.MAP_VALUE);
private static final RedisCommand<Long> EVAL_REMOVE_VALUE = new RedisCommand<Long>("EVAL", new LongReplayConvertor(), 5, ValueType.MAP); private static final RedisCommand<Long> EVAL_REMOVE_VALUE = new RedisCommand<Long>("EVAL", new LongReplayConvertor(), 5, ValueType.MAP);
private static final RedisCommand<Object> EVAL_PUT_TTL = new RedisCommand<Object>("EVAL", 6, ValueType.MAP, ValueType.MAP_VALUE); private static final RedisCommand<Object> EVAL_PUT_TTL = new RedisCommand<Object>("EVAL", 6, ValueType.MAP, ValueType.MAP_VALUE);
private static final RedisCommand<Boolean> EVAL_FAST_PUT_TTL = new RedisCommand<Boolean>("EVAL", new BooleanReplayConvertor(), 6, ValueType.MAP, ValueType.MAP_VALUE);
private static final RedisCommand<List<Object>> EVAL_GET_TTL = new RedisCommand<List<Object>>("EVAL", new TTLMapValueReplayDecoder<Object>(), 5, ValueType.MAP_KEY, ValueType.MAP_VALUE); private static final RedisCommand<List<Object>> EVAL_GET_TTL = new RedisCommand<List<Object>>("EVAL", new TTLMapValueReplayDecoder<Object>(), 5, ValueType.MAP_KEY, ValueType.MAP_VALUE);
private static final RedisCommand<List<Object>> EVAL_CONTAINS_KEY = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_KEY); private static final RedisCommand<List<Object>> EVAL_CONTAINS_KEY = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_KEY);
private static final RedisCommand<List<Object>> EVAL_CONTAINS_VALUE = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_VALUE); private static final RedisCommand<List<Object>> EVAL_CONTAINS_VALUE = new RedisCommand<List<Object>>("EVAL", new ObjectListReplayDecoder<Object>(), 5, ValueType.MAP_VALUE);
@ -279,6 +280,31 @@ public class RedissonMapCache<K, V> extends RedissonMap<K, V> implements RMapCac
return get(putAsync(key, value, ttl, unit)); return get(putAsync(key, value, ttl, unit));
} }
@Override
public boolean fastPut(K key, V value, long ttl, TimeUnit unit) {
return get(fastPutAsync(key, value, ttl, unit));
}
@Override
public Future<Boolean> fastPutAsync(K key, V value, long ttl, TimeUnit unit) {
if (ttl < 0) {
throw new IllegalArgumentException("TTL can't be negative");
}
if (ttl == 0) {
return fastPutAsync(key, value);
}
if (unit == null) {
throw new NullPointerException("TimeUnit param can't be null");
}
long timeoutDate = System.currentTimeMillis() + unit.toMillis(ttl);
return commandExecutor.evalWriteAsync(getName(), codec, EVAL_FAST_PUT_TTL,
"redis.call('zadd', KEYS[2], ARGV[1], ARGV[2]); " +
"return redis.call('hset', KEYS[1], ARGV[2], ARGV[3]); ",
Arrays.<Object>asList(getName(), getTimeoutSetName()), timeoutDate, key, value);
}
@Override @Override
public Future<V> putAsync(K key, V value, long ttl, TimeUnit unit) { public Future<V> putAsync(K key, V value, long ttl, TimeUnit unit) {
if (ttl < 0) { if (ttl < 0) {

@ -91,6 +91,13 @@ public class RedisCommand<R> {
this(name, null, null, null, inParamIndex); this(name, null, null, null, inParamIndex);
} }
public RedisCommand(String name, Convertor<R> convertor, int inParamIndex, ValueType inParamType, ValueType outParamType) {
this(name, null, null, null, inParamIndex);
this.convertor = convertor;
this.inParamType = Arrays.asList(inParamType);
this.outParamType = outParamType;
}
public RedisCommand(String name, int inParamIndex, ValueType inParamType, ValueType outParamType) { public RedisCommand(String name, int inParamIndex, ValueType inParamType, ValueType outParamType) {
this(name, null, null, null, inParamIndex); this(name, null, null, null, inParamIndex);
this.inParamType = Arrays.asList(inParamType); this.inParamType = Arrays.asList(inParamType);

@ -71,6 +71,8 @@ public interface RMapCache<K, V> extends RMap<K, V>, RMapCacheAsync<K, V> {
*/ */
V put(K key, V value, long ttl, TimeUnit unit); V put(K key, V value, long ttl, TimeUnit unit);
boolean fastPut(K key, V value, long ttl, TimeUnit unit);
/** /**
* Returns the number of entries in cache. * Returns the number of entries in cache.
* This number can reflects expired entries too * This number can reflects expired entries too

@ -73,6 +73,8 @@ public interface RMapCacheAsync<K, V> extends RMapAsync<K, V> {
*/ */
Future<V> putAsync(K key, V value, long ttl, TimeUnit unit); Future<V> putAsync(K key, V value, long ttl, TimeUnit unit);
Future<Boolean> fastPutAsync(K key, V value, long ttl, TimeUnit unit);
/** /**
* Returns the number of entries in cache. * Returns the number of entries in cache.
* This number can reflects expired entries too * This number can reflects expired entries too

@ -25,6 +25,7 @@ import org.redisson.core.RMapCache;
import org.redisson.core.RSetCache; import org.redisson.core.RSetCache;
import org.redisson.core.RMap; import org.redisson.core.RMap;
import io.netty.util.Timeout;
import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Future;
public class RedissonMapCacheTest extends BaseTest { public class RedissonMapCacheTest extends BaseTest {
@ -596,6 +597,14 @@ public class RedissonMapCacheTest extends BaseTest {
Assert.assertEquals(1, map.size()); Assert.assertEquals(1, map.size());
} }
@Test
public void testFastPutWithTTL() throws Exception {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
Assert.assertTrue(map.fastPut(1, 2, 2, TimeUnit.SECONDS));
Assert.assertFalse(map.fastPut(1, 2, 2, TimeUnit.SECONDS));
Assert.assertEquals(1, map.size());
}
@Test @Test
public void testEquals() { public void testEquals() {
RMapCache<String, String> map = redisson.getMapCache("simple"); RMapCache<String, String> map = redisson.getMapCache("simple");

Loading…
Cancel
Save