From 097eb32cd6dffdce4af99c60e9f276df23c828b1 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 12 Sep 2017 11:02:42 +0300 Subject: [PATCH] RMapCache.setMaxSize method added. #1027 --- .../main/java/org/redisson/RedissonMap.java | 2 +- .../java/org/redisson/RedissonMapCache.java | 48 +++++++++++-------- .../java/org/redisson/api/RKeysAsync.java | 2 +- .../main/java/org/redisson/api/RMapCache.java | 11 ++++- .../java/org/redisson/api/RMapCacheAsync.java | 12 ++++- .../client/protocol/RedisCommands.java | 5 +- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonMap.java b/redisson/src/main/java/org/redisson/RedissonMap.java index 7c4f7fd22..b8b52fa70 100644 --- a/redisson/src/main/java/org/redisson/RedissonMap.java +++ b/redisson/src/main/java/org/redisson/RedissonMap.java @@ -849,7 +849,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { } protected RFuture fastPutOperationAsync(K key, V value) { - return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSET, getName(key), key, value); + return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSET, getName(key), encodeMapKey(key), encodeMapValue(value)); } @Override diff --git a/redisson/src/main/java/org/redisson/RedissonMapCache.java b/redisson/src/main/java/org/redisson/RedissonMapCache.java index 6ac3a9faa..09752207a 100644 --- a/redisson/src/main/java/org/redisson/RedissonMapCache.java +++ b/redisson/src/main/java/org/redisson/RedissonMapCache.java @@ -106,16 +106,24 @@ public class RedissonMapCache extends RedissonMap implements RMapCac throw new IllegalArgumentException("maxSize should be greater than zero"); } - return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN, - "local value = redis.call('hget', KEYS[1], 'max-size'); " + - "if (value == false) then " - + "redis.call('hset', KEYS[1], 'max-size', ARGV[1]); " - + "return 1;" - + "end;" - + "return 0;", - Arrays.asList(getOptionsName()), maxSize); + return commandExecutor.writeAsync(getName(), codec, RedisCommands.HSETNX, getOptionsName(), "max-size", maxSize); + } + + @Override + public void setMaxSize(int permits) { + get(setMaxSizeAsync(permits)); } + @Override + public RFuture setMaxSizeAsync(int maxSize) { + if (maxSize <= 0) { + throw new IllegalArgumentException("maxSize should be greater than zero"); + } + + return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.HSET_VOID, getOptionsName(), "max-size", maxSize); + } + + @Override public RFuture containsKeyAsync(Object key) { checkKey(key); @@ -515,8 +523,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac " if maxSize ~= nil and maxSize ~= 0 then" + " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]);" + " local cacheSize = tonumber(redis.call('hlen', KEYS[1]));" + - " if cacheSize >= maxSize then" + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize);" + + " if cacheSize > maxSize then" + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1);" + " for index, lruItem in ipairs(lruItems) do" + " if lruItem then" + " local lruItemValue = redis.call('hget', KEYS[1], lruItem);" + @@ -584,8 +592,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac + "if maxSize ~= nil and maxSize ~= 0 then " + " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + " local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + - " if cacheSize >= maxSize then " + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize); " + + " if cacheSize > maxSize then " + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + " for index, lruItem in ipairs(lruItems) do " + " if lruItem then " + " local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + @@ -662,8 +670,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac " local lastAccessTimeSetName = KEYS[6]; " + " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + " local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + - " if cacheSize >= maxSize then " + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize); " + + " if cacheSize > maxSize then " + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + " for index, lruItem in ipairs(lruItems) do " + " if lruItem then " + " local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + @@ -1290,8 +1298,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac " local lastAccessTimeSetName = KEYS[6]; " + " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + " local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + - " if cacheSize >= maxSize then " + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize); " + + " if cacheSize > maxSize then " + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + " for index, lruItem in ipairs(lruItems) do " + " if lruItem then " + " local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + @@ -1340,8 +1348,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac "if maxSize ~= nil and maxSize ~= 0 then " + " redis.call('zadd', lastAccessTimeSetName, currentTime, ARGV[2]); " + " local cacheSize = tonumber(redis.call('hlen', KEYS[1])); " + - " if cacheSize >= maxSize then " + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize); " + + " if cacheSize > maxSize then " + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1); " + " for index, lruItem in ipairs(lruItems) do " + " if lruItem then " + " local lruItemValue = redis.call('hget', KEYS[1], lruItem); " + @@ -1647,8 +1655,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac " if maxSize ~= nil and maxSize ~= 0 then" + " redis.call('zadd', lastAccessTimeSetName, currentTime, key);" + " local cacheSize = tonumber(redis.call('hlen', KEYS[1]));" + - " if cacheSize >= maxSize then" + - " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize);" + + " if cacheSize > maxSize then" + + " local lruItems = redis.call('zrange', lastAccessTimeSetName, 0, cacheSize - maxSize - 1);" + " for index, lruItem in ipairs(lruItems) do" + " if lruItem then" + " local lruItemValue = redis.call('hget', KEYS[1], lruItem);" + diff --git a/redisson/src/main/java/org/redisson/api/RKeysAsync.java b/redisson/src/main/java/org/redisson/api/RKeysAsync.java index 65343be43..d22b8ab74 100644 --- a/redisson/src/main/java/org/redisson/api/RKeysAsync.java +++ b/redisson/src/main/java/org/redisson/api/RKeysAsync.java @@ -194,7 +194,7 @@ public interface RKeysAsync { *

* Requires Redis 4.0+ * - * @param keys + * @param keys - object names * @return number of removed keys */ RFuture unlinkAsync(String ... keys); diff --git a/redisson/src/main/java/org/redisson/api/RMapCache.java b/redisson/src/main/java/org/redisson/api/RMapCache.java index f84c05016..9286d9a38 100644 --- a/redisson/src/main/java/org/redisson/api/RMapCache.java +++ b/redisson/src/main/java/org/redisson/api/RMapCache.java @@ -40,7 +40,16 @@ import org.redisson.api.map.event.MapEntryListener; public interface RMapCache extends RMap, RMapCacheAsync { /** - * Tries to set max size of the map. + * Sets max size of the map. + * Superfluous elements are evicted using LRU algorithm. + * + * @param maxSize - max size + */ + void setMaxSize(int maxSize); + + /** + * Tries to set max size of the map. + * Superfluous elements are evicted using LRU algorithm. * * @param maxSize - max size * @return true if max size has been successfully set, otherwise false. diff --git a/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java b/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java index cbbc74781..cc4a716bf 100644 --- a/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java +++ b/redisson/src/main/java/org/redisson/api/RMapCacheAsync.java @@ -38,7 +38,17 @@ import java.util.concurrent.TimeUnit; public interface RMapCacheAsync extends RMapAsync { /** - * Tries to set max size of the map. + * Sets max size of the map. + * Superfluous elements are evicted using LRU algorithm. + * + * @param maxSize - max size + * @return void + */ + RFuture setMaxSizeAsync(int maxSize); + + /** + * Tries to set max size of the map. + * Superfluous elements are evicted using LRU algorithm. * * @param maxSize - max size * @return true if max size has been successfully set, otherwise false. diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index 6e50d598b..56fd6bb35 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -254,8 +254,9 @@ public interface RedisCommands { RedisStrictCommand MSET = new RedisStrictCommand("MSET", new VoidReplayConvertor()); RedisStrictCommand MSETNX = new RedisStrictCommand("MSETNX", new BooleanReplayConvertor()); - RedisCommand HSETNX = new RedisCommand("HSETNX", new BooleanReplayConvertor()); - RedisCommand HSET = new RedisCommand("HSET", new BooleanReplayConvertor(), 2, ValueType.MAP); + RedisStrictCommand HSETNX = new RedisStrictCommand("HSETNX", new BooleanReplayConvertor()); + RedisStrictCommand HSET = new RedisStrictCommand("HSET", new BooleanReplayConvertor()); + RedisStrictCommand HSET_VOID = new RedisStrictCommand("HSET", new VoidReplayConvertor()); RedisCommand> HSCAN = new RedisCommand>("HSCAN", new NestedMultiDecoder(new ObjectMapReplayDecoder(), new MapScanResultReplayDecoder()), ValueType.MAP); RedisCommand> HGETALL = new RedisCommand>("HGETALL", new ObjectMapReplayDecoder(), ValueType.MAP); RedisCommand>> HGETALL_ENTRY = new RedisCommand>>("HGETALL", new ObjectMapEntryReplayDecoder(), ValueType.MAP);