From f3eeab4b28b1b8c7064eb5f184e7cfbd410636b2 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 09:31:15 +0300 Subject: [PATCH 01/11] adding cacheclass to cacheconfig --- .../redis/clients/jedis/JedisCluster.java | 14 +-- .../java/redis/clients/jedis/JedisPooled.java | 10 +- .../redis/clients/jedis/JedisSentineled.java | 8 +- .../redis/clients/jedis/UnifiedJedis.java | 7 +- .../redis/clients/jedis/csc/CacheConfig.java | 14 ++- .../redis/clients/jedis/csc/CacheFactory.java | 58 ++++++++++ .../clients/jedis/csc/CacheProvider.java | 23 ---- .../redis/clients/jedis/csc/DefaultCache.java | 4 + .../jedis/benchmark/CSCPooleadBenchmark.java | 2 +- .../csc/AllowAndDenyListCacheableTest.java | 10 +- .../csc/ClientSideCacheFunctionalityTest.java | 106 +++++++++--------- .../csc/JedisClusterClientSideCacheTest.java | 3 +- .../JedisPooledClientSideCacheTestBase.java | 2 +- .../JedisSentineledClientSideCacheTest.java | 2 +- .../redis/clients/jedis/csc/TestCache.java | 9 +- 15 files changed, 159 insertions(+), 113 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/csc/CacheFactory.java delete mode 100644 src/main/java/redis/clients/jedis/csc/CacheProvider.java diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index ca1dddc0f6..414e7629c6 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -12,7 +12,7 @@ import redis.clients.jedis.providers.ClusterConnectionProvider; import redis.clients.jedis.csc.Cache; import redis.clients.jedis.csc.CacheConfig; -import redis.clients.jedis.csc.CacheProvider; +import redis.clients.jedis.csc.CacheFactory; import redis.clients.jedis.util.JedisClusterCRC16; public class JedisCluster extends UnifiedJedis { @@ -223,31 +223,31 @@ private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Durati @Experimental public JedisCluster(Set hnp, JedisClientConfig jedisClientConfig, CacheConfig cacheConfig) { - this(hnp, jedisClientConfig, new CacheProvider().getCache(cacheConfig)); + this(hnp, jedisClientConfig, new CacheFactory().getCache(cacheConfig)); } @Experimental - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache) { + protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache) { this(clusterNodes, clientConfig, clientSideCache, DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis())); } @Experimental - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } @Experimental - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } @Experimental - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis()), @@ -255,7 +255,7 @@ public JedisCluster(Set clusterNodes, JedisClientConfig clientConfi } @Experimental - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, GenericObjectPoolConfig poolConfig, Duration topologyRefreshPeriod, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig, topologyRefreshPeriod), diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index eff097c1be..d251c54981 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -10,7 +10,7 @@ import redis.clients.jedis.annots.Experimental; import redis.clients.jedis.csc.Cache; import redis.clients.jedis.csc.CacheConfig; -import redis.clients.jedis.csc.CacheProvider; +import redis.clients.jedis.csc.CacheFactory; import redis.clients.jedis.providers.PooledConnectionProvider; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.Pool; @@ -81,11 +81,11 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client @Experimental public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, CacheConfig cacheConfig) { - this(hostAndPort, clientConfig, new CacheProvider().getCache(cacheConfig)); + this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig)); } @Experimental - public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache) { + protected JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache) { super(hostAndPort, clientConfig, clientSideCache); } @@ -392,11 +392,11 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client @Experimental public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, CacheConfig cacheConfig, final GenericObjectPoolConfig poolConfig) { - this(hostAndPort, clientConfig, new CacheProvider().getCache(cacheConfig), poolConfig); + this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig), poolConfig); } @Experimental - public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache, + protected JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache, final GenericObjectPoolConfig poolConfig) { super(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache, poolConfig), clientConfig.getRedisProtocol(), clientSideCache); diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index 03fbdbb9d6..6c0d316f38 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -5,7 +5,7 @@ import redis.clients.jedis.annots.Experimental; import redis.clients.jedis.csc.Cache; import redis.clients.jedis.csc.CacheConfig; -import redis.clients.jedis.csc.CacheProvider; +import redis.clients.jedis.csc.CacheFactory; import redis.clients.jedis.providers.SentineledConnectionProvider; public class JedisSentineled extends UnifiedJedis { @@ -19,12 +19,12 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo @Experimental public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, CacheConfig cacheConfig, Set sentinels, final JedisClientConfig sentinelClientConfig) { - this(masterName, masterClientConfig, new CacheProvider().getCache(cacheConfig), + this(masterName, masterClientConfig, new CacheFactory().getCache(cacheConfig), sentinels, sentinelClientConfig); } @Experimental - public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, + protected JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, sentinels, sentinelClientConfig), masterClientConfig.getRedisProtocol(), clientSideCache); @@ -38,7 +38,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo } @Experimental - public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, + protected JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, final GenericObjectPoolConfig poolConfig, Set sentinels, final JedisClientConfig sentinelClientConfig) { super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, poolConfig, diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 706c61be1b..6528eb45fc 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -22,7 +22,7 @@ import redis.clients.jedis.csc.Cache; import redis.clients.jedis.csc.CacheConfig; import redis.clients.jedis.csc.CacheConnection; -import redis.clients.jedis.csc.CacheProvider; +import redis.clients.jedis.csc.CacheFactory; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.executors.*; import redis.clients.jedis.gears.TFunctionListParams; @@ -99,13 +99,14 @@ public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) { } @Experimental - public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig) { - this(hostAndPort, clientConfig, new CacheProvider().getCache(cacheConfig)); + protected UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig) { + this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig)); } @Experimental public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache cache) { this(new PooledConnectionProvider(hostAndPort, clientConfig, cache), clientConfig.getRedisProtocol(), cache); + } public UnifiedJedis(ConnectionProvider provider) { diff --git a/src/main/java/redis/clients/jedis/csc/CacheConfig.java b/src/main/java/redis/clients/jedis/csc/CacheConfig.java index 2c782a4a7c..ab907dfbde 100644 --- a/src/main/java/redis/clients/jedis/csc/CacheConfig.java +++ b/src/main/java/redis/clients/jedis/csc/CacheConfig.java @@ -5,6 +5,7 @@ public class CacheConfig { private int maxSize; private Cacheable cacheable; private EvictionPolicy evictionPolicy; + private Class cacheClass; public int getMaxSize() { return maxSize; @@ -18,14 +19,19 @@ public EvictionPolicy getEvictionPolicy() { return evictionPolicy; } + public Class getCacheClass() { + return cacheClass; + } public static Builder builder() { return new Builder(); } public static class Builder { - private int maxSize; + private final int DEFAULT_MAX_SIZE = 10000; + private int maxSize = DEFAULT_MAX_SIZE; private Cacheable cacheable = DefaultCacheable.INSTANCE; private EvictionPolicy evictionPolicy; + private Class cacheClass; public Builder maxSize(int maxSize) { this.maxSize = maxSize; @@ -42,11 +48,17 @@ public Builder cacheable(Cacheable cacheable) { return this; } + public Builder cacheClass(Class cacheClass) { + this.cacheClass = cacheClass; + return this; + } + public CacheConfig build() { CacheConfig cacheConfig = new CacheConfig(); cacheConfig.maxSize = this.maxSize; cacheConfig.cacheable = this.cacheable; cacheConfig.evictionPolicy = this.evictionPolicy; + cacheConfig.cacheClass = this.cacheClass; return cacheConfig; } } diff --git a/src/main/java/redis/clients/jedis/csc/CacheFactory.java b/src/main/java/redis/clients/jedis/csc/CacheFactory.java new file mode 100644 index 0000000000..db503dd662 --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/CacheFactory.java @@ -0,0 +1,58 @@ +package redis.clients.jedis.csc; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; + +import redis.clients.jedis.exceptions.JedisCacheException; + +public class CacheFactory { + + public Cache getCache(CacheConfig config) { + if (config.getCacheClass() == null) { + return new DefaultCache(config.getMaxSize(), config.getCacheable(), getEvictionPolicy(config)); + } + return instantiateCustomCache(config); + } + + private Cache instantiateCustomCache(CacheConfig config) { + try { + Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass()); + if (ctorWithCacheable != null) { + return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable()); + } + Constructor ctor = getConstructor(config.getCacheClass()); + return (Cache) ctor.newInstance(config.getMaxSize(), getEvictionPolicy(config)); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { + throw new JedisCacheException("Failed to insantiate custom cache type!", e); + } + } + + private Constructor findConstructorWithCacheable(Class customCacheType) { + return Arrays.stream(customCacheType.getConstructors()) + .filter( + ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { Integer.class, EvictionPolicy.class, Cacheable.class })) + .findFirst().orElse(null); + } + + private Constructor getConstructor(Class customCacheType) { + try { + return customCacheType.getConstructor(Integer.class, EvictionPolicy.class); + } catch (NoSuchMethodException e) { + String className = customCacheType.getName(); + throw new JedisCacheException(String.format( + "Failed to find compatible constructor for custom cache type! Provide one of these;" + // give hints about the compatible constructors + + "\n - %s(int maxSize, EvictionPolicy evictionPolicy)\n - %s(int maxSize, EvictionPolicy evictionPolicy, Cacheable cacheable)", + className, className), e); + } + } + + private EvictionPolicy getEvictionPolicy(CacheConfig config) { + if (config.getEvictionPolicy() == null) { + // It will be default to LRUEviction, until we have other eviction implementations + return new LRUEviction(config.getMaxSize()); + } + return config.getEvictionPolicy(); + } +} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/csc/CacheProvider.java b/src/main/java/redis/clients/jedis/csc/CacheProvider.java deleted file mode 100644 index 774f772c49..0000000000 --- a/src/main/java/redis/clients/jedis/csc/CacheProvider.java +++ /dev/null @@ -1,23 +0,0 @@ -package redis.clients.jedis.csc; - -import java.util.HashMap; - -public class CacheProvider { - - public Cache getCache(CacheConfig config) { - return getCache(config, new HashMap()); - } - - public Cache getCache(CacheConfig config, HashMap map) { - return new DefaultCache(config.getMaxSize(), map, config.getCacheable(), - getEvictionPolicy(config)); - } - - private EvictionPolicy getEvictionPolicy(CacheConfig config) { - if (config.getEvictionPolicy() == null) { - // It will be default to LRUEviction, until we have other eviction implementations - return new LRUEviction(config.getMaxSize()); - } - return config.getEvictionPolicy(); - } -} \ No newline at end of file diff --git a/src/main/java/redis/clients/jedis/csc/DefaultCache.java b/src/main/java/redis/clients/jedis/csc/DefaultCache.java index ea561359eb..5577cc0758 100644 --- a/src/main/java/redis/clients/jedis/csc/DefaultCache.java +++ b/src/main/java/redis/clients/jedis/csc/DefaultCache.java @@ -21,6 +21,10 @@ protected DefaultCache(int maximumSize, Cacheable cacheable) { this(maximumSize, new HashMap(), cacheable, new LRUEviction(maximumSize)); } + protected DefaultCache(int maximumSize, Cacheable cacheable, EvictionPolicy evictionPolicy) { + this(maximumSize, new HashMap(), cacheable, evictionPolicy); + } + protected DefaultCache(int maximumSize, Map map, Cacheable cacheable, EvictionPolicy evictionPolicy) { super(maximumSize, cacheable); this.cache = map; diff --git a/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java index 8ee0580011..ddaf27d958 100644 --- a/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java @@ -50,7 +50,7 @@ private static void withPool(Cache cache) throws Exception { .password(endpoint.getPassword()).build(); List tds = new ArrayList<>(); final AtomicInteger ind = new AtomicInteger(); - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), config, cache)) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), config, cache){}) { for (int i = 0; i < NUMBER_OF_THREADS; i++) { Thread hj = new Thread(new Runnable() { @Override diff --git a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java index c8d311865c..5a5ce341b8 100644 --- a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java +++ b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java @@ -25,7 +25,7 @@ public void none() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, null, null)), - singleConnectionPoolConfig.get())) { + singleConnectionPoolConfig.get()){}) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -38,7 +38,7 @@ public void whiteListCommand() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createTestCache(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), - singleConnectionPoolConfig.get())) { + singleConnectionPoolConfig.get()){}) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -51,7 +51,7 @@ public void blackListCommand() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createTestCache(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), - singleConnectionPoolConfig.get())) { + singleConnectionPoolConfig.get()){}) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -64,7 +64,7 @@ public void whiteListKey() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), - singleConnectionPoolConfig.get())) { + singleConnectionPoolConfig.get()){}) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -77,7 +77,7 @@ public void blackListKey() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), - singleConnectionPoolConfig.get())) { + singleConnectionPoolConfig.get()){}) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 0d171df89c..0fe7808bc3 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -17,7 +17,6 @@ import java.util.Map; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -42,8 +41,8 @@ public void flushAllTest() { control.set("k" + i, "v" + i); } - Cache cache = new TestCache(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + Cache cache = jedis.getCache(); for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -66,7 +65,8 @@ public void lruEvictionTest() { Map map = new LinkedHashMap<>(count); Cache cache = new DefaultCache(count, map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache) { + }) { // Retrieve the 100 keys in the same order for (int i = 0; i < count; i++) { @@ -91,8 +91,9 @@ public void lruEvictionTest() { @Test // T.5.2 public void deleteByKeyUsingMGetTest() { - Cache clientSideCache = new TestCache(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + Cache clientSideCache = jedis.getCache(); + jedis.set("1", "one"); jedis.set("2", "two"); @@ -114,7 +115,8 @@ public void deleteByKeyTest() { // By using LinkedHashMap, we can get the hashes (map keys) at the same order of the actual keys. LinkedHashMap map = new LinkedHashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { + }) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -145,7 +147,8 @@ public void deleteByKeysTest() { // By using LinkedHashMap, we can get the hashes (map keys) at the same order of the actual keys. LinkedHashMap map = new LinkedHashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { + }) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -170,7 +173,8 @@ public void deleteByEntryTest() { HashMap map = new HashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { + }) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -196,7 +200,8 @@ public void deleteByEntriesTest() { HashMap map = new HashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { + }) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -215,10 +220,9 @@ public void multiKeyOperation() { control.set("k1", "v1"); control.set("k2", "v2"); - Cache cache = new TestCache(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { jedis.mget("k1", "k2"); - assertEquals(1, cache.getSize()); + assertEquals(1, jedis.getCache().getSize()); } } @@ -227,8 +231,8 @@ public void maximumSizeExact() { control.set("k1", "v1"); control.set("k2", "v2"); - DefaultCache cache = new DefaultCache(1); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1).build())) { + Cache cache = jedis.getCache(); assertEquals(0, cache.getSize()); jedis.get("k1"); assertEquals(1, cache.getSize()); @@ -243,7 +247,8 @@ public void maximumSizeExact() { public void testInvalidationWithUnifiedJedis() { Cache cache = new TestCache(); Cache mock = Mockito.spy(cache); - UnifiedJedis client = new UnifiedJedis(hnp, clientConfig.get(), mock); + UnifiedJedis client = new UnifiedJedis(hnp, clientConfig.get(), mock) { + }; UnifiedJedis controlClient = new UnifiedJedis(hnp, clientConfig.get()); try { @@ -269,11 +274,10 @@ public void testInvalidationWithUnifiedJedis() { @Test public void differentInstanceOnEachCacheHit() { - ConcurrentHashMap map = new ConcurrentHashMap<>(); - TestCache testCache = new TestCache(map); // fill the cache for maxSize - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), testCache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + Cache cache = jedis.getCache(); jedis.sadd("foo", "a"); jedis.sadd("foo", "b"); @@ -284,8 +288,7 @@ public void differentInstanceOnEachCacheHit() { Set members1 = jedis.smembers("foo"); Set members2 = jedis.smembers("foo"); - Set fromMap = (Set) testCache.get(new CacheKey<>(new CommandObjects().smembers("foo"))) - .getValue(); + Set fromMap = (Set) cache.get(new CacheKey<>(new CommandObjects().smembers("foo"))).getValue(); assertEquals(expected, members1); assertEquals(expected, members2); assertEquals(expected, fromMap); @@ -349,8 +352,8 @@ public void testConcurrentAccessWithStats() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(threadCount); // Create the shared mock instance of cache - TestCache testCache = new TestCache(); - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), testCache)) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + Cache cache = jedis.getCache(); // Submit multiple threads to perform concurrent operations CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { @@ -369,13 +372,13 @@ public void testConcurrentAccessWithStats() throws InterruptedException { // wait for all threads to complete latch.await(); - } - executorService.shutdownNow(); + executorService.shutdownNow(); - CacheStats stats = testCache.getStats(); - assertEquals(threadCount * iterations, stats.getMissCount() + stats.getHitCount()); - assertEquals(stats.getMissCount(), stats.getLoadCount()); + CacheStats stats = cache.getStats(); + assertEquals(threadCount * iterations, stats.getMissCount() + stats.getHitCount()); + assertEquals(stats.getMissCount(), stats.getLoadCount()); + } } @Test @@ -386,11 +389,8 @@ public void testMaxSize() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(threadCount); - ConcurrentHashMap map = new ConcurrentHashMap<>(); - // Create the shared mock instance of cache - TestCache testCache = new TestCache(maxSize, map, DefaultCacheable.INSTANCE); - - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), testCache)) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + Cache testCache = jedis.getCache(); // Submit multiple threads to perform concurrent operations CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { @@ -409,16 +409,16 @@ public void testMaxSize() throws InterruptedException { // wait for all threads to complete latch.await(); - } - executorService.shutdownNow(); + executorService.shutdownNow(); - CacheStats stats = testCache.getStats(); + CacheStats stats = testCache.getStats(); - assertEquals(threadCount * iterations, stats.getMissCount() + stats.getHitCount()); - assertEquals(stats.getMissCount(), stats.getLoadCount()); - assertEquals(threadCount * iterations, stats.getNonCacheableCount()); - assertTrue(maxSize >= testCache.getSize()); + assertEquals(threadCount * iterations, stats.getMissCount() + stats.getHitCount()); + assertEquals(stats.getMissCount(), stats.getLoadCount()); + assertEquals(threadCount * iterations, stats.getNonCacheableCount()); + assertTrue(maxSize >= testCache.getSize()); + } } @Test @@ -427,11 +427,10 @@ public void testEvictionPolicy() throws InterruptedException { int expectedEvictions = 20; int touchOffset = 10; - HashMap map = new HashMap<>(); - TestCache testCache = new TestCache(maxSize, map, DefaultCacheable.INSTANCE); - // fill the cache for maxSize - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), testCache)) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), + CacheConfig.builder().maxSize(maxSize).build())) { + Cache cache = jedis.getCache(); for (int i = 0; i < maxSize; i++) { jedis.set("foo" + i, "bar" + i); assertEquals("bar" + i, jedis.get("foo" + i)); @@ -450,21 +449,20 @@ public void testEvictionPolicy() throws InterruptedException { // check touched keys not evicted for (int i = touchOffset; i < touchOffset + expectedEvictions; i++) { - - assertTrue(map.containsKey(new CacheKey(new CommandObjects().get("foo" + i)))); + assertTrue(cache.hasCacheKey(new CacheKey(new CommandObjects().get("foo" + i)))); } // check expected evictions are done till the offset for (int i = 0; i < touchOffset; i++) { - assertTrue(!map.containsKey(new CacheKey(new CommandObjects().get("foo" + i)))); + assertTrue(!cache.hasCacheKey(new CacheKey(new CommandObjects().get("foo" + i)))); } /// check expected evictions are done after the touched keys for (int i = touchOffset + expectedEvictions; i < (2 * expectedEvictions); i++) { - assertTrue(!map.containsKey(new CacheKey(new CommandObjects().get("foo" + i)))); + assertTrue(!cache.hasCacheKey(new CacheKey(new CommandObjects().get("foo" + i)))); } - assertEquals(maxSize, testCache.getSize()); + assertEquals(maxSize, cache.getSize()); } } @@ -476,10 +474,11 @@ public void testEvictionPolicyMultithreaded() throws InterruptedException { int MAX_SIZE = 20; List exceptions = new ArrayList<>(); - TestCache cache = new TestCache(MAX_SIZE, new HashMap<>(), DefaultCacheable.INSTANCE); List tds = new ArrayList<>(); final AtomicInteger ind = new AtomicInteger(); - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), + CacheConfig.builder().maxSize(MAX_SIZE).build())) { + Cache cache = jedis.getCache(); for (int i = 0; i < NUMBER_OF_THREADS; i++) { Thread hj = new Thread(new Runnable() { @Override @@ -517,9 +516,8 @@ public void testNullValue() throws InterruptedException { String nonExisting = "non-existing-key"; control.del(nonExisting); - TestCache cache = new TestCache(MAX_SIZE, new HashMap<>(), DefaultCacheable.INSTANCE); - - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().maxSize(MAX_SIZE).build())) { + Cache cache = jedis.getCache(); CacheStats stats = cache.getStats(); String val = jedis.get(nonExisting); diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index a5823dea56..7d6611a33d 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -35,7 +35,8 @@ protected JedisCluster createRegularJedis() { @Override protected JedisCluster createCachedJedis(Cache cache) { - return new JedisCluster(hnp, clientConfig.get(), cache); + return new JedisCluster(hnp, clientConfig.get(), cache) { + }; } @Override diff --git a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java index e86ef936af..b5357333f6 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java @@ -22,7 +22,7 @@ protected JedisPooled createRegularJedis() { @Override protected JedisPooled createCachedJedis(Cache cache) { - return new JedisPooled(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().resp3().build(), cache); + return new JedisPooled(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().resp3().build(), cache){}; } @Override diff --git a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index e81ec6e90f..410f967219 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -30,7 +30,7 @@ protected JedisSentineled createRegularJedis() { @Override protected JedisSentineled createCachedJedis(Cache cache) { - return new JedisSentineled(MASTER_NAME, masterClientConfig, cache, sentinels, sentinelClientConfig); + return new JedisSentineled(MASTER_NAME, masterClientConfig, cache, sentinels, sentinelClientConfig){}; } @Override diff --git a/src/test/java/redis/clients/jedis/csc/TestCache.java b/src/test/java/redis/clients/jedis/csc/TestCache.java index c4ea43a9e7..d54175793d 100644 --- a/src/test/java/redis/clients/jedis/csc/TestCache.java +++ b/src/test/java/redis/clients/jedis/csc/TestCache.java @@ -17,13 +17,8 @@ public TestCache(Map map, Cacheable cacheable) { super(10000, map, cacheable, new LRUEviction(10000)); } - public TestCache(int maximumSize, Map map, Cacheable cacheable) { - this(maximumSize, map, cacheable, new LRUEviction(maximumSize)); - } - - public TestCache(int maximumSize, Map map, Cacheable cacheable, - EvictionPolicy evictionPolicy) { - super(maximumSize, map, cacheable, evictionPolicy); + public TestCache(int maximumSize, EvictionPolicy evictionPolicy, Cacheable cacheable ) { + super(maximumSize, new HashMap(), cacheable, evictionPolicy); } } From 4118b85de14443df7d100320ee079b5cf5cd7b5a Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 10:19:11 +0300 Subject: [PATCH 02/11] - add cachefactory test --- .../redis/clients/jedis/UnifiedJedis.java | 1 - .../redis/clients/jedis/csc/CacheFactory.java | 4 +- .../csc/ClientSideCacheFunctionalityTest.java | 37 +++++++++++++++++++ .../csc/JedisClusterClientSideCacheTest.java | 3 +- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 6528eb45fc..2d1858013e 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -106,7 +106,6 @@ protected UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, @Experimental public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache cache) { this(new PooledConnectionProvider(hostAndPort, clientConfig, cache), clientConfig.getRedisProtocol(), cache); - } public UnifiedJedis(ConnectionProvider provider) { diff --git a/src/main/java/redis/clients/jedis/csc/CacheFactory.java b/src/main/java/redis/clients/jedis/csc/CacheFactory.java index db503dd662..e68ff6c54e 100644 --- a/src/main/java/redis/clients/jedis/csc/CacheFactory.java +++ b/src/main/java/redis/clients/jedis/csc/CacheFactory.java @@ -31,13 +31,13 @@ private Cache instantiateCustomCache(CacheConfig config) { private Constructor findConstructorWithCacheable(Class customCacheType) { return Arrays.stream(customCacheType.getConstructors()) .filter( - ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { Integer.class, EvictionPolicy.class, Cacheable.class })) + ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class })) .findFirst().orElse(null); } private Constructor getConstructor(Class customCacheType) { try { - return customCacheType.getConstructor(Integer.class, EvictionPolicy.class); + return customCacheType.getConstructor(int.class, EvictionPolicy.class); } catch (NoSuchMethodException e) { String className = customCacheType.getName(); throw new JedisCacheException(String.format( diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 0fe7808bc3..8a338cf9c1 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -543,4 +543,41 @@ public void testNullValue() throws InterruptedException { } } + public static class CustomCache extends TestCache { + public CustomCache(int maximumSize, EvictionPolicy evictionPolicy) { + super(maximumSize, evictionPolicy, DefaultCacheable.INSTANCE); + } + } + + @Test + public void testCacheProvider() throws InterruptedException { + // this checks the instantiation with parameters (int, EvictionPolicy, Cacheable) + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(TestCache.class).build())) { + Cache cache = jedis.getCache(); + CacheStats stats = cache.getStats(); + + String val = jedis.get("foo"); + val = jedis.get("foo"); + assertNull(val); + assertEquals(1, cache.getSize()); + assertNull(cache.getCacheEntries().iterator().next().getValue()); + assertEquals(1, stats.getHitCount()); + assertEquals(1, stats.getMissCount()); + } + + // this checks the instantiation with parameters (int, EvictionPolicy) + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(CustomCache.class).build())) { + Cache cache = jedis.getCache(); + CacheStats stats = cache.getStats(); + + String val = jedis.get("foo"); + val = jedis.get("foo"); + assertNull(val); + assertEquals(1, cache.getSize()); + assertNull(cache.getCacheEntries().iterator().next().getValue()); + assertEquals(1, stats.getHitCount()); + assertEquals(1, stats.getMissCount()); + } + } + } diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 7d6611a33d..ca01239881 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -35,8 +35,7 @@ protected JedisCluster createRegularJedis() { @Override protected JedisCluster createCachedJedis(Cache cache) { - return new JedisCluster(hnp, clientConfig.get(), cache) { - }; + return new JedisCluster(hnp, clientConfig.get(), cache){}; } @Override From f89f0da1aa7825792775aa7124e5b5bf32ad47d4 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 12:23:26 +0300 Subject: [PATCH 03/11] - revert connection ctors to public - udpate some tests with UnifiedJedis.getCache - add ping to flaky tests --- .../redis/clients/jedis/JedisCluster.java | 8 +-- .../java/redis/clients/jedis/JedisPooled.java | 4 +- .../redis/clients/jedis/JedisSentineled.java | 4 +- .../redis/clients/jedis/UnifiedJedis.java | 4 +- .../csc/AllowAndDenyListCacheableTest.java | 56 +++++++-------- .../csc/ClientSideCacheFunctionalityTest.java | 35 +++++---- .../csc/JedisClusterClientSideCacheTest.java | 5 -- .../JedisPooledClientSideCacheTestBase.java | 10 +-- .../JedisSentineledClientSideCacheTest.java | 5 -- .../UnifiedJedisClientSideCacheTestBase.java | 71 ++++++++++--------- 10 files changed, 90 insertions(+), 112 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 414e7629c6..42dd523fbb 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -227,7 +227,7 @@ public JedisCluster(Set hnp, JedisClientConfig jedisClientConfig, C } @Experimental - protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache) { + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache) { this(clusterNodes, clientConfig, clientSideCache, DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis())); } @@ -240,14 +240,14 @@ protected JedisCluster(Set clusterNodes, JedisClientConfig clientCo } @Experimental - protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } @Experimental - protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis()), @@ -255,7 +255,7 @@ protected JedisCluster(Set clusterNodes, JedisClientConfig clientCo } @Experimental - protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, GenericObjectPoolConfig poolConfig, Duration topologyRefreshPeriod, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig, topologyRefreshPeriod), diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index d251c54981..cfb3928f54 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -85,7 +85,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client } @Experimental - protected JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache) { + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache) { super(hostAndPort, clientConfig, clientSideCache); } @@ -396,7 +396,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client } @Experimental - protected JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache, + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache, final GenericObjectPoolConfig poolConfig) { super(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache, poolConfig), clientConfig.getRedisProtocol(), clientSideCache); diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index 6c0d316f38..40e2596335 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -24,7 +24,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo } @Experimental - protected JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, sentinels, sentinelClientConfig), masterClientConfig.getRedisProtocol(), clientSideCache); @@ -38,7 +38,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo } @Experimental - protected JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache, final GenericObjectPoolConfig poolConfig, Set sentinels, final JedisClientConfig sentinelClientConfig) { super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, poolConfig, diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 2d1858013e..e7f2bd440d 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -117,7 +117,7 @@ protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol) { } @Experimental - protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, Cache cache) { + public UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, Cache cache) { this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol, cache); } @@ -196,7 +196,7 @@ protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Dura } @Experimental - protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, + public UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, RedisProtocol protocol, Cache cache) { this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider, new ClusterCommandObjects(), protocol, cache); diff --git a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java index 5a5ce341b8..a0b7d68381 100644 --- a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java +++ b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListCacheableTest.java @@ -1,12 +1,8 @@ package redis.clients.jedis.csc; import static java.util.Collections.singleton; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; -import java.util.HashMap; -import java.util.Map; -import org.hamcrest.Matchers; import org.junit.Test; import redis.clients.jedis.JedisPooled; @@ -15,73 +11,69 @@ public class AllowAndDenyListCacheableTest extends ClientSideCacheTestBase { - private static Cache createTestCache(Map map, Cacheable cacheable) { - Cache mapCache = new TestCache(map, cacheable); - return mapCache; + private static CacheConfig createConfig(Cacheable cacheable) { + return CacheConfig.builder().cacheable(cacheable).cacheClass(TestCache.class).build(); } @Test public void none() { - HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, null, null)), - singleConnectionPoolConfig.get()){}) { + createConfig(new AllowAndDenyListWithStringKeys(null, null, null, null)), singleConnectionPoolConfig.get())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @Test public void whiteListCommand() { - HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - createTestCache(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), - singleConnectionPoolConfig.get()){}) { + createConfig(new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), + singleConnectionPoolConfig.get())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @Test public void blackListCommand() { - HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - createTestCache(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), - singleConnectionPoolConfig.get()){}) { + createConfig(new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), + singleConnectionPoolConfig.get())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); } } @Test public void whiteListKey() { - HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), - singleConnectionPoolConfig.get()){}) { + createConfig(new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + Cache cache = jedis.getCache(); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @Test public void blackListKey() { - HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - createTestCache(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), - singleConnectionPoolConfig.get()){}) { + createConfig(new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), singleConnectionPoolConfig.get())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); } } } diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 8a338cf9c1..b809cda02b 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -22,6 +22,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collector; +import java.util.stream.Collectors; import org.hamcrest.Matchers; import org.junit.Assert; @@ -171,21 +173,19 @@ public void deleteByEntryTest() { control.set("k" + i, "v" + i); } - HashMap map = new HashMap<>(); - Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { - }) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); for (int i = 0; i < count; i++) { jedis.get("k" + i); } - assertThat(map, aMapWithSize(count)); + assertEquals(count, cache.getSize()); - List cacheKeys = new ArrayList<>(map.keySet()); + List cacheKeys = new ArrayList<>(cache.getCacheEntries()); for (int i = 0; i < count; i++) { - CacheKey cacheKey = cacheKeys.get(i); - assertTrue(clientSideCache.delete(cacheKey)); - assertFalse(map.containsKey(cacheKey)); - assertThat(map, aMapWithSize(count - i - 1)); + CacheKey cacheKey = cacheKeys.get(i).getCacheKey(); + assertTrue(cache.delete(cacheKey)); + assertFalse(cache.hasCacheKey(cacheKey)); + assertEquals(count - i - 1, cache.getSize()); } } } @@ -198,20 +198,19 @@ public void deleteByEntriesTest() { control.set("k" + i, "v" + i); } - HashMap map = new HashMap<>(); - Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { - }) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); + for (int i = 0; i < count; i++) { jedis.get("k" + i); } - assertThat(map, aMapWithSize(count)); + assertEquals(count, cache.getSize()); - List cacheKeysToDelete = new ArrayList<>(map.keySet()).subList(0, delete); - List isDeleted = clientSideCache.delete(cacheKeysToDelete); + List cacheKeysToDelete = new ArrayList<>(cache.getCacheEntries()).subList(0, delete).stream().map(e->e.getCacheKey()).collect(Collectors.toList()); + List isDeleted = cache.delete(cacheKeysToDelete); assertThat(isDeleted, hasSize(delete)); isDeleted.forEach(Assert::assertTrue); - assertThat(map, aMapWithSize(count - delete)); + assertEquals(count - delete, cache.getSize()); } } diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index ca01239881..89114d154f 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -33,11 +33,6 @@ protected JedisCluster createRegularJedis() { return new JedisCluster(hnp, clientConfig.get()); } - @Override - protected JedisCluster createCachedJedis(Cache cache) { - return new JedisCluster(hnp, clientConfig.get(), cache){}; - } - @Override protected JedisCluster createCachedJedis(CacheConfig cacheConfig) { return new JedisCluster(hnp, clientConfig.get(), cacheConfig); diff --git a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java index b5357333f6..133efcb3fc 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTestBase.java @@ -20,11 +20,6 @@ protected JedisPooled createRegularJedis() { return new JedisPooled(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().build()); } - @Override - protected JedisPooled createCachedJedis(Cache cache) { - return new JedisPooled(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().resp3().build(), cache){}; - } - @Override protected JedisPooled createCachedJedis(CacheConfig cacheConfig) { return new JedisPooled(endpoint.getHostAndPort(), endpoint.getClientConfigBuilder().resp3().build(), cacheConfig); @@ -32,9 +27,8 @@ protected JedisPooled createCachedJedis(CacheConfig cacheConfig) { @Test public void clearIfOneDiesTest() { - Cache cache = new TestCache(); - try (JedisPooled jedis = createCachedJedis(cache)) { - + try (JedisPooled jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); // Create 100 keys for (int i = 0; i < 100; i++) { jedis.set("key" + i, "value" + i); diff --git a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 410f967219..82da0b14af 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -28,11 +28,6 @@ protected JedisSentineled createRegularJedis() { return new JedisSentineled(MASTER_NAME, masterClientConfig, sentinels, sentinelClientConfig); } - @Override - protected JedisSentineled createCachedJedis(Cache cache) { - return new JedisSentineled(MASTER_NAME, masterClientConfig, cache, sentinels, sentinelClientConfig){}; - } - @Override protected JedisSentineled createCachedJedis(CacheConfig cacheConfig) { return new JedisSentineled(MASTER_NAME, masterClientConfig, cacheConfig, sentinels, sentinelClientConfig); diff --git a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java index 9d512e755e..b08aa33e62 100644 --- a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java @@ -1,15 +1,12 @@ package redis.clients.jedis.csc; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -22,8 +19,6 @@ public abstract class UnifiedJedisClientSideCacheTestBase { protected abstract UnifiedJedis createRegularJedis(); - protected abstract UnifiedJedis createCachedJedis(Cache cache); - protected abstract UnifiedJedis createCachedJedis(CacheConfig cacheConfig); @Before @@ -44,24 +39,29 @@ public void simple() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); + // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection + control.ping(); assertNull(jedis.get("foo")); } } @Test public void simpleWithSimpleMap() { - HashMap map = new HashMap<>(); - try (UnifiedJedis jedis = createCachedJedis(new TestCache(map))) { + // HashMap map = new HashMap<>(); + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); control.del("foo"); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); + // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection + control.ping(); assertNull(jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); assertNull(jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @@ -72,43 +72,47 @@ public void flushAll() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); + // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection + control.ping(); assertNull(jedis.get("foo")); } } @Test public void flushAllWithSimpleMap() { - HashMap map = new HashMap<>(); - try (UnifiedJedis jedis = createCachedJedis(new TestCache(map))) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); control.flushAll(); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); + // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection + control.ping(); assertNull(jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); assertNull(jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @Test public void cacheNotEmptyTest() { - HashMap map = new HashMap<>(); - try (UnifiedJedis jedis = createCachedJedis(new TestCache(map))) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); control.set("foo", "bar"); - assertThat(map, Matchers.aMapWithSize(0)); + assertEquals(0, cache.getSize()); assertEquals("bar", jedis.get("foo")); - assertThat(map, Matchers.aMapWithSize(1)); + assertEquals(1, cache.getSize()); } } @Test public void cacheUsedTest() { - HashMap map = new HashMap<>(); - Cache cache = new TestCache(map); - try (UnifiedJedis jedis = createCachedJedis(cache)) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); + control.set("foo", "bar"); assertEquals(0, cache.getStats().getMissCount()); @@ -126,7 +130,7 @@ public void cacheUsedTest() { @Test public void immutableCacheEntriesTest() { - try (UnifiedJedis jedis = createCachedJedis(new TestCache())) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { jedis.set("{csc}a", "AA"); jedis.set("{csc}b", "BB"); jedis.set("{csc}c", "CC"); @@ -145,8 +149,8 @@ public void immutableCacheEntriesTest() { @Test public void invalidationTest() { - Cache cache = new TestCache(); - try (UnifiedJedis jedis = createCachedJedis(cache)) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); jedis.set("{csc}1", "one"); jedis.set("{csc}2", "two"); jedis.set("{csc}3", "three"); @@ -170,8 +174,8 @@ public void invalidationTest() { @Test public void getNumEntriesTest() { - Cache cache = new TestCache(); - try (UnifiedJedis jedis = createCachedJedis(cache)) { + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); // Create 100 keys for (int i = 0; i < 100; i++) { @@ -189,9 +193,8 @@ public void getNumEntriesTest() { @Test public void invalidationOnCacheHitTest() { - Cache cache = new TestCache(); - try (UnifiedJedis jedis = createCachedJedis(cache)) { - + try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { + Cache cache = jedis.getCache(); // Create 100 keys for (int i = 0; i < 100; i++) { jedis.set("key" + i, "value" + i); From b83a5d410d2756362a7a44ba666dc4c2fc1edf8d Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 12:32:29 +0300 Subject: [PATCH 04/11] remove unnecessary anonymous types --- .../jedis/benchmark/CSCPooleadBenchmark.java | 2 +- .../jedis/csc/ClientSideCacheFunctionalityTest.java | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java b/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java index ddaf27d958..8ee0580011 100644 --- a/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java +++ b/src/test/java/redis/clients/jedis/benchmark/CSCPooleadBenchmark.java @@ -50,7 +50,7 @@ private static void withPool(Cache cache) throws Exception { .password(endpoint.getPassword()).build(); List tds = new ArrayList<>(); final AtomicInteger ind = new AtomicInteger(); - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), config, cache){}) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), config, cache)) { for (int i = 0; i < NUMBER_OF_THREADS; i++) { Thread hj = new Thread(new Runnable() { @Override diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index b809cda02b..5e9be20a49 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -67,8 +67,7 @@ public void lruEvictionTest() { Map map = new LinkedHashMap<>(count); Cache cache = new DefaultCache(count, map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache) { - }) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) { // Retrieve the 100 keys in the same order for (int i = 0; i < count; i++) { @@ -117,8 +116,7 @@ public void deleteByKeyTest() { // By using LinkedHashMap, we can get the hashes (map keys) at the same order of the actual keys. LinkedHashMap map = new LinkedHashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { - }) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -149,8 +147,7 @@ public void deleteByKeysTest() { // By using LinkedHashMap, we can get the hashes (map keys) at the same order of the actual keys. LinkedHashMap map = new LinkedHashMap<>(); Cache clientSideCache = new TestCache(map); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache) { - }) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), clientSideCache)) { for (int i = 0; i < count; i++) { jedis.get("k" + i); } @@ -200,13 +197,13 @@ public void deleteByEntriesTest() { try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { Cache cache = jedis.getCache(); - for (int i = 0; i < count; i++) { jedis.get("k" + i); } assertEquals(count, cache.getSize()); - List cacheKeysToDelete = new ArrayList<>(cache.getCacheEntries()).subList(0, delete).stream().map(e->e.getCacheKey()).collect(Collectors.toList()); + List cacheKeysToDelete = new ArrayList<>(cache.getCacheEntries()).subList(0, delete).stream().map(e -> e.getCacheKey()) + .collect(Collectors.toList()); List isDeleted = cache.delete(cacheKeysToDelete); assertThat(isDeleted, hasSize(delete)); isDeleted.forEach(Assert::assertTrue); From 3085004c8c1d91e26fa8aa7c70467e78b542466d Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 12:41:21 +0300 Subject: [PATCH 05/11] change ctor access modifiers --- src/main/java/redis/clients/jedis/JedisCluster.java | 2 +- src/main/java/redis/clients/jedis/UnifiedJedis.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 42dd523fbb..6ae11cc9c3 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -233,7 +233,7 @@ public JedisCluster(Set clusterNodes, JedisClientConfig clientConfi } @Experimental - protected JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index e7f2bd440d..eee7ab9c37 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -99,7 +99,7 @@ public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) { } @Experimental - protected UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig) { + public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig) { this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig)); } @@ -117,7 +117,7 @@ protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol) { } @Experimental - public UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, Cache cache) { + protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, Cache cache) { this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol, cache); } @@ -196,7 +196,7 @@ protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Dura } @Experimental - public UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, + protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, RedisProtocol protocol, Cache cache) { this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider, new ClusterCommandObjects(), protocol, cache); From e4d76037bb947cada6c76cac2867acf9838f2c90 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 12:45:03 +0300 Subject: [PATCH 06/11] fix test name --- .../clients/jedis/csc/ClientSideCacheFunctionalityTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 5e9be20a49..5a360732ce 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -546,7 +546,7 @@ public CustomCache(int maximumSize, EvictionPolicy evictionPolicy) { } @Test - public void testCacheProvider() throws InterruptedException { + public void testCacheFactory() throws InterruptedException { // this checks the instantiation with parameters (int, EvictionPolicy, Cacheable) try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(TestCache.class).build())) { Cache cache = jedis.getCache(); From f5cb18b1dd107460ec366064ad9b0941efc854c7 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 14:55:03 +0300 Subject: [PATCH 07/11] make cachefactory methods static --- src/main/java/redis/clients/jedis/JedisCluster.java | 2 +- src/main/java/redis/clients/jedis/JedisPooled.java | 4 ++-- .../java/redis/clients/jedis/JedisSentineled.java | 2 +- src/main/java/redis/clients/jedis/UnifiedJedis.java | 2 +- .../java/redis/clients/jedis/csc/CacheFactory.java | 12 ++++++------ 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 6ae11cc9c3..0b9f5b49d1 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -223,7 +223,7 @@ private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Durati @Experimental public JedisCluster(Set hnp, JedisClientConfig jedisClientConfig, CacheConfig cacheConfig) { - this(hnp, jedisClientConfig, new CacheFactory().getCache(cacheConfig)); + this(hnp, jedisClientConfig, CacheFactory.getCache(cacheConfig)); } @Experimental diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index cfb3928f54..c3429319e7 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -81,7 +81,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client @Experimental public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, CacheConfig cacheConfig) { - this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig)); + this(hostAndPort, clientConfig, CacheFactory.getCache(cacheConfig)); } @Experimental @@ -392,7 +392,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client @Experimental public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, CacheConfig cacheConfig, final GenericObjectPoolConfig poolConfig) { - this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig), poolConfig); + this(hostAndPort, clientConfig, CacheFactory.getCache(cacheConfig), poolConfig); } @Experimental diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index 40e2596335..26f208a03b 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -19,7 +19,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo @Experimental public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, CacheConfig cacheConfig, Set sentinels, final JedisClientConfig sentinelClientConfig) { - this(masterName, masterClientConfig, new CacheFactory().getCache(cacheConfig), + this(masterName, masterClientConfig, CacheFactory.getCache(cacheConfig), sentinels, sentinelClientConfig); } diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index eee7ab9c37..9ccf2538b2 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -100,7 +100,7 @@ public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) { @Experimental public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, CacheConfig cacheConfig) { - this(hostAndPort, clientConfig, new CacheFactory().getCache(cacheConfig)); + this(hostAndPort, clientConfig, CacheFactory.getCache(cacheConfig)); } @Experimental diff --git a/src/main/java/redis/clients/jedis/csc/CacheFactory.java b/src/main/java/redis/clients/jedis/csc/CacheFactory.java index e68ff6c54e..2e65899f35 100644 --- a/src/main/java/redis/clients/jedis/csc/CacheFactory.java +++ b/src/main/java/redis/clients/jedis/csc/CacheFactory.java @@ -6,16 +6,16 @@ import redis.clients.jedis.exceptions.JedisCacheException; -public class CacheFactory { +public final class CacheFactory { - public Cache getCache(CacheConfig config) { + public static Cache getCache(CacheConfig config) { if (config.getCacheClass() == null) { return new DefaultCache(config.getMaxSize(), config.getCacheable(), getEvictionPolicy(config)); } return instantiateCustomCache(config); } - private Cache instantiateCustomCache(CacheConfig config) { + private static Cache instantiateCustomCache(CacheConfig config) { try { Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass()); if (ctorWithCacheable != null) { @@ -28,14 +28,14 @@ private Cache instantiateCustomCache(CacheConfig config) { } } - private Constructor findConstructorWithCacheable(Class customCacheType) { + private static Constructor findConstructorWithCacheable(Class customCacheType) { return Arrays.stream(customCacheType.getConstructors()) .filter( ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class })) .findFirst().orElse(null); } - private Constructor getConstructor(Class customCacheType) { + private static Constructor getConstructor(Class customCacheType) { try { return customCacheType.getConstructor(int.class, EvictionPolicy.class); } catch (NoSuchMethodException e) { @@ -48,7 +48,7 @@ private Constructor getConstructor(Class customCacheType) { } } - private EvictionPolicy getEvictionPolicy(CacheConfig config) { + private static EvictionPolicy getEvictionPolicy(CacheConfig config) { if (config.getEvictionPolicy() == null) { // It will be default to LRUEviction, until we have other eviction implementations return new LRUEviction(config.getMaxSize()); From 74a41857aa016ad769692fd9d4463f49796e6175 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 15:46:31 +0300 Subject: [PATCH 08/11] removing pings due to still flaky with inv messages --- .../jedis/csc/UnifiedJedisClientSideCacheTestBase.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java index b08aa33e62..49707d0b97 100644 --- a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java @@ -39,8 +39,6 @@ public void simple() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); - // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection - control.ping(); assertNull(jedis.get("foo")); } } @@ -56,8 +54,6 @@ public void simpleWithSimpleMap() { assertEquals(1, cache.getSize()); control.del("foo"); assertEquals(1, cache.getSize()); - // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection - control.ping(); assertNull(jedis.get("foo")); assertEquals(1, cache.getSize()); assertNull(jedis.get("foo")); @@ -72,8 +68,6 @@ public void flushAll() { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); - // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection - control.ping(); assertNull(jedis.get("foo")); } } @@ -88,8 +82,6 @@ public void flushAllWithSimpleMap() { assertEquals(1, cache.getSize()); control.flushAll(); assertEquals(1, cache.getSize()); - // this ping is an attempt to gain a reasonable amount of time to let invalidation messsage shows up on CacheConnection - control.ping(); assertNull(jedis.get("foo")); assertEquals(1, cache.getSize()); assertNull(jedis.get("foo")); From e69ae8de8029e5448f894f8e55125f913e478f67 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 17:16:21 +0300 Subject: [PATCH 09/11] - drop CustomCache in tests and use TestCache - check null cacheable issue with defaultcache - support both ctors in custom cache classes regarding to value of cacheconfig.cacheable --- .../redis/clients/jedis/csc/CacheFactory.java | 19 ++++++++++++------- .../csc/ClientSideCacheFunctionalityTest.java | 14 ++++---------- .../redis/clients/jedis/csc/TestCache.java | 4 ++++ .../UnifiedJedisClientSideCacheTestBase.java | 1 - 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/redis/clients/jedis/csc/CacheFactory.java b/src/main/java/redis/clients/jedis/csc/CacheFactory.java index 2e65899f35..0286783dfc 100644 --- a/src/main/java/redis/clients/jedis/csc/CacheFactory.java +++ b/src/main/java/redis/clients/jedis/csc/CacheFactory.java @@ -10,28 +10,33 @@ public final class CacheFactory { public static Cache getCache(CacheConfig config) { if (config.getCacheClass() == null) { + if (config.getCacheable() == null) { + throw new JedisCacheException("Cacheable is required to create the default cache!"); + } return new DefaultCache(config.getMaxSize(), config.getCacheable(), getEvictionPolicy(config)); } return instantiateCustomCache(config); } - private static Cache instantiateCustomCache(CacheConfig config) { + private static Cache instantiateCustomCache(CacheConfig config) { try { - Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass()); - if (ctorWithCacheable != null) { - return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable()); + if (config.getCacheable() != null) { + Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass()); + if (ctorWithCacheable != null) { + return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable()); + } } Constructor ctor = getConstructor(config.getCacheClass()); return (Cache) ctor.newInstance(config.getMaxSize(), getEvictionPolicy(config)); - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | SecurityException e) { throw new JedisCacheException("Failed to insantiate custom cache type!", e); } } private static Constructor findConstructorWithCacheable(Class customCacheType) { return Arrays.stream(customCacheType.getConstructors()) - .filter( - ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class })) + .filter(ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class })) .findFirst().orElse(null); } diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 5a360732ce..e7042edb80 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -512,7 +512,7 @@ public void testNullValue() throws InterruptedException { String nonExisting = "non-existing-key"; control.del(nonExisting); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().maxSize(MAX_SIZE).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(MAX_SIZE).build())) { Cache cache = jedis.getCache(); CacheStats stats = cache.getStats(); @@ -539,16 +539,10 @@ public void testNullValue() throws InterruptedException { } } - public static class CustomCache extends TestCache { - public CustomCache(int maximumSize, EvictionPolicy evictionPolicy) { - super(maximumSize, evictionPolicy, DefaultCacheable.INSTANCE); - } - } - @Test public void testCacheFactory() throws InterruptedException { // this checks the instantiation with parameters (int, EvictionPolicy, Cacheable) - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(TestCache.class).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().cacheClass(TestCache.class).build())) { Cache cache = jedis.getCache(); CacheStats stats = cache.getStats(); @@ -562,7 +556,8 @@ public void testCacheFactory() throws InterruptedException { } // this checks the instantiation with parameters (int, EvictionPolicy) - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(CustomCache.class).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + CacheConfig.builder().cacheClass(TestCache.class).cacheable(null).build())) { Cache cache = jedis.getCache(); CacheStats stats = cache.getStats(); @@ -575,5 +570,4 @@ public void testCacheFactory() throws InterruptedException { assertEquals(1, stats.getMissCount()); } } - } diff --git a/src/test/java/redis/clients/jedis/csc/TestCache.java b/src/test/java/redis/clients/jedis/csc/TestCache.java index d54175793d..0c9db2dbba 100644 --- a/src/test/java/redis/clients/jedis/csc/TestCache.java +++ b/src/test/java/redis/clients/jedis/csc/TestCache.java @@ -17,6 +17,10 @@ public TestCache(Map map, Cacheable cacheable) { super(10000, map, cacheable, new LRUEviction(10000)); } + public TestCache(int maximumSize, EvictionPolicy evictionPolicy ) { + super(maximumSize, new HashMap(), DefaultCacheable.INSTANCE, evictionPolicy); + } + public TestCache(int maximumSize, EvictionPolicy evictionPolicy, Cacheable cacheable ) { super(maximumSize, new HashMap(), cacheable, evictionPolicy); } diff --git a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java index 49707d0b97..388113307b 100644 --- a/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java +++ b/src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java @@ -45,7 +45,6 @@ public void simple() { @Test public void simpleWithSimpleMap() { - // HashMap map = new HashMap<>(); try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) { Cache cache = jedis.getCache(); control.set("foo", "bar"); From 2923793dc65f8267e9e70e54a5e453509091a203 Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 17:31:01 +0300 Subject: [PATCH 10/11] remove unncessary maxsize --- .../jedis/csc/ClientSideCacheFunctionalityTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index e7042edb80..80de3d0f6b 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -43,7 +43,7 @@ public void flushAllTest() { control.set("k" + i, "v" + i); } - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { Cache cache = jedis.getCache(); for (int i = 0; i < count; i++) { jedis.get("k" + i); @@ -92,7 +92,7 @@ public void lruEvictionTest() { @Test // T.5.2 public void deleteByKeyUsingMGetTest() { - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { Cache clientSideCache = jedis.getCache(); jedis.set("1", "one"); @@ -216,7 +216,7 @@ public void multiKeyOperation() { control.set("k1", "v1"); control.set("k2", "v2"); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { jedis.mget("k1", "k2"); assertEquals(1, jedis.getCache().getSize()); } @@ -272,7 +272,7 @@ public void testInvalidationWithUnifiedJedis() { public void differentInstanceOnEachCacheHit() { // fill the cache for maxSize - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().build())) { Cache cache = jedis.getCache(); jedis.sadd("foo", "a"); jedis.sadd("foo", "b"); @@ -348,7 +348,7 @@ public void testConcurrentAccessWithStats() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(threadCount); // Create the shared mock instance of cache - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().build())) { Cache cache = jedis.getCache(); // Submit multiple threads to perform concurrent operations CountDownLatch latch = new CountDownLatch(threadCount); @@ -385,7 +385,7 @@ public void testMaxSize() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(threadCount); - try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().maxSize(1000).build())) { + try (JedisPooled jedis = new JedisPooled(endpoint.getHostAndPort(), clientConfig.get(), CacheConfig.builder().maxSize(maxSize).build())) { Cache testCache = jedis.getCache(); // Submit multiple threads to perform concurrent operations CountDownLatch latch = new CountDownLatch(threadCount); From 91ecbec691b3b015d2e430251c7cb88f04a51dcd Mon Sep 17 00:00:00 2001 From: atakavci Date: Thu, 29 Aug 2024 17:43:05 +0300 Subject: [PATCH 11/11] - remove inline anonymious --- .../clients/jedis/csc/ClientSideCacheFunctionalityTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java index 80de3d0f6b..d2032e5d23 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java @@ -243,8 +243,7 @@ public void maximumSizeExact() { public void testInvalidationWithUnifiedJedis() { Cache cache = new TestCache(); Cache mock = Mockito.spy(cache); - UnifiedJedis client = new UnifiedJedis(hnp, clientConfig.get(), mock) { - }; + UnifiedJedis client = new UnifiedJedis(hnp, clientConfig.get(), mock); UnifiedJedis controlClient = new UnifiedJedis(hnp, clientConfig.get()); try {