From 67fd52904f3ce2e539aa23045c9785cec50c1735 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 17:33:39 +0600 Subject: [PATCH 1/7] Create csc package --- src/main/java/redis/clients/jedis/Connection.java | 1 + src/main/java/redis/clients/jedis/ConnectionFactory.java | 2 +- src/main/java/redis/clients/jedis/ConnectionPool.java | 1 + src/main/java/redis/clients/jedis/JedisCluster.java | 1 + .../java/redis/clients/jedis/JedisClusterInfoCache.java | 1 + src/main/java/redis/clients/jedis/JedisPooled.java | 1 + src/main/java/redis/clients/jedis/JedisSentineled.java | 1 + src/main/java/redis/clients/jedis/Protocol.java | 1 + src/main/java/redis/clients/jedis/UnifiedJedis.java | 1 + .../redis/clients/jedis/{ => csc}/ClientSideCache.java | 7 ++++--- .../clients/jedis/providers/ClusterConnectionProvider.java | 2 +- .../clients/jedis/providers/PooledConnectionProvider.java | 2 +- .../jedis/providers/SentineledConnectionProvider.java | 2 +- src/main/java/redis/clients/jedis/util/CaffeineCSC.java | 2 +- src/main/java/redis/clients/jedis/util/GuavaCSC.java | 2 +- src/main/java/redis/clients/jedis/util/JedisURIHelper.java | 2 +- src/test/java/redis/clients/jedis/util/MapCSC.java | 2 +- 17 files changed, 20 insertions(+), 11 deletions(-) rename src/main/java/redis/clients/jedis/{ => csc}/ClientSideCache.java (93%) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index bff5898f8da..b1dbcaf86b1 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -17,6 +17,7 @@ import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.args.ClientAttributeOption; import redis.clients.jedis.args.Rawable; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; diff --git a/src/main/java/redis/clients/jedis/ConnectionFactory.java b/src/main/java/redis/clients/jedis/ConnectionFactory.java index 5b436062052..ecfceb77ef1 100644 --- a/src/main/java/redis/clients/jedis/ConnectionFactory.java +++ b/src/main/java/redis/clients/jedis/ConnectionFactory.java @@ -6,7 +6,7 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisException; /** diff --git a/src/main/java/redis/clients/jedis/ConnectionPool.java b/src/main/java/redis/clients/jedis/ConnectionPool.java index 70202deeae9..59f416649ce 100644 --- a/src/main/java/redis/clients/jedis/ConnectionPool.java +++ b/src/main/java/redis/clients/jedis/ConnectionPool.java @@ -2,6 +2,7 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.util.Pool; public class ConnectionPool extends Pool { diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 0138058d660..bd7f8d017c8 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -6,6 +6,7 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.providers.ClusterConnectionProvider; import redis.clients.jedis.util.JedisClusterCRC16; diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 5646dbfb59c..482fe12cb5e 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -21,6 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.SafeEncoder; diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index a5840c6530c..4bda1d1ebb2 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -7,6 +7,7 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.providers.PooledConnectionProvider; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.Pool; diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index f1cb8ea6509..164c665d3f0 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -2,6 +2,7 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.providers.SentineledConnectionProvider; public class JedisSentineled extends UnifiedJedis { diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index 4bd82fec1ef..dc84974db77 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -11,6 +11,7 @@ import redis.clients.jedis.exceptions.*; import redis.clients.jedis.args.Rawable; import redis.clients.jedis.commands.ProtocolCommand; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.util.KeyValue; import redis.clients.jedis.util.RedisInputStream; import redis.clients.jedis.util.RedisOutputStream; diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 37c4316ec05..80f6897fbae 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -17,6 +17,7 @@ import redis.clients.jedis.commands.SampleBinaryKeyedCommands; import redis.clients.jedis.commands.SampleKeyedCommands; import redis.clients.jedis.commands.RedisModuleCommands; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.executors.*; import redis.clients.jedis.gears.TFunctionListParams; diff --git a/src/main/java/redis/clients/jedis/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java similarity index 93% rename from src/main/java/redis/clients/jedis/ClientSideCache.java rename to src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 389b795814d..376d3d673ac 100644 --- a/src/main/java/redis/clients/jedis/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -1,4 +1,4 @@ -package redis.clients.jedis; +package redis.clients.jedis.csc; import java.nio.ByteBuffer; import java.util.HashSet; @@ -8,6 +8,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; +import redis.clients.jedis.CommandObject; import redis.clients.jedis.util.SafeEncoder; /** @@ -40,7 +41,7 @@ public final void clear() { invalidateAllKeysAndCommandHashes(); } - final void invalidate(List list) { + public final void invalidate(List list) { if (list == null) { invalidateAllKeysAndCommandHashes(); return; @@ -68,7 +69,7 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) { } } - final T getValue(Function, T> loader, CommandObject command, Object... keys) { + public final T getValue(Function, T> loader, CommandObject command, Object... keys) { final long hash = getCommandHash(command); diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index be83aa73888..6b148a5e773 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -8,7 +8,6 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.ClientSideCache; import redis.clients.jedis.ClusterCommandArguments; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.HostAndPort; @@ -16,6 +15,7 @@ import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.JedisClusterInfoCache; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; diff --git a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java index 85fa3cecd29..0d85fabad29 100644 --- a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java @@ -5,13 +5,13 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.ClientSideCache; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionFactory; import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.util.Pool; public class PooledConnectionProvider implements ConnectionProvider { diff --git a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java index e335803b628..1d7c5790c21 100644 --- a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java @@ -10,7 +10,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.ClientSideCache; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPool; @@ -18,6 +17,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.IOUtils; diff --git a/src/main/java/redis/clients/jedis/util/CaffeineCSC.java b/src/main/java/redis/clients/jedis/util/CaffeineCSC.java index 3bce3504b38..dbe4604e8f3 100644 --- a/src/main/java/redis/clients/jedis/util/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/util/CaffeineCSC.java @@ -4,7 +4,7 @@ import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; import net.openhft.hashing.LongHashFunction; -import redis.clients.jedis.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; diff --git a/src/main/java/redis/clients/jedis/util/GuavaCSC.java b/src/main/java/redis/clients/jedis/util/GuavaCSC.java index d9973b7dc60..8c3614c4eb1 100644 --- a/src/main/java/redis/clients/jedis/util/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/util/GuavaCSC.java @@ -5,7 +5,7 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hasher; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.CommandObject; public class GuavaCSC extends ClientSideCache { diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 2d73cf04d4d..68d1228d858 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -1,7 +1,7 @@ package redis.clients.jedis.util; import java.net.URI; -import redis.clients.jedis.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; diff --git a/src/test/java/redis/clients/jedis/util/MapCSC.java b/src/test/java/redis/clients/jedis/util/MapCSC.java index eb229036eae..f693c838334 100644 --- a/src/test/java/redis/clients/jedis/util/MapCSC.java +++ b/src/test/java/redis/clients/jedis/util/MapCSC.java @@ -3,9 +3,9 @@ import java.util.Arrays; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import redis.clients.jedis.ClientSideCache; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; +import redis.clients.jedis.csc.ClientSideCache; public class MapCSC extends ClientSideCache { From 9e49f4babc8094534ca306adc0585eb7c1f66db0 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 17:36:27 +0600 Subject: [PATCH 2/7] Create csc.util package --- src/main/java/redis/clients/jedis/csc/ClientSideCache.java | 5 ++--- .../java/redis/clients/jedis/{ => csc}/util/CaffeineCSC.java | 2 +- .../java/redis/clients/jedis/{ => csc}/util/GuavaCSC.java | 2 +- src/main/java/redis/clients/jedis/util/JedisURIHelper.java | 2 ++ 4 files changed, 6 insertions(+), 5 deletions(-) rename src/main/java/redis/clients/jedis/{ => csc}/util/CaffeineCSC.java (98%) rename src/main/java/redis/clients/jedis/{ => csc}/util/GuavaCSC.java (98%) diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 376d3d673ac..cc5b697d7e4 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -6,15 +6,14 @@ import java.util.Map; 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; /** * 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.util.CaffeineCSC CaffeineCSC} or - * {@link redis.clients.jedis.util.GuavaCSC GuavaCSC} or a custom implementation of their own. + * object; e.g. {@link redis.clients.jedis.csc.util.CaffeineCSC CaffeineCSC} or + * {@link redis.clients.jedis.csc.util.GuavaCSC GuavaCSC} or a custom implementation of their own. */ public abstract class ClientSideCache { diff --git a/src/main/java/redis/clients/jedis/util/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java similarity index 98% rename from src/main/java/redis/clients/jedis/util/CaffeineCSC.java rename to src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java index dbe4604e8f3..735e0fff2d4 100644 --- a/src/main/java/redis/clients/jedis/util/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.util; +package redis.clients.jedis.csc.util; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; diff --git a/src/main/java/redis/clients/jedis/util/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java similarity index 98% rename from src/main/java/redis/clients/jedis/util/GuavaCSC.java rename to src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java index 8c3614c4eb1..9a60792d253 100644 --- a/src/main/java/redis/clients/jedis/util/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.util; +package redis.clients.jedis.csc.util; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 68d1228d858..642973a1321 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -5,6 +5,8 @@ import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; +import redis.clients.jedis.csc.util.GuavaCSC; +import redis.clients.jedis.csc.util.CaffeineCSC; public final class JedisURIHelper { From b30651148dc8bdc7ac7ed4a287473bc6120e2766 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 18:44:22 +0600 Subject: [PATCH 3/7] Create a config interface for client-side caching --- .../java/redis/clients/jedis/Connection.java | 8 +++---- .../clients/jedis/ConnectionFactory.java | 6 ++--- .../redis/clients/jedis/ConnectionPool.java | 6 ++--- .../redis/clients/jedis/JedisCluster.java | 14 ++++++------ .../clients/jedis/JedisClusterInfoCache.java | 12 +++++----- .../java/redis/clients/jedis/JedisPooled.java | 6 ++--- .../redis/clients/jedis/JedisSentineled.java | 6 ++--- .../java/redis/clients/jedis/Protocol.java | 7 +++--- .../redis/clients/jedis/UnifiedJedis.java | 22 ++++++++++--------- .../clients/jedis/csc/ClientSideCache.java | 22 +++++++++---------- .../jedis/csc/ClientSideCacheConfig.java | 13 +++++++++++ .../csc/DefaultClientSideCacheConfig.java | 22 +++++++++++++++++++ .../clients/jedis/csc/util/CaffeineCSC.java | 12 +++++----- .../clients/jedis/csc/util/GuavaCSC.java | 12 +++++----- .../providers/ClusterConnectionProvider.java | 8 +++---- .../providers/PooledConnectionProvider.java | 6 ++--- .../SentineledConnectionProvider.java | 12 +++++----- .../clients/jedis/util/JedisURIHelper.java | 10 +++++---- .../JedisClusterClientSideCacheTest.java | 11 ++++++---- .../jedis/JedisPooledClientSideCacheTest.java | 9 ++++---- .../JedisSentineledClientSideCacheTest.java | 13 +++++++---- .../java/redis/clients/jedis/util/MapCSC.java | 10 ++++----- 22 files changed, 148 insertions(+), 99 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java create mode 100644 src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index b1dbcaf86b1..dddd2270ca6 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -17,7 +17,7 @@ import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.args.ClientAttributeOption; import redis.clients.jedis.args.Rawable; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; @@ -35,7 +35,7 @@ public class Connection implements Closeable { private Socket socket; private RedisOutputStream outputStream; private RedisInputStream inputStream; - private ClientSideCache clientSideCache; + private ClientSideCacheConfig clientSideCache; private int soTimeout = 0; private int infiniteSoTimeout = 0; private boolean broken = false; @@ -67,7 +67,7 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie initializeConnection(clientConfig); } - public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, ClientSideCache csCache) { + public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { this.socketFactory = socketFactory; this.soTimeout = clientConfig.getSocketTimeoutMillis(); this.infiniteSoTimeout = clientConfig.getBlockingSocketTimeoutMillis(); @@ -520,7 +520,7 @@ public boolean ping() { return true; } - private void initializeClientSideCache(ClientSideCache csCache) { + private void initializeClientSideCache(ClientSideCacheConfig csCache) { this.clientSideCache = csCache; if (clientSideCache != null) { if (protocol != RedisProtocol.RESP3) { diff --git a/src/main/java/redis/clients/jedis/ConnectionFactory.java b/src/main/java/redis/clients/jedis/ConnectionFactory.java index ecfceb77ef1..190b4b53dd1 100644 --- a/src/main/java/redis/clients/jedis/ConnectionFactory.java +++ b/src/main/java/redis/clients/jedis/ConnectionFactory.java @@ -6,7 +6,7 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisException; /** @@ -18,7 +18,7 @@ public class ConnectionFactory implements PooledObjectFactory { private final JedisSocketFactory jedisSocketFactory; private final JedisClientConfig clientConfig; - private ClientSideCache clientSideCache = null; + private ClientSideCacheConfig clientSideCache = null; public ConnectionFactory(final HostAndPort hostAndPort) { this.clientConfig = DefaultJedisClientConfig.builder().build(); @@ -30,7 +30,7 @@ public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig); } - public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache) { + public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { this.clientConfig = clientConfig; this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig); this.clientSideCache = csCache; diff --git a/src/main/java/redis/clients/jedis/ConnectionPool.java b/src/main/java/redis/clients/jedis/ConnectionPool.java index 59f416649ce..fe04cfdd64c 100644 --- a/src/main/java/redis/clients/jedis/ConnectionPool.java +++ b/src/main/java/redis/clients/jedis/ConnectionPool.java @@ -2,7 +2,7 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.util.Pool; public class ConnectionPool extends Pool { @@ -11,7 +11,7 @@ public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this(new ConnectionFactory(hostAndPort, clientConfig)); } - public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache csCache) { + public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { this(new ConnectionFactory(hostAndPort, clientConfig, csCache)); } @@ -24,7 +24,7 @@ public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, this(new ConnectionFactory(hostAndPort, clientConfig), poolConfig); } - public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache csCache, + public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig csCache, GenericObjectPoolConfig poolConfig) { this(new ConnectionFactory(hostAndPort, clientConfig, csCache), poolConfig); } diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index bd7f8d017c8..8b1019ba4d3 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -6,7 +6,7 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.ClusterConnectionProvider; import redis.clients.jedis.util.JedisClusterCRC16; @@ -216,31 +216,31 @@ private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Durati super(provider, maxAttempts, maxTotalRetriesDuration, protocol); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache) { + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache) { this(clusterNodes, clientConfig, clientSideCache, DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis())); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, GenericObjectPoolConfig poolConfig) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig), DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis()), clientConfig.getRedisProtocol(), clientSideCache); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, GenericObjectPoolConfig poolConfig, Duration topologyRefreshPeriod, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig, topologyRefreshPeriod), @@ -248,7 +248,7 @@ public JedisCluster(Set clusterNodes, JedisClientConfig clientConfi } private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, - RedisProtocol protocol, ClientSideCache clientSideCache) { + RedisProtocol protocol, ClientSideCacheConfig clientSideCache) { super(provider, maxAttempts, maxTotalRetriesDuration, protocol, clientSideCache); } diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 482fe12cb5e..233e3e4538b 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -21,7 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.SafeEncoder; @@ -43,7 +43,7 @@ public class JedisClusterInfoCache { private final GenericObjectPoolConfig poolConfig; private final JedisClientConfig clientConfig; - private final ClientSideCache clientSideCache; + private final ClientSideCacheConfig clientSideCache; private final Set startNodes; private static final int MASTER_NODE_INDEX = 2; @@ -66,7 +66,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig, final Set startNodes) { + public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCacheConfig csCache, final Set startNodes) { this(clientConfig, csCache, null, startNodes); } @@ -75,7 +75,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig, this(clientConfig, null, poolConfig, startNodes); } - public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache csCache, + public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCacheConfig csCache, final GenericObjectPoolConfig poolConfig, final Set startNodes) { this(clientConfig, csCache, poolConfig, startNodes, null); } @@ -86,7 +86,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig, this(clientConfig, null, poolConfig, startNodes, topologyRefreshPeriod); } - public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache csCache, + public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCacheConfig csCache, final GenericObjectPoolConfig poolConfig, final Set startNodes, final Duration topologyRefreshPeriod) { this.poolConfig = poolConfig; @@ -228,7 +228,7 @@ private void discoverClusterSlots(Connection jedis) { Arrays.fill(slots, null); Arrays.fill(slotNodes, null); if (clientSideCache != null) { - clientSideCache.clear(); + clientSideCache.getClientSideCache().clear(); } Set hostAndPortKeys = new HashSet<>(); diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index 4bda1d1ebb2..ed6ea9c9c00 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -7,7 +7,7 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.PooledConnectionProvider; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.Pool; @@ -76,7 +76,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client super(hostAndPort, clientConfig); } - public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache) { + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { super(hostAndPort, clientConfig, csCache); } @@ -380,7 +380,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client super(new PooledConnectionProvider(hostAndPort, clientConfig, poolConfig), clientConfig.getRedisProtocol()); } - public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache, + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCacheConfig csCache, final GenericObjectPoolConfig poolConfig) { super(new PooledConnectionProvider(hostAndPort, clientConfig, csCache, poolConfig), clientConfig.getRedisProtocol(), csCache); diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index 164c665d3f0..7643865c642 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -2,7 +2,7 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.SentineledConnectionProvider; public class JedisSentineled extends UnifiedJedis { @@ -13,7 +13,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo masterClientConfig.getRedisProtocol()); } - public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCacheConfig clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, sentinels, sentinelClientConfig), masterClientConfig.getRedisProtocol(), clientSideCache); @@ -26,7 +26,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo masterClientConfig.getRedisProtocol()); } - public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCacheConfig 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/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java index dc84974db77..4451504421c 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -12,6 +12,7 @@ import redis.clients.jedis.args.Rawable; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.util.KeyValue; import redis.clients.jedis.util.RedisInputStream; import redis.clients.jedis.util.RedisOutputStream; @@ -229,17 +230,17 @@ public static Object read(final RedisInputStream is) { return process(is); } - public static Object read(final RedisInputStream is, final ClientSideCache cache) { + public static Object read(final RedisInputStream is, final ClientSideCacheConfig cache) { readPushes(is, cache); return process(is); } - private static void readPushes(final RedisInputStream is, final ClientSideCache cache) { + private static void readPushes(final RedisInputStream is, final ClientSideCacheConfig cache) { if (cache != null) { //System.out.println("PEEK: " + is.peekByte()); while (is.peek(GREATER_THAN_BYTE)) { is.readByte(); - processPush(is, cache); + processPush(is, cache.getClientSideCache()); } } } diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 80f6897fbae..395788ec060 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -17,7 +17,7 @@ import redis.clients.jedis.commands.SampleBinaryKeyedCommands; import redis.clients.jedis.commands.SampleKeyedCommands; import redis.clients.jedis.commands.RedisModuleCommands; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.executors.*; import redis.clients.jedis.gears.TFunctionListParams; @@ -50,7 +50,7 @@ public class UnifiedJedis implements JedisCommands, JedisBinaryCommands, AutoCloseable { @Deprecated protected RedisProtocol protocol = null; - private final ClientSideCache clientSideCache; + private final ClientSideCacheConfig clientSideCache; protected final ConnectionProvider provider; protected final CommandExecutor executor; protected final CommandObjects commandObjects; @@ -93,7 +93,7 @@ public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this(new PooledConnectionProvider(hostAndPort, clientConfig), clientConfig.getRedisProtocol()); } - public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache) { + public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache) { this(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache), clientConfig.getRedisProtocol(), clientSideCache); } @@ -105,7 +105,7 @@ protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol) { this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol); } - protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, ClientSideCache clientSideCache) { + protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, ClientSideCacheConfig clientSideCache) { this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol, clientSideCache); } @@ -177,7 +177,7 @@ protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Dura } protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration, - RedisProtocol protocol, ClientSideCache clientSideCache) { + RedisProtocol protocol, ClientSideCacheConfig clientSideCache) { this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider, new ClusterCommandObjects(), protocol, clientSideCache); } @@ -242,11 +242,11 @@ private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, Comm private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects, RedisProtocol protocol) { - this(executor, provider, commandObjects, protocol, (ClientSideCache) null); + this(executor, provider, commandObjects, protocol, (ClientSideCacheConfig) null); } private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects, - RedisProtocol protocol, ClientSideCache clientSideCache) { + RedisProtocol protocol, ClientSideCacheConfig clientSideCache) { if (clientSideCache != null && protocol != RedisProtocol.RESP3) { throw new IllegalArgumentException("Client-side caching is only supported with RESP3."); @@ -301,11 +301,13 @@ public void setBroadcastAndRoundRobinConfig(JedisBroadcastAndRoundRobinConfig co } private T checkAndClientSideCacheCommand(CommandObject command, Object... keys) { - if (clientSideCache == null) { - return executeCommand(command); + if (clientSideCache != null) { + if (clientSideCache.isCacheable(command.getArguments().getCommand(), keys)) { + return clientSideCache.getClientSideCache().get((cmd) -> executeCommand(cmd), command, keys); + } } - return clientSideCache.getValue((cmd) -> executeCommand(cmd), command, keys); + return executeCommand(command); } public String ping() { diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index cc5b697d7e4..cb52e247d83 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -26,15 +26,15 @@ protected ClientSideCache() { this.keyToCommandHashes = new ConcurrentHashMap<>(); } - protected abstract void invalidateAllCommandHashes(); + protected abstract void invalidateAllHashes(); - protected abstract void invalidateCommandHashes(Iterable hashes); + protected abstract void invalidateHashes(Iterable hashes); - protected abstract void put(long hash, Object value); + protected abstract void putValue(long hash, Object value); - protected abstract Object get(long hash); + protected abstract Object getValue(long hash); - protected abstract long getCommandHash(CommandObject command); + protected abstract long getHash(CommandObject command); public final void clear() { invalidateAllKeysAndCommandHashes(); @@ -50,7 +50,7 @@ public final void invalidate(List list) { } private void invalidateAllKeysAndCommandHashes() { - invalidateAllCommandHashes(); + invalidateAllHashes(); keyToCommandHashes.clear(); } @@ -63,23 +63,23 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) { Set hashes = keyToCommandHashes.get(mapKey); if (hashes != null) { - invalidateCommandHashes(hashes); + invalidateHashes(hashes); keyToCommandHashes.remove(mapKey); } } - public final T getValue(Function, T> loader, CommandObject command, Object... keys) { + public final T get(Function, T> loader, CommandObject command, Object... keys) { - final long hash = getCommandHash(command); + final long hash = getHash(command); - T value = (T) get(hash); + T value = (T) getValue(hash); if (value != null) { return value; } value = loader.apply(command); if (value != null) { - put(hash, value); + putValue(hash, value); for (Object key : keys) { ByteBuffer mapKey = makeKeyForKeyToCommandHashes(key); if (keyToCommandHashes.containsKey(mapKey)) { diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java new file mode 100644 index 00000000000..caee090937b --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java @@ -0,0 +1,13 @@ +package redis.clients.jedis.csc; + +import redis.clients.jedis.commands.ProtocolCommand; + +public interface ClientSideCacheConfig { + + /** + * MUST NOT be {@code null}. + */ + ClientSideCache getClientSideCache(); + + boolean isCacheable(ProtocolCommand command, Object... keys); +} diff --git a/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java new file mode 100644 index 00000000000..2fae4200299 --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java @@ -0,0 +1,22 @@ +package redis.clients.jedis.csc; + +import redis.clients.jedis.commands.ProtocolCommand; + +public class DefaultClientSideCacheConfig implements ClientSideCacheConfig { + + private final ClientSideCache csCache; + + public DefaultClientSideCacheConfig(ClientSideCache clientSideCache) { + this.csCache = clientSideCache; + } + + @Override + public ClientSideCache getClientSideCache() { + return this.csCache; + } + + @Override + public boolean isCacheable(ProtocolCommand command, Object... keys) { + return true; + } +} diff --git a/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java index 735e0fff2d4..861972142e9 100644 --- a/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java @@ -4,9 +4,9 @@ import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; import net.openhft.hashing.LongHashFunction; -import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; +import redis.clients.jedis.csc.ClientSideCache; public class CaffeineCSC extends ClientSideCache { @@ -21,27 +21,27 @@ public CaffeineCSC(Cache caffeineCache, LongHashFunction hashFunct } @Override - protected final void invalidateAllCommandHashes() { + protected final void invalidateAllHashes() { cache.invalidateAll(); } @Override - protected void invalidateCommandHashes(Iterable hashes) { + protected void invalidateHashes(Iterable hashes) { cache.invalidateAll(hashes); } @Override - protected void put(long hash, Object value) { + protected void putValue(long hash, Object value) { cache.put(hash, value); } @Override - protected Object get(long hash) { + protected Object getValue(long hash) { return cache.getIfPresent(hash); } @Override - protected final long getCommandHash(CommandObject command) { + protected final long getHash(CommandObject command) { long[] nums = new long[command.getArguments().size() + 1]; int idx = 0; for (Rawable raw : command.getArguments()) { diff --git a/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java index 9a60792d253..1c6957c4d53 100644 --- a/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java @@ -5,8 +5,8 @@ import com.google.common.hash.HashFunction; import com.google.common.hash.Hasher; import java.util.concurrent.TimeUnit; -import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.CommandObject; +import redis.clients.jedis.csc.ClientSideCache; public class GuavaCSC extends ClientSideCache { @@ -21,27 +21,27 @@ public GuavaCSC(Cache guavaCache, HashFunction hashFunction) { } @Override - protected final void invalidateAllCommandHashes() { + protected final void invalidateAllHashes() { cache.invalidateAll(); } @Override - protected void invalidateCommandHashes(Iterable hashes) { + protected void invalidateHashes(Iterable hashes) { cache.invalidateAll(hashes); } @Override - protected void put(long hash, Object value) { + protected void putValue(long hash, Object value) { cache.put(hash, value); } @Override - protected Object get(long hash) { + protected Object getValue(long hash) { return cache.getIfPresent(hash); } @Override - protected final long getCommandHash(CommandObject command) { + protected final long getHash(CommandObject command) { Hasher hasher = function.newHasher(); command.getArguments().forEach(raw -> hasher.putBytes(raw.getRaw())); hasher.putInt(command.getBuilder().hashCode()); diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index 6b148a5e773..b4961535846 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -15,7 +15,7 @@ import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.JedisClusterInfoCache; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; @@ -30,7 +30,7 @@ public ClusterConnectionProvider(Set clusterNodes, JedisClientConfi initializeSlotsCache(clusterNodes, clientConfig); } - public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache csCache) { + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { this.cache = new JedisClusterInfoCache(clientConfig, csCache, clusterNodes); initializeSlotsCache(clusterNodes, clientConfig); } @@ -41,7 +41,7 @@ public ClusterConnectionProvider(Set clusterNodes, JedisClientConfi initializeSlotsCache(clusterNodes, clientConfig); } - public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache csCache, + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig csCache, GenericObjectPoolConfig poolConfig) { this.cache = new JedisClusterInfoCache(clientConfig, csCache, poolConfig, clusterNodes); initializeSlotsCache(clusterNodes, clientConfig); @@ -53,7 +53,7 @@ public ClusterConnectionProvider(Set clusterNodes, JedisClientConfi initializeSlotsCache(clusterNodes, clientConfig); } - public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache csCache, + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig csCache, GenericObjectPoolConfig poolConfig, Duration topologyRefreshPeriod) { this.cache = new JedisClusterInfoCache(clientConfig, csCache, poolConfig, clusterNodes, topologyRefreshPeriod); initializeSlotsCache(clusterNodes, clientConfig); diff --git a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java index 0d85fabad29..9765a4578eb 100644 --- a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java @@ -11,7 +11,7 @@ import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisClientConfig; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.util.Pool; public class PooledConnectionProvider implements ConnectionProvider { @@ -29,7 +29,7 @@ public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clien this.connectionMapKey = hostAndPort; } - public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache csCache) { + public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { this(new ConnectionPool(hostAndPort, clientConfig, csCache)); this.connectionMapKey = hostAndPort; } @@ -40,7 +40,7 @@ public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clien this.connectionMapKey = hostAndPort; } - public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache csCache, + public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig csCache, GenericObjectPoolConfig poolConfig) { this(new ConnectionPool(hostAndPort, clientConfig, csCache, poolConfig)); this.connectionMapKey = hostAndPort; diff --git a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java index 1d7c5790c21..de5b02d111c 100644 --- a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java @@ -17,7 +17,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPubSub; -import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.IOUtils; @@ -36,7 +36,7 @@ public class SentineledConnectionProvider implements ConnectionProvider { private final JedisClientConfig masterClientConfig; - private final ClientSideCache clientSideCache; + private final ClientSideCacheConfig clientSideCache; private final GenericObjectPoolConfig masterPoolConfig; @@ -54,7 +54,7 @@ public SentineledConnectionProvider(String masterName, final JedisClientConfig m } public SentineledConnectionProvider(String masterName, final JedisClientConfig masterClientConfig, - ClientSideCache clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { + ClientSideCacheConfig clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { this(masterName, masterClientConfig, clientSideCache, null, sentinels, sentinelClientConfig); } @@ -66,7 +66,7 @@ public SentineledConnectionProvider(String masterName, final JedisClientConfig m } public SentineledConnectionProvider(String masterName, final JedisClientConfig masterClientConfig, - ClientSideCache clientSideCache, final GenericObjectPoolConfig poolConfig, + ClientSideCacheConfig clientSideCache, final GenericObjectPoolConfig poolConfig, Set sentinels, final JedisClientConfig sentinelClientConfig) { this(masterName, masterClientConfig, clientSideCache, poolConfig, sentinels, sentinelClientConfig, DEFAULT_SUBSCRIBE_RETRY_WAIT_TIME_MILLIS); @@ -80,7 +80,7 @@ public SentineledConnectionProvider(String masterName, final JedisClientConfig m } public SentineledConnectionProvider(String masterName, final JedisClientConfig masterClientConfig, - ClientSideCache clientSideCache, final GenericObjectPoolConfig poolConfig, + ClientSideCacheConfig clientSideCache, final GenericObjectPoolConfig poolConfig, Set sentinels, final JedisClientConfig sentinelClientConfig, final long subscribeRetryWaitTimeMillis) { @@ -128,7 +128,7 @@ private void initMaster(HostAndPort master) { pool = newPool; LOG.info("Created connection pool to master at {}.", master); if (clientSideCache != null) { - clientSideCache.clear(); + clientSideCache.getClientSideCache().clear(); } if (existingPool != null) { diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 642973a1321..10225e278bd 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -1,10 +1,12 @@ package redis.clients.jedis.util; import java.net.URI; -import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; + +import redis.clients.jedis.csc.ClientSideCacheConfig; +import redis.clients.jedis.csc.DefaultClientSideCacheConfig; import redis.clients.jedis.csc.util.GuavaCSC; import redis.clients.jedis.csc.util.CaffeineCSC; @@ -76,7 +78,7 @@ public static RedisProtocol getRedisProtocol(URI uri) { private static final Integer ZERO_INTEGER = 0; - public static ClientSideCache getClientSideCache(URI uri) { + public static ClientSideCacheConfig getClientSideCache(URI uri) { if (uri.getQuery() == null) return null; boolean guava = false, caffeine = false; // cache_lib @@ -140,12 +142,12 @@ public static ClientSideCache getClientSideCache(URI uri) { GuavaCSC.Builder guavaBuilder = GuavaCSC.builder(); if (maxSize != null) guavaBuilder.maximumSize(maxSize); if (ttl != null) guavaBuilder.ttl(ttl); - return guavaBuilder.build(); + return new DefaultClientSideCacheConfig(guavaBuilder.build()); } else if (caffeine) { CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder(); if (maxSize != null) caffeineBuilder.maximumSize(maxSize); if (ttl != null) caffeineBuilder.ttl(ttl); - return caffeineBuilder.build(); + return new DefaultClientSideCacheConfig(caffeineBuilder.build()); } return null; // null (default) when not defined diff --git a/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java index 60ddf002d73..9eaabce49a4 100644 --- a/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java @@ -13,6 +13,7 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.hamcrest.Matchers; import org.junit.Test; +import redis.clients.jedis.csc.DefaultClientSideCacheConfig; import redis.clients.jedis.util.MapCSC; public class JedisClusterClientSideCacheTest extends JedisClusterTestBase { @@ -31,7 +32,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 DefaultClientSideCacheConfig(new MapCSC()))) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.del("foo"); @@ -42,7 +43,8 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(map), singleConnectionPoolConfig.get())) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), + singleConnectionPoolConfig.get())) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -60,7 +62,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 DefaultClientSideCacheConfig(new MapCSC()))) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.flushAll(); @@ -71,7 +73,8 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(map), singleConnectionPoolConfig.get())) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), + singleConnectionPoolConfig.get())) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java index 2e641e0f3a8..474bf0ed0f7 100644 --- a/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java @@ -11,6 +11,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import redis.clients.jedis.csc.DefaultClientSideCacheConfig; import redis.clients.jedis.util.MapCSC; public class JedisPooledClientSideCacheTest { @@ -42,7 +43,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 DefaultClientSideCacheConfig(new MapCSC()))) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -53,7 +54,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC(map), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -71,7 +72,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 DefaultClientSideCacheConfig(new MapCSC()))) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); @@ -82,7 +83,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC(map), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), 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/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java index 9e5f720933f..55608f9efa9 100644 --- a/src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java @@ -11,6 +11,7 @@ import org.hamcrest.Matchers; import org.junit.Test; +import redis.clients.jedis.csc.DefaultClientSideCacheConfig; import redis.clients.jedis.util.MapCSC; public class JedisSentineledClientSideCacheTest { @@ -28,7 +29,8 @@ public class JedisSentineledClientSideCacheTest { @Test public void simple() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, + new DefaultClientSideCacheConfig(new MapCSC()), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.del("foo"); @@ -39,7 +41,8 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(map), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, + new DefaultClientSideCacheConfig(new MapCSC(map)), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -57,7 +60,8 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, + new DefaultClientSideCacheConfig(new MapCSC()), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.flushAll(); @@ -68,7 +72,8 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(map), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, + new DefaultClientSideCacheConfig(new MapCSC(map)), sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/util/MapCSC.java b/src/test/java/redis/clients/jedis/util/MapCSC.java index f693c838334..7df8255f004 100644 --- a/src/test/java/redis/clients/jedis/util/MapCSC.java +++ b/src/test/java/redis/clients/jedis/util/MapCSC.java @@ -20,27 +20,27 @@ public MapCSC(Map map) { } @Override - protected final void invalidateAllCommandHashes() { + protected final void invalidateAllHashes() { cache.clear(); } @Override - protected void invalidateCommandHashes(Iterable hashes) { + protected void invalidateHashes(Iterable hashes) { hashes.forEach(hash -> cache.remove(hash)); } @Override - protected void put(long hash, Object value) { + protected void putValue(long hash, Object value) { cache.put(hash, value); } @Override - protected Object get(long hash) { + protected Object getValue(long hash) { return cache.get(hash); } @Override - protected final long getCommandHash(CommandObject command) { + protected final long getHash(CommandObject command) { long result = 1; for (Rawable raw : command.getArguments()) { result = 31 * result + Arrays.hashCode(raw.getRaw()); From 65e93ce3bd36a93f3830fe0207b59171ad0ec19f Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 18:50:44 +0600 Subject: [PATCH 4/7] Default isCacheable --- .../java/redis/clients/jedis/csc/ClientSideCacheConfig.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java index caee090937b..b7704baa825 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java @@ -9,5 +9,7 @@ public interface ClientSideCacheConfig { */ ClientSideCache getClientSideCache(); - boolean isCacheable(ProtocolCommand command, Object... keys); + default boolean isCacheable(ProtocolCommand command, Object... keys) { + return true; + } } From 5fb6debdd4fef519ec225cef157017600708d08e Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:56:49 +0600 Subject: [PATCH 5/7] Config to WhiteList/BlackList commands and String keys --- ...iteListBlackListClientSideCacheConfig.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java diff --git a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java new file mode 100644 index 00000000000..0747d6347cb --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java @@ -0,0 +1,48 @@ +package redis.clients.jedis.csc; + +import java.util.Set; +import redis.clients.jedis.commands.ProtocolCommand; + +public class StringWhiteListBlackListClientSideCacheConfig implements ClientSideCacheConfig { + + private final ClientSideCache csCache; + + private final Set whiteCommands; + private final Set blackCommands; + + private final Set whiteKeys; + private final Set blackKeys; + + public StringWhiteListBlackListClientSideCacheConfig(ClientSideCache clientSideCache, + Set whiteListCommands, Set blackListCommands, + Set whiteListKeys, Set blackListKeys) { + this.csCache = clientSideCache; + this.whiteCommands = whiteListCommands; + this.blackCommands = blackListCommands; + this.whiteKeys = whiteListKeys; + this.blackKeys = blackListKeys; + } + + @Override + public ClientSideCache getClientSideCache() { + return this.csCache; + } + + @Override + public boolean isCacheable(ProtocolCommand command, Object... keys) { + if (!(keys instanceof String[])) { + throw new IllegalArgumentException(this.getClass() + " can only process String keys."); + } + + if (!whiteCommands.contains(command)) return false; + if (blackCommands.contains(command)) return false; + + String[] strs = (String[]) keys; + for (String str : strs) { + if (!whiteKeys.contains(str)) return false; + if (blackKeys.contains(str)) return false; + } + + return true; + } +} From 0e17dbe2188243574af4e8c419a3c1c51c9da0d8 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:59:35 +0600 Subject: [PATCH 6/7] Create csc test package(s) --- .../{ => csc}/JedisClusterClientSideCacheTest.java | 12 +++++++++--- .../{ => csc}/JedisPooledClientSideCacheTest.java | 14 +++++++++++--- .../JedisSentineledClientSideCacheTest.java | 11 ++++++++--- .../redis/clients/jedis/{ => csc}/util/MapCSC.java | 2 +- 4 files changed, 29 insertions(+), 10 deletions(-) rename src/test/java/redis/clients/jedis/{ => csc}/JedisClusterClientSideCacheTest.java (89%) rename src/test/java/redis/clients/jedis/{ => csc}/JedisPooledClientSideCacheTest.java (88%) rename src/test/java/redis/clients/jedis/{ => csc}/JedisSentineledClientSideCacheTest.java (91%) rename src/test/java/redis/clients/jedis/{ => csc}/util/MapCSC.java (96%) diff --git a/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java similarity index 89% rename from src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java rename to src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 9eaabce49a4..3f5d5419495 100644 --- a/src/test/java/redis/clients/jedis/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -1,4 +1,4 @@ -package redis.clients.jedis; +package redis.clients.jedis.csc; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; @@ -13,8 +13,14 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.hamcrest.Matchers; import org.junit.Test; -import redis.clients.jedis.csc.DefaultClientSideCacheConfig; -import redis.clients.jedis.util.MapCSC; +import redis.clients.jedis.Connection; +import redis.clients.jedis.ConnectionPoolConfig; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.JedisCluster; +import redis.clients.jedis.JedisClusterTestBase; +import redis.clients.jedis.csc.util.MapCSC; public class JedisClusterClientSideCacheTest extends JedisClusterTestBase { diff --git a/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java similarity index 88% rename from src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java rename to src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java index 474bf0ed0f7..c1ed6d4c81d 100644 --- a/src/test/java/redis/clients/jedis/JedisPooledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java @@ -1,4 +1,4 @@ -package redis.clients.jedis; +package redis.clients.jedis.csc; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; @@ -11,8 +11,16 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.csc.DefaultClientSideCacheConfig; -import redis.clients.jedis.util.MapCSC; + +import redis.clients.jedis.Connection; +import redis.clients.jedis.ConnectionPoolConfig; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.JedisPooled; +import redis.clients.jedis.csc.util.MapCSC; public class JedisPooledClientSideCacheTest { diff --git a/src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java similarity index 91% rename from src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java rename to src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 55608f9efa9..0113a444df0 100644 --- a/src/test/java/redis/clients/jedis/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -1,4 +1,4 @@ -package redis.clients.jedis; +package redis.clients.jedis.csc; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; @@ -11,8 +11,13 @@ import org.hamcrest.Matchers; import org.junit.Test; -import redis.clients.jedis.csc.DefaultClientSideCacheConfig; -import redis.clients.jedis.util.MapCSC; + +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.JedisSentineled; +import redis.clients.jedis.csc.util.MapCSC; public class JedisSentineledClientSideCacheTest { diff --git a/src/test/java/redis/clients/jedis/util/MapCSC.java b/src/test/java/redis/clients/jedis/csc/util/MapCSC.java similarity index 96% rename from src/test/java/redis/clients/jedis/util/MapCSC.java rename to src/test/java/redis/clients/jedis/csc/util/MapCSC.java index 7df8255f004..ec9f5f64d1d 100644 --- a/src/test/java/redis/clients/jedis/util/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/util/MapCSC.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.util; +package redis.clients.jedis.csc.util; import java.util.Arrays; import java.util.Map; From 5375c9e872016bb3af2688fae4346d51add6eb64 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 27 Feb 2024 20:48:43 +0600 Subject: [PATCH 7/7] Test white-list/black-list commands and keys --- .../redis/clients/jedis/UnifiedJedis.java | 2 +- ...iteListBlackListClientSideCacheConfig.java | 16 +-- ...WhiteListBlackListClientSideCacheTest.java | 117 ++++++++++++++++++ 3 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 395788ec060..fec08ada03c 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -302,7 +302,7 @@ public void setBroadcastAndRoundRobinConfig(JedisBroadcastAndRoundRobinConfig co private T checkAndClientSideCacheCommand(CommandObject command, Object... keys) { if (clientSideCache != null) { - if (clientSideCache.isCacheable(command.getArguments().getCommand(), keys)) { + if (clientSideCache.isCacheable(command.getArguments().getCommand(), (Object[]) keys)) { return clientSideCache.getClientSideCache().get((cmd) -> executeCommand(cmd), command, keys); } } diff --git a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java index 0747d6347cb..0b0c421454a 100644 --- a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java +++ b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java @@ -30,17 +30,13 @@ public ClientSideCache getClientSideCache() { @Override public boolean isCacheable(ProtocolCommand command, Object... keys) { - if (!(keys instanceof String[])) { - throw new IllegalArgumentException(this.getClass() + " can only process String keys."); - } - - if (!whiteCommands.contains(command)) return false; - if (blackCommands.contains(command)) return false; + if (whiteCommands != null && !whiteCommands.contains(command)) return false; + if (blackCommands != null && blackCommands.contains(command)) return false; - String[] strs = (String[]) keys; - for (String str : strs) { - if (!whiteKeys.contains(str)) return false; - if (blackKeys.contains(str)) return false; + for (Object key : keys) { + if (!(key instanceof String)) throw new IllegalArgumentException(this.getClass() + " can only process String keys."); + if (whiteKeys != null && !whiteKeys.contains((String) key)) return false; + if (blackKeys != null && blackKeys.contains((String) key)) return false; } return true; diff --git a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java new file mode 100644 index 00000000000..41e2c5e06fd --- /dev/null +++ b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java @@ -0,0 +1,117 @@ +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.function.Supplier; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import org.hamcrest.Matchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import redis.clients.jedis.Connection; +import redis.clients.jedis.ConnectionPoolConfig; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.HostAndPorts; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisClientConfig; +import redis.clients.jedis.JedisPooled; +import redis.clients.jedis.Protocol; +import redis.clients.jedis.csc.util.MapCSC; + +public class WhiteListBlackListClientSideCacheTest { + + protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1); + + protected Jedis control; + + @Before + public void setUp() throws Exception { + control = new Jedis(hnp, DefaultJedisClientConfig.builder().password("foobared").build()); + control.flushAll(); + } + + @After + public void tearDown() throws Exception { + control.close(); + } + + private static final Supplier clientConfig + = () -> DefaultJedisClientConfig.builder().resp3().password("foobared").build(); + + private static final Supplier> singleConnectionPoolConfig + = () -> { + ConnectionPoolConfig poolConfig = new ConnectionPoolConfig(); + poolConfig.setMaxTotal(1); + return poolConfig; + }; + + @Test + public void none() { + HashMap map = new HashMap<>(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), null, null, null, null), + singleConnectionPoolConfig.get())) { + control.set("foo", "bar"); + assertThat(map, Matchers.aMapWithSize(0)); + assertEquals("bar", jedis.get("foo")); + assertThat(map, Matchers.aMapWithSize(1)); + } + } + + @Test + public void whiteListCommand() { + HashMap map = new HashMap<>(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), singleton(Protocol.Command.GET), null, null, null), + singleConnectionPoolConfig.get())) { + control.set("foo", "bar"); + assertThat(map, Matchers.aMapWithSize(0)); + assertEquals("bar", jedis.get("foo")); + assertThat(map, Matchers.aMapWithSize(1)); + } + } + + @Test + public void blackListCommand() { + HashMap map = new HashMap<>(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), null, singleton(Protocol.Command.GET), null, null), + singleConnectionPoolConfig.get())) { + control.set("foo", "bar"); + assertThat(map, Matchers.aMapWithSize(0)); + assertEquals("bar", jedis.get("foo")); + assertThat(map, Matchers.aMapWithSize(0)); + } + } + + @Test + public void whiteListKey() { + HashMap map = new HashMap<>(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), null, null, singleton("foo"), null), + singleConnectionPoolConfig.get())) { + control.set("foo", "bar"); + assertThat(map, Matchers.aMapWithSize(0)); + assertEquals("bar", jedis.get("foo")); + assertThat(map, Matchers.aMapWithSize(1)); + } + } + + @Test + public void blackListKey() { + HashMap map = new HashMap<>(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), null, null, null, singleton("foo")), + singleConnectionPoolConfig.get())) { + control.set("foo", "bar"); + assertThat(map, Matchers.aMapWithSize(0)); + assertEquals("bar", jedis.get("foo")); + assertThat(map, Matchers.aMapWithSize(0)); + } + } +}