From 45827a827efe42ffd3eaa4fe593ea1f9e410a72d Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:35:50 +0600 Subject: [PATCH 1/6] Rename 'Hashing' to 'Hash' to keep similarity with 'Cache' --- .../redis/clients/jedis/csc/CaffeineCSC.java | 14 ++++++------- .../clients/jedis/csc/ClientSideCache.java | 8 ++++---- .../redis/clients/jedis/csc/GuavaCSC.java | 20 +++++++++---------- ...dHashing.java => AbstractCommandHash.java} | 2 +- ...dLongHashing.java => CommandLongHash.java} | 2 +- ...uavaHashing.java => GuavaCommandHash.java} | 4 ++-- ...ftHashing.java => OpenHftCommandHash.java} | 4 ++-- ...ayHashing.java => PrimitiveArrayHash.java} | 2 +- .../jedis/csc/ClientSideCacheLibsTest.java | 4 ++-- .../java/redis/clients/jedis/csc/MapCSC.java | 7 ++----- 10 files changed, 32 insertions(+), 35 deletions(-) rename src/main/java/redis/clients/jedis/csc/hash/{AbstractCommandHashing.java => AbstractCommandHash.java} (89%) rename src/main/java/redis/clients/jedis/csc/hash/{CommandLongHashing.java => CommandLongHash.java} (89%) rename src/main/java/redis/clients/jedis/csc/hash/{GuavaHashing.java => GuavaCommandHash.java} (84%) rename src/main/java/redis/clients/jedis/csc/hash/{OpenHftHashing.java => OpenHftCommandHash.java} (79%) rename src/main/java/redis/clients/jedis/csc/hash/{PrimitiveArrayHashing.java => PrimitiveArrayHash.java} (85%) diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java index 82f4f0f9a8a..cfddb549c73 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java @@ -4,22 +4,22 @@ import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.CommandLongHashing; -import redis.clients.jedis.csc.hash.OpenHftHashing; +import redis.clients.jedis.csc.hash.OpenHftCommandHash; +import redis.clients.jedis.csc.hash.CommandLongHash; public class CaffeineCSC extends ClientSideCache { private final Cache cache; public CaffeineCSC(Cache caffeineCache) { - this(caffeineCache, new OpenHftHashing(OpenHftHashing.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE); + this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE); } public CaffeineCSC(Cache caffeineCache, ClientSideCacheable cacheable) { - this(caffeineCache, new OpenHftHashing(OpenHftHashing.DEFAULT_HASH_FUNCTION), cacheable); + this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), cacheable); } - public CaffeineCSC(Cache caffeineCache, CommandLongHashing hashing, ClientSideCacheable cacheable) { + public CaffeineCSC(Cache caffeineCache, CommandLongHash hashing, ClientSideCacheable cacheable) { super(hashing, cacheable); this.cache = caffeineCache; } @@ -55,7 +55,7 @@ public static class Builder { private final TimeUnit expireTimeUnit = TimeUnit.SECONDS; // not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)' - private CommandLongHashing longHashing = null; + private CommandLongHash longHashing = null; private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; @@ -71,7 +71,7 @@ public Builder ttl(int seconds) { return this; } - public Builder hashing(CommandLongHashing hashing) { + public Builder hash(CommandLongHash hashing) { this.longHashing = hashing; return this; } diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index ef6b833a60f..6ead3cee4d0 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -8,8 +8,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import redis.clients.jedis.CommandObject; -import redis.clients.jedis.csc.hash.CommandLongHashing; import redis.clients.jedis.util.SafeEncoder; +import redis.clients.jedis.csc.hash.CommandLongHash; /** * The class to manage the client-side caching. User can provide any of implementation of this class to the client @@ -22,14 +22,14 @@ public abstract class ClientSideCache { protected static final int DEFAULT_EXPIRE_SECONDS = 100; private final Map> keyToCommandHashes = new ConcurrentHashMap<>(); - private final CommandLongHashing commandHashing; + private final CommandLongHash commandHashing; private final ClientSideCacheable cacheable; - protected ClientSideCache(CommandLongHashing commandHashing) { + protected ClientSideCache(CommandLongHash commandHashing) { this(commandHashing, DefaultClientSideCacheable.INSTANCE); } - protected ClientSideCache(CommandLongHashing commandHashing, ClientSideCacheable cacheable) { + protected ClientSideCache(CommandLongHash commandHashing, ClientSideCacheable cacheable) { this.commandHashing = commandHashing; this.cacheable = cacheable; } diff --git a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java index c5c173f5d84..00b37c8fd0d 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java @@ -5,31 +5,31 @@ import com.google.common.hash.HashFunction; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.CommandLongHashing; -import redis.clients.jedis.csc.hash.GuavaHashing; +import redis.clients.jedis.csc.hash.GuavaCommandHash; +import redis.clients.jedis.csc.hash.CommandLongHash; public class GuavaCSC extends ClientSideCache { private final Cache cache; public GuavaCSC(Cache guavaCache) { - this(guavaCache, GuavaHashing.DEFAULT_HASH_FUNCTION); + this(guavaCache, GuavaCommandHash.DEFAULT_HASH_FUNCTION); } public GuavaCSC(Cache guavaCache, HashFunction hashFunction) { - this(guavaCache, new GuavaHashing(hashFunction)); + this(guavaCache, new GuavaCommandHash(hashFunction)); } - public GuavaCSC(Cache guavaCache, CommandLongHashing hashing) { + public GuavaCSC(Cache guavaCache, CommandLongHash hashing) { super(hashing); this.cache = guavaCache; } public GuavaCSC(Cache guavaCache, ClientSideCacheable cacheable) { - this(guavaCache, new GuavaHashing(GuavaHashing.DEFAULT_HASH_FUNCTION), cacheable); + this(guavaCache, new GuavaCommandHash(GuavaCommandHash.DEFAULT_HASH_FUNCTION), cacheable); } - public GuavaCSC(Cache cache, CommandLongHashing hashing, ClientSideCacheable cacheable) { + public GuavaCSC(Cache cache, CommandLongHash hashing, ClientSideCacheable cacheable) { super(hashing, cacheable); this.cache = cache; } @@ -66,7 +66,7 @@ public static class Builder { // not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)' private HashFunction hashFunction = null; - private CommandLongHashing longHashing = null; + private CommandLongHash longHashing = null; private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; @@ -88,7 +88,7 @@ public Builder hashFunction(HashFunction function) { return this; } - public Builder hashing(CommandLongHashing hashing) { + public Builder hash(CommandLongHash hashing) { this.longHashing = hashing; this.hashFunction = null; return this; @@ -107,7 +107,7 @@ public GuavaCSC build() { cb.expireAfterWrite(expireTime, expireTimeUnit); return longHashing != null ? new GuavaCSC(cb.build(), longHashing, cacheable) - : hashFunction != null ? new GuavaCSC(cb.build(), new GuavaHashing(hashFunction), cacheable) + : hashFunction != null ? new GuavaCSC(cb.build(), new GuavaCommandHash(hashFunction), cacheable) : new GuavaCSC(cb.build(), cacheable); } } diff --git a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java b/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java similarity index 89% rename from src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java rename to src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java index 561217e2999..1acdf0dc064 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java +++ b/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java @@ -4,7 +4,7 @@ import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; -public abstract class AbstractCommandHashing implements CommandLongHashing { +public abstract class AbstractCommandHash implements CommandLongHash { @Override public final long hash(CommandObject command) { diff --git a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java similarity index 89% rename from src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java rename to src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java index 6632f46e724..4e03f376288 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java +++ b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java @@ -5,7 +5,7 @@ /** * The interface for hashing a command object for client-side caching. */ -public interface CommandLongHashing { +public interface CommandLongHash { /** * Produce a 64-bit signed hash from a command object. diff --git a/src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java similarity index 84% rename from src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java rename to src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java index 2b9ad6ff5af..7249e8d3b34 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java +++ b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java @@ -4,13 +4,13 @@ import com.google.common.hash.Hasher; import redis.clients.jedis.CommandObject; -public class GuavaHashing implements CommandLongHashing { +public class GuavaCommandHash implements CommandLongHash { public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); private final HashFunction function; - public GuavaHashing(HashFunction function) { + public GuavaCommandHash(HashFunction function) { this.function = function; } diff --git a/src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java b/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java similarity index 79% rename from src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java rename to src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java index b112980fd4b..0a38115987d 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java +++ b/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java @@ -2,13 +2,13 @@ import net.openhft.hashing.LongHashFunction; -public class OpenHftHashing extends PrimitiveArrayHashing implements CommandLongHashing { +public class OpenHftCommandHash extends PrimitiveArrayHash implements CommandLongHash { public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); private final LongHashFunction function; - public OpenHftHashing(LongHashFunction function) { + public OpenHftCommandHash(LongHashFunction function) { this.function = function; } diff --git a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java b/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java similarity index 85% rename from src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java rename to src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java index 385f97fdd23..671a227bfb4 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java +++ b/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java @@ -3,7 +3,7 @@ import redis.clients.jedis.Builder; import redis.clients.jedis.args.Rawable; -public abstract class PrimitiveArrayHashing extends AbstractCommandHashing { +public abstract class PrimitiveArrayHash extends AbstractCommandHash { @Override protected final long hashRawable(Rawable raw) { diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index b49d524f833..f4e12ad80bd 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -23,7 +23,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPooled; -import redis.clients.jedis.csc.hash.OpenHftHashing; +import redis.clients.jedis.csc.hash.OpenHftCommandHash; public class ClientSideCacheLibsTest { @@ -93,7 +93,7 @@ public void guavaMore() { @Test public void caffeineSimple() { CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10) - .hashing(new OpenHftHashing(LongHashFunction.xx())).build(); + .hash(new OpenHftCommandHash(LongHashFunction.xx())).build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/MapCSC.java b/src/test/java/redis/clients/jedis/csc/MapCSC.java index fcb6d232a86..a678ac24429 100644 --- a/src/test/java/redis/clients/jedis/csc/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/MapCSC.java @@ -3,14 +3,11 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; - -import redis.clients.jedis.CommandObject; -import redis.clients.jedis.args.Rawable; -import redis.clients.jedis.csc.hash.PrimitiveArrayHashing; +import redis.clients.jedis.csc.hash.PrimitiveArrayHash; public class MapCSC extends ClientSideCache { - private static final PrimitiveArrayHashing HASHING = new PrimitiveArrayHashing() { + private static final PrimitiveArrayHash HASHING = new PrimitiveArrayHash() { @Override protected long hashLongs(long[] longs) { From e5034c60870921707ec57ea4b430290e2b895c96 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:37:21 +0600 Subject: [PATCH 2/6] Expand GuavaCSC and CaffeineCSC names --- ...eCSC.java => CaffeineClientSideCache.java} | 14 ++++++------- ...uavaCSC.java => GuavaClientSideCache.java} | 20 +++++++++---------- .../clients/jedis/util/JedisURIHelper.java | 8 ++++---- .../AllowAndDenyListClientSideCacheTest.java | 10 +++++----- .../jedis/csc/ClientSideCacheLibsTest.java | 8 ++++---- .../csc/JedisClusterClientSideCacheTest.java | 8 ++++---- .../csc/JedisPooledClientSideCacheTest.java | 8 ++++---- .../JedisSentineledClientSideCacheTest.java | 8 ++++---- .../{MapCSC.java => MapClientSideCache.java} | 8 ++++---- 9 files changed, 46 insertions(+), 46 deletions(-) rename src/main/java/redis/clients/jedis/csc/{CaffeineCSC.java => CaffeineClientSideCache.java} (79%) rename src/main/java/redis/clients/jedis/csc/{GuavaCSC.java => GuavaClientSideCache.java} (74%) rename src/test/java/redis/clients/jedis/csc/{MapCSC.java => MapClientSideCache.java} (81%) diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java similarity index 79% rename from src/main/java/redis/clients/jedis/csc/CaffeineCSC.java rename to src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java index cfddb549c73..0b9043e6282 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java @@ -7,19 +7,19 @@ import redis.clients.jedis.csc.hash.OpenHftCommandHash; import redis.clients.jedis.csc.hash.CommandLongHash; -public class CaffeineCSC extends ClientSideCache { +public class CaffeineClientSideCache extends ClientSideCache { private final Cache cache; - public CaffeineCSC(Cache caffeineCache) { + public CaffeineClientSideCache(Cache caffeineCache) { this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE); } - public CaffeineCSC(Cache caffeineCache, ClientSideCacheable cacheable) { + public CaffeineClientSideCache(Cache caffeineCache, ClientSideCacheable cacheable) { this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), cacheable); } - public CaffeineCSC(Cache caffeineCache, CommandLongHash hashing, ClientSideCacheable cacheable) { + public CaffeineClientSideCache(Cache caffeineCache, CommandLongHash hashing, ClientSideCacheable cacheable) { super(hashing, cacheable); this.cache = caffeineCache; } @@ -81,7 +81,7 @@ public Builder cacheable(ClientSideCacheable cacheable) { return this; } - public CaffeineCSC build() { + public CaffeineClientSideCache build() { Caffeine cb = Caffeine.newBuilder(); cb.maximumSize(maximumSize); @@ -89,8 +89,8 @@ public CaffeineCSC build() { cb.expireAfterWrite(expireTime, expireTimeUnit); return longHashing != null - ? new CaffeineCSC(cb.build(), longHashing, cacheable) - : new CaffeineCSC(cb.build(), cacheable); + ? new CaffeineClientSideCache(cb.build(), longHashing, cacheable) + : new CaffeineClientSideCache(cb.build(), cacheable); } } } diff --git a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java similarity index 74% rename from src/main/java/redis/clients/jedis/csc/GuavaCSC.java rename to src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java index 00b37c8fd0d..0e15bf9e574 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java @@ -8,28 +8,28 @@ import redis.clients.jedis.csc.hash.GuavaCommandHash; import redis.clients.jedis.csc.hash.CommandLongHash; -public class GuavaCSC extends ClientSideCache { +public class GuavaClientSideCache extends ClientSideCache { private final Cache cache; - public GuavaCSC(Cache guavaCache) { + public GuavaClientSideCache(Cache guavaCache) { this(guavaCache, GuavaCommandHash.DEFAULT_HASH_FUNCTION); } - public GuavaCSC(Cache guavaCache, HashFunction hashFunction) { + public GuavaClientSideCache(Cache guavaCache, HashFunction hashFunction) { this(guavaCache, new GuavaCommandHash(hashFunction)); } - public GuavaCSC(Cache guavaCache, CommandLongHash hashing) { + public GuavaClientSideCache(Cache guavaCache, CommandLongHash hashing) { super(hashing); this.cache = guavaCache; } - public GuavaCSC(Cache guavaCache, ClientSideCacheable cacheable) { + public GuavaClientSideCache(Cache guavaCache, ClientSideCacheable cacheable) { this(guavaCache, new GuavaCommandHash(GuavaCommandHash.DEFAULT_HASH_FUNCTION), cacheable); } - public GuavaCSC(Cache cache, CommandLongHash hashing, ClientSideCacheable cacheable) { + public GuavaClientSideCache(Cache cache, CommandLongHash hashing, ClientSideCacheable cacheable) { super(hashing, cacheable); this.cache = cache; } @@ -99,16 +99,16 @@ public Builder cacheable(ClientSideCacheable cacheable) { return this; } - public GuavaCSC build() { + public GuavaClientSideCache build() { CacheBuilder cb = CacheBuilder.newBuilder(); cb.maximumSize(maximumSize); cb.expireAfterWrite(expireTime, expireTimeUnit); - return longHashing != null ? new GuavaCSC(cb.build(), longHashing, cacheable) - : hashFunction != null ? new GuavaCSC(cb.build(), new GuavaCommandHash(hashFunction), cacheable) - : new GuavaCSC(cb.build(), cacheable); + return longHashing != null ? new GuavaClientSideCache(cb.build(), longHashing, cacheable) + : hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHash(hashFunction), cacheable) + : new GuavaClientSideCache(cb.build(), cacheable); } } } diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 6abedd05c43..ab1ee1d66a9 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -4,9 +4,9 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; -import redis.clients.jedis.csc.CaffeineCSC; +import redis.clients.jedis.csc.CaffeineClientSideCache; import redis.clients.jedis.csc.ClientSideCache; -import redis.clients.jedis.csc.GuavaCSC; +import redis.clients.jedis.csc.GuavaClientSideCache; public final class JedisURIHelper { @@ -137,12 +137,12 @@ public static ClientSideCache getClientSideCache(URI uri) { } if (guava) { - GuavaCSC.Builder guavaBuilder = GuavaCSC.builder(); + GuavaClientSideCache.Builder guavaBuilder = GuavaClientSideCache.builder(); if (maxSize != null) guavaBuilder.maximumSize(maxSize); if (ttl != null) guavaBuilder.ttl(ttl); return guavaBuilder.build(); } else if (caffeine) { - CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder(); + CaffeineClientSideCache.Builder caffeineBuilder = CaffeineClientSideCache.builder(); if (maxSize != null) caffeineBuilder.maximumSize(maxSize); if (ttl != null) caffeineBuilder.ttl(ttl); return caffeineBuilder.build(); diff --git a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java index 86c6dbc42fc..17672ff955b 100644 --- a/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java @@ -54,7 +54,7 @@ public void tearDown() throws Exception { public void none() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, null, null)), + new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -67,7 +67,7 @@ public void none() { public void whiteListCommand() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new MapCSC(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), + new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -80,7 +80,7 @@ public void whiteListCommand() { public void blackListCommand() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new MapCSC(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), + new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -93,7 +93,7 @@ public void blackListCommand() { public void whiteListKey() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), + new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -106,7 +106,7 @@ public void whiteListKey() { public void blackListKey() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), + new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index f4e12ad80bd..2880a53ef16 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -54,7 +54,7 @@ public void tearDown() throws Exception { @Test public void guavaSimple() { - GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10) + GuavaClientSideCache guava = GuavaClientSideCache.builder().maximumSize(10).ttl(10) .hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) { control.set("foo", "bar"); @@ -69,7 +69,7 @@ public void guavaMore() { com.google.common.cache.Cache guava = CacheBuilder.newBuilder().recordStats().build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaCSC(guava), + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaClientSideCache(guava), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, guava.size()); @@ -92,7 +92,7 @@ public void guavaMore() { @Test public void caffeineSimple() { - CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10) + CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10) .hash(new OpenHftCommandHash(LongHashFunction.xx())).build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { control.set("foo", "bar"); @@ -107,7 +107,7 @@ public void caffeineMore() { com.github.benmanes.caffeine.cache.Cache caffeine = Caffeine.newBuilder().recordStats().build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineCSC(caffeine), + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CaffeineClientSideCache(caffeine), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, caffeine.estimatedSize()); diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 3b520725602..edd8c786e94 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -38,7 +38,7 @@ public class JedisClusterClientSideCacheTest extends JedisClusterTestBase { @Test public void simple() { - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC())) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache())) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.del("foo"); @@ -49,7 +49,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(map), + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -68,7 +68,7 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC())) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache())) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.flushAll(); @@ -79,7 +79,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(map), + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); diff --git a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java index 149b1730a01..39f78df68f6 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java @@ -50,7 +50,7 @@ public void tearDown() throws Exception { @Test public void simple() { - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache())) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -61,7 +61,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC(map), + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -80,7 +80,7 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache())) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); @@ -91,7 +91,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC(map), + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapClientSideCache(map), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); diff --git a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 6390ab3194e..9b8eac8f9f2 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -32,7 +32,7 @@ public class JedisSentineledClientSideCacheTest { @Test public void simple() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); @@ -44,7 +44,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(map), + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -63,7 +63,7 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); @@ -75,7 +75,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(map), + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapClientSideCache(map), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); diff --git a/src/test/java/redis/clients/jedis/csc/MapCSC.java b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java similarity index 81% rename from src/test/java/redis/clients/jedis/csc/MapCSC.java rename to src/test/java/redis/clients/jedis/csc/MapClientSideCache.java index a678ac24429..dcdea2bf956 100644 --- a/src/test/java/redis/clients/jedis/csc/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java @@ -5,7 +5,7 @@ import java.util.Map; import redis.clients.jedis.csc.hash.PrimitiveArrayHash; -public class MapCSC extends ClientSideCache { +public class MapClientSideCache extends ClientSideCache { private static final PrimitiveArrayHash HASHING = new PrimitiveArrayHash() { @@ -22,16 +22,16 @@ protected long hashBytes(byte[] bytes) { private final Map cache; - public MapCSC() { + public MapClientSideCache() { this(new HashMap<>()); } - public MapCSC(Map map) { + public MapClientSideCache(Map map) { super(HASHING); this.cache = map; } - public MapCSC(Map cache, ClientSideCacheable cacheable) { + public MapClientSideCache(Map cache, ClientSideCacheable cacheable) { super(HASHING, cacheable); this.cache = cache; } From 3960daead2682cd8e897ab28e32e0ec1b50b9398 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:56:59 +0600 Subject: [PATCH 3/6] Format imports --- .../java/redis/clients/jedis/csc/CaffeineClientSideCache.java | 2 +- src/main/java/redis/clients/jedis/csc/ClientSideCache.java | 3 ++- .../java/redis/clients/jedis/csc/GuavaClientSideCache.java | 2 +- src/test/java/redis/clients/jedis/csc/MapClientSideCache.java | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java index 0b9043e6282..eb80d5e705a 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java @@ -4,8 +4,8 @@ import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.OpenHftCommandHash; import redis.clients.jedis.csc.hash.CommandLongHash; +import redis.clients.jedis.csc.hash.OpenHftCommandHash; public class CaffeineClientSideCache extends ClientSideCache { diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 6ead3cee4d0..d598be70652 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -7,9 +7,10 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; + import redis.clients.jedis.CommandObject; -import redis.clients.jedis.util.SafeEncoder; import redis.clients.jedis.csc.hash.CommandLongHash; +import redis.clients.jedis.util.SafeEncoder; /** * The class to manage the client-side caching. User can provide any of implementation of this class to the client diff --git a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java index 0e15bf9e574..e25489bcedf 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java @@ -5,8 +5,8 @@ import com.google.common.hash.HashFunction; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.GuavaCommandHash; import redis.clients.jedis.csc.hash.CommandLongHash; +import redis.clients.jedis.csc.hash.GuavaCommandHash; public class GuavaClientSideCache extends ClientSideCache { diff --git a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java index dcdea2bf956..af5f0ab735b 100644 --- a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java +++ b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; + import redis.clients.jedis.csc.hash.PrimitiveArrayHash; public class MapClientSideCache extends ClientSideCache { From df1fdc62244195ddbfb1146ad55c6d230defe4c4 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:03:08 +0600 Subject: [PATCH 4/6] Fix javadoc --- src/main/java/redis/clients/jedis/csc/ClientSideCache.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index d598be70652..6b507fcb892 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -14,8 +14,8 @@ /** * The class to manage the client-side caching. User can provide any of implementation of this class to the client - * object; e.g. {@link redis.clients.jedis.csc.CaffeineCSC CaffeineCSC} or - * {@link redis.clients.jedis.csc.GuavaCSC GuavaCSC} or a custom implementation of their own. + * object; e.g. {@link redis.clients.jedis.csc.CaffeineClientSideCache CaffeineClientSideCache} or + * {@link redis.clients.jedis.csc.GuavaClientSideCache GuavaClientSideCache} or a custom implementation of their own. */ public abstract class ClientSideCache { From b6830b17a3f590eccf74d8d3837efce448006326 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sun, 10 Mar 2024 16:15:12 +0600 Subject: [PATCH 5/6] Rename to Hash'er's --- .../jedis/csc/CaffeineClientSideCache.java | 21 +++++++------ .../clients/jedis/csc/ClientSideCache.java | 14 ++++----- .../jedis/csc/GuavaClientSideCache.java | 30 +++++++++---------- ...ndHash.java => AbstractCommandHasher.java} | 2 +- .../jedis/csc/hash/CommandLongHash.java | 16 ---------- .../jedis/csc/hash/CommandLongHasher.java | 16 ++++++++++ ...mmandHash.java => GuavaCommandHasher.java} | 4 +-- ...andHash.java => OpenHftCommandHasher.java} | 4 +-- ....java => PrimitiveArrayCommandHasher.java} | 7 ++++- .../jedis/csc/ClientSideCacheLibsTest.java | 4 +-- .../clients/jedis/csc/MapClientSideCache.java | 4 +-- 11 files changed, 63 insertions(+), 59 deletions(-) rename src/main/java/redis/clients/jedis/csc/hash/{AbstractCommandHash.java => AbstractCommandHasher.java} (89%) delete mode 100644 src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java create mode 100644 src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java rename src/main/java/redis/clients/jedis/csc/hash/{GuavaCommandHash.java => GuavaCommandHasher.java} (84%) rename src/main/java/redis/clients/jedis/csc/hash/{OpenHftCommandHash.java => OpenHftCommandHasher.java} (77%) rename src/main/java/redis/clients/jedis/csc/hash/{PrimitiveArrayHash.java => PrimitiveArrayCommandHasher.java} (53%) diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java index eb80d5e705a..8ff993d6c75 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java @@ -4,23 +4,23 @@ import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.CommandLongHash; -import redis.clients.jedis.csc.hash.OpenHftCommandHash; +import redis.clients.jedis.csc.hash.CommandLongHasher; +import redis.clients.jedis.csc.hash.OpenHftCommandHasher; public class CaffeineClientSideCache extends ClientSideCache { private final Cache cache; public CaffeineClientSideCache(Cache caffeineCache) { - this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE); + this(caffeineCache, DefaultClientSideCacheable.INSTANCE); } public CaffeineClientSideCache(Cache caffeineCache, ClientSideCacheable cacheable) { - this(caffeineCache, new OpenHftCommandHash(OpenHftCommandHash.DEFAULT_HASH_FUNCTION), cacheable); + this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable); } - public CaffeineClientSideCache(Cache caffeineCache, CommandLongHash hashing, ClientSideCacheable cacheable) { - super(hashing, cacheable); + public CaffeineClientSideCache(Cache caffeineCache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) { + super(commandHasher, cacheable); this.cache = caffeineCache; } @@ -55,7 +55,7 @@ public static class Builder { private final TimeUnit expireTimeUnit = TimeUnit.SECONDS; // not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)' - private CommandLongHash longHashing = null; + private CommandLongHasher commandHasher = null; private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; @@ -71,8 +71,8 @@ public Builder ttl(int seconds) { return this; } - public Builder hash(CommandLongHash hashing) { - this.longHashing = hashing; + public Builder commandHasher(CommandLongHasher commandHasher) { + this.commandHasher = commandHasher; return this; } @@ -88,8 +88,7 @@ public CaffeineClientSideCache build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return longHashing != null - ? new CaffeineClientSideCache(cb.build(), longHashing, cacheable) + return commandHasher != null ? new CaffeineClientSideCache(cb.build(), commandHasher, cacheable) : new CaffeineClientSideCache(cb.build(), cacheable); } } diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 6b507fcb892..d0ad06e6a46 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -9,7 +9,7 @@ import java.util.function.Function; import redis.clients.jedis.CommandObject; -import redis.clients.jedis.csc.hash.CommandLongHash; +import redis.clients.jedis.csc.hash.CommandLongHasher; import redis.clients.jedis.util.SafeEncoder; /** @@ -23,15 +23,15 @@ public abstract class ClientSideCache { protected static final int DEFAULT_EXPIRE_SECONDS = 100; private final Map> keyToCommandHashes = new ConcurrentHashMap<>(); - private final CommandLongHash commandHashing; + private final CommandLongHasher commandHasher; private final ClientSideCacheable cacheable; - protected ClientSideCache(CommandLongHash commandHashing) { - this(commandHashing, DefaultClientSideCacheable.INSTANCE); + protected ClientSideCache(CommandLongHasher commandHasher) { + this(commandHasher, DefaultClientSideCacheable.INSTANCE); } - protected ClientSideCache(CommandLongHash commandHashing, ClientSideCacheable cacheable) { - this.commandHashing = commandHashing; + protected ClientSideCache(CommandLongHasher commandHasher, ClientSideCacheable cacheable) { + this.commandHasher = commandHasher; this.cacheable = cacheable; } @@ -81,7 +81,7 @@ public final T get(Function, T> loader, CommandObject co return loader.apply(command); } - final long hash = commandHashing.hash(command); + final long hash = commandHasher.hash(command); T value = (T) getValue(hash); if (value != null) { diff --git a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java index e25489bcedf..76446db52a9 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java @@ -5,32 +5,32 @@ import com.google.common.hash.HashFunction; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.hash.CommandLongHash; -import redis.clients.jedis.csc.hash.GuavaCommandHash; +import redis.clients.jedis.csc.hash.CommandLongHasher; +import redis.clients.jedis.csc.hash.GuavaCommandHasher; public class GuavaClientSideCache extends ClientSideCache { private final Cache cache; public GuavaClientSideCache(Cache guavaCache) { - this(guavaCache, GuavaCommandHash.DEFAULT_HASH_FUNCTION); + this(guavaCache, GuavaCommandHasher.DEFAULT_HASH_FUNCTION); } public GuavaClientSideCache(Cache guavaCache, HashFunction hashFunction) { - this(guavaCache, new GuavaCommandHash(hashFunction)); + this(guavaCache, new GuavaCommandHasher(hashFunction)); } - public GuavaClientSideCache(Cache guavaCache, CommandLongHash hashing) { - super(hashing); + public GuavaClientSideCache(Cache guavaCache, CommandLongHasher commandHasher) { + super(commandHasher); this.cache = guavaCache; } public GuavaClientSideCache(Cache guavaCache, ClientSideCacheable cacheable) { - this(guavaCache, new GuavaCommandHash(GuavaCommandHash.DEFAULT_HASH_FUNCTION), cacheable); + this(guavaCache, new GuavaCommandHasher(GuavaCommandHasher.DEFAULT_HASH_FUNCTION), cacheable); } - public GuavaClientSideCache(Cache cache, CommandLongHash hashing, ClientSideCacheable cacheable) { - super(hashing, cacheable); + public GuavaClientSideCache(Cache cache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) { + super(commandHasher, cacheable); this.cache = cache; } @@ -66,7 +66,7 @@ public static class Builder { // not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)' private HashFunction hashFunction = null; - private CommandLongHash longHashing = null; + private CommandLongHasher commandHasher = null; private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; @@ -84,12 +84,12 @@ public Builder ttl(int seconds) { public Builder hashFunction(HashFunction function) { this.hashFunction = function; - this.longHashing = null; + this.commandHasher = null; return this; } - public Builder hash(CommandLongHash hashing) { - this.longHashing = hashing; + public Builder commandHasher(CommandLongHasher commandHasher) { + this.commandHasher = commandHasher; this.hashFunction = null; return this; } @@ -106,8 +106,8 @@ public GuavaClientSideCache build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return longHashing != null ? new GuavaClientSideCache(cb.build(), longHashing, cacheable) - : hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHash(hashFunction), cacheable) + return commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher, cacheable) + : hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction), cacheable) : new GuavaClientSideCache(cb.build(), cacheable); } } diff --git a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java b/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java similarity index 89% rename from src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java rename to src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java index 1acdf0dc064..2c71e4ea809 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHash.java +++ b/src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java @@ -4,7 +4,7 @@ import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; -public abstract class AbstractCommandHash implements CommandLongHash { +public abstract class AbstractCommandHasher implements CommandLongHasher { @Override public final long hash(CommandObject command) { diff --git a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java deleted file mode 100644 index 4e03f376288..00000000000 --- a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHash.java +++ /dev/null @@ -1,16 +0,0 @@ -package redis.clients.jedis.csc.hash; - -import redis.clients.jedis.CommandObject; - -/** - * The interface for hashing a command object for client-side caching. - */ -public interface CommandLongHash { - - /** - * Produce a 64-bit signed hash from a command object. - * @param command the command object - * @return 64-bit signed hash - */ - long hash(CommandObject command); -} diff --git a/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java new file mode 100644 index 00000000000..bb0b03e0721 --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/hash/CommandLongHasher.java @@ -0,0 +1,16 @@ +package redis.clients.jedis.csc.hash; + +import redis.clients.jedis.CommandObject; + +/** + * The interface for hashing a command object to support client-side caching. + */ +public interface CommandLongHasher { + + /** + * Produce a 64-bit signed hash value from a command object. + * @param command the command object + * @return 64-bit signed hash value + */ + long hash(CommandObject command); +} diff --git a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java similarity index 84% rename from src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java rename to src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java index 7249e8d3b34..8c072452730 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHash.java +++ b/src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java @@ -4,13 +4,13 @@ import com.google.common.hash.Hasher; import redis.clients.jedis.CommandObject; -public class GuavaCommandHash implements CommandLongHash { +public class GuavaCommandHasher implements CommandLongHasher { public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); private final HashFunction function; - public GuavaCommandHash(HashFunction function) { + public GuavaCommandHasher(HashFunction function) { this.function = function; } diff --git a/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java b/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java similarity index 77% rename from src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java rename to src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java index 0a38115987d..e2a7bced3db 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHash.java +++ b/src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java @@ -2,13 +2,13 @@ import net.openhft.hashing.LongHashFunction; -public class OpenHftCommandHash extends PrimitiveArrayHash implements CommandLongHash { +public class OpenHftCommandHasher extends PrimitiveArrayCommandHasher implements CommandLongHasher { public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); private final LongHashFunction function; - public OpenHftCommandHash(LongHashFunction function) { + public OpenHftCommandHasher(LongHashFunction function) { this.function = function; } diff --git a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java b/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java similarity index 53% rename from src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java rename to src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java index 671a227bfb4..ed4a9ff97c1 100644 --- a/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHash.java +++ b/src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java @@ -3,7 +3,12 @@ import redis.clients.jedis.Builder; import redis.clients.jedis.args.Rawable; -public abstract class PrimitiveArrayHash extends AbstractCommandHash { +/** + * It is possible to extend {@link PrimitiveArrayCommandHasher this abstract class} in order to implement + * {@link CommandLongHasher} as {@link PrimitiveArrayCommandHasher#hashLongs(long[])} and + * {@link PrimitiveArrayCommandHasher#hashBytes(byte[])} can be supported by almost all Java hashing libraries. + */ +public abstract class PrimitiveArrayCommandHasher extends AbstractCommandHasher { @Override protected final long hashRawable(Rawable raw) { diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index 2880a53ef16..189332b2602 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -23,7 +23,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPooled; -import redis.clients.jedis.csc.hash.OpenHftCommandHash; +import redis.clients.jedis.csc.hash.OpenHftCommandHasher; public class ClientSideCacheLibsTest { @@ -93,7 +93,7 @@ public void guavaMore() { @Test public void caffeineSimple() { CaffeineClientSideCache caffeine = CaffeineClientSideCache.builder().maximumSize(10).ttl(10) - .hash(new OpenHftCommandHash(LongHashFunction.xx())).build(); + .commandHasher(new OpenHftCommandHasher(LongHashFunction.xx())).build(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java index af5f0ab735b..4ff4e4367a2 100644 --- a/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java +++ b/src/test/java/redis/clients/jedis/csc/MapClientSideCache.java @@ -4,11 +4,11 @@ import java.util.HashMap; import java.util.Map; -import redis.clients.jedis.csc.hash.PrimitiveArrayHash; +import redis.clients.jedis.csc.hash.PrimitiveArrayCommandHasher; public class MapClientSideCache extends ClientSideCache { - private static final PrimitiveArrayHash HASHING = new PrimitiveArrayHash() { + private static final PrimitiveArrayCommandHasher HASHING = new PrimitiveArrayCommandHasher() { @Override protected long hashLongs(long[] longs) { From e5a7659452ae260a8b1b11f8bdd012014bde0ad8 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sun, 10 Mar 2024 16:47:11 +0600 Subject: [PATCH 6/6] Guava HashFunction is preferred over CommandLongHasher --- .../java/redis/clients/jedis/csc/GuavaClientSideCache.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java index 76446db52a9..ca176e8b33e 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java @@ -106,8 +106,8 @@ public GuavaClientSideCache build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher, cacheable) - : hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction), cacheable) + return hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction), cacheable) + : commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher, cacheable) : new GuavaClientSideCache(cb.build(), cacheable); } }