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 01/14] 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 bff5898f8d..b1dbcaf86b 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 5b43606205..ecfceb77ef 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 70202deeae..59f416649c 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 0138058d66..bd7f8d017c 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 5646dbfb59..482fe12cb5 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 a5840c6530..4bda1d1ebb 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 f1cb8ea650..164c665d3f 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 4bd82fec1e..dc84974db7 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 37c4316ec0..80f6897fba 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 389b795814..376d3d673a 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 be83aa7388..6b148a5e77 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 85fa3cecd2..0d85fabad2 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 e335803b62..1d7c5790c2 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 3bce3504b3..dbe4604e8f 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 d9973b7dc6..8c3614c4eb 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 2d73cf04d4..68d1228d85 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 eb229036ea..f693c83833 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 02/14] 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 376d3d673a..cc5b697d7e 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 dbe4604e8f..735e0fff2d 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 8c3614c4eb..9a60792d25 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 68d1228d85..642973a132 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 03/14] 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 b1dbcaf86b..dddd2270ca 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 ecfceb77ef..190b4b53dd 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 59f416649c..fe04cfdd64 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 bd7f8d017c..8b1019ba4d 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 482fe12cb5..233e3e4538 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 4bda1d1ebb..ed6ea9c9c0 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 164c665d3f..7643865c64 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 dc84974db7..4451504421 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 80f6897fba..395788ec06 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 cc5b697d7e..cb52e247d8 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 0000000000..caee090937 --- /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 0000000000..2fae420029 --- /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 735e0fff2d..861972142e 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 9a60792d25..1c6957c4d5 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 6b148a5e77..b496153584 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 0d85fabad2..9765a4578e 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 1d7c5790c2..de5b02d111 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 642973a132..10225e278b 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 60ddf002d7..9eaabce49a 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 2e641e0f3a..474bf0ed0f 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 9e5f720933..55608f9efa 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 f693c83833..7df8255f00 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 04/14] 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 caee090937..b7704baa82 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 05/14] 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 0000000000..0747d6347c --- /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 06/14] 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 9eaabce49a..3f5d541949 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 474bf0ed0f..c1ed6d4c81 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 55608f9efa..0113a444df 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 7df8255f00..ec9f5f64d1 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 07/14] 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 395788ec06..fec08ada03 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 0747d6347c..0b0c421454 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 0000000000..41e2c5e06f --- /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)); + } + } +} From 20e6b4fbdb4c5798dc4fd390f7ad734a7a001aef Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 28 Feb 2024 15:56:38 +0600 Subject: [PATCH 08/14] Merge fix --- .../clients/jedis/ClientSideCacheLibsTest.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java index f6469c09b0..cfb3a2f0fd 100644 --- a/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java @@ -16,8 +16,9 @@ import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.util.CaffeineCSC; -import redis.clients.jedis.util.GuavaCSC; +import redis.clients.jedis.csc.DefaultClientSideCacheConfig; +import redis.clients.jedis.csc.util.CaffeineCSC; +import redis.clients.jedis.csc.util.GuavaCSC; public class ClientSideCacheLibsTest { @@ -48,8 +49,9 @@ public void tearDown() throws Exception { @Test public void guavaSimple() { - GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10).hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) { + GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10) + .hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build(); + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(guava))) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -62,7 +64,8 @@ public void guavaMore() { com.google.common.cache.Cache guava = CacheBuilder.newBuilder().recordStats().build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaCSC(guava), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new DefaultClientSideCacheConfig(new GuavaCSC(guava)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, guava.size()); assertEquals("bar", jedis.get("foo")); @@ -85,7 +88,7 @@ public void guavaMore() { @Test public void caffeineSimple() { CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10).hashFunction(LongHashFunction.xx()).build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(caffeine))) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -98,7 +101,8 @@ 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, LongHashFunction.city_1_1()), + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), + new DefaultClientSideCacheConfig(new CaffeineCSC(caffeine, LongHashFunction.city_1_1())), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, caffeine.estimatedSize()); From 4331688573924dd42518d937adee011bf8b6239d Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:00:18 +0600 Subject: [PATCH 09/14] Remove csc.util package --- .../clients/jedis/csc/{util => }/CaffeineCSC.java | 3 +-- .../clients/jedis/csc/{util => }/GuavaCSC.java | 3 +-- .../redis/clients/jedis/util/JedisURIHelper.java | 4 ++-- .../jedis/{ => csc}/ClientSideCacheLibsTest.java | 13 +++++++++---- .../jedis/csc/JedisClusterClientSideCacheTest.java | 1 - .../jedis/csc/JedisPooledClientSideCacheTest.java | 1 - .../csc/JedisSentineledClientSideCacheTest.java | 1 - .../redis/clients/jedis/csc/{util => }/MapCSC.java | 3 +-- .../csc/WhiteListBlackListClientSideCacheTest.java | 1 - 9 files changed, 14 insertions(+), 16 deletions(-) rename src/main/java/redis/clients/jedis/csc/{util => }/CaffeineCSC.java (96%) rename src/main/java/redis/clients/jedis/csc/{util => }/GuavaCSC.java (96%) rename src/test/java/redis/clients/jedis/{ => csc}/ClientSideCacheLibsTest.java (91%) rename src/test/java/redis/clients/jedis/csc/{util => }/MapCSC.java (92%) diff --git a/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java similarity index 96% rename from src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java rename to src/main/java/redis/clients/jedis/csc/CaffeineCSC.java index 861972142e..d0bfa1ec89 100644 --- a/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.csc.util; +package redis.clients.jedis.csc; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; @@ -6,7 +6,6 @@ import net.openhft.hashing.LongHashFunction; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; -import redis.clients.jedis.csc.ClientSideCache; public class CaffeineCSC extends ClientSideCache { diff --git a/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java similarity index 96% rename from src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java rename to src/main/java/redis/clients/jedis/csc/GuavaCSC.java index 82ab627d8e..f435978cda 100644 --- a/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java @@ -1,4 +1,4 @@ -package redis.clients.jedis.csc.util; +package redis.clients.jedis.csc; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; @@ -6,7 +6,6 @@ import com.google.common.hash.Hasher; import java.util.concurrent.TimeUnit; import redis.clients.jedis.CommandObject; -import redis.clients.jedis.csc.ClientSideCache; 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 10225e278b..6a6f846396 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -7,8 +7,8 @@ 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; +import redis.clients.jedis.csc.GuavaCSC; +import redis.clients.jedis.csc.CaffeineCSC; public final class JedisURIHelper { diff --git a/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java similarity index 91% rename from src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java rename to src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index cfb3a2f0fd..eda1200962 100644 --- a/src/test/java/redis/clients/jedis/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.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; @@ -16,9 +16,14 @@ import org.junit.Before; import org.junit.Test; -import redis.clients.jedis.csc.DefaultClientSideCacheConfig; -import redis.clients.jedis.csc.util.CaffeineCSC; -import redis.clients.jedis.csc.util.GuavaCSC; +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; public class ClientSideCacheLibsTest { diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 3f5d541949..946eb94f3d 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -20,7 +20,6 @@ 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/csc/JedisPooledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java index c1ed6d4c81..cc0d7c6133 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisPooledClientSideCacheTest.java @@ -20,7 +20,6 @@ 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/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 0113a444df..1ced5d0787 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -17,7 +17,6 @@ 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/csc/util/MapCSC.java b/src/test/java/redis/clients/jedis/csc/MapCSC.java similarity index 92% rename from src/test/java/redis/clients/jedis/csc/util/MapCSC.java rename to src/test/java/redis/clients/jedis/csc/MapCSC.java index ec9f5f64d1..b6772ec33b 100644 --- a/src/test/java/redis/clients/jedis/csc/util/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/MapCSC.java @@ -1,11 +1,10 @@ -package redis.clients.jedis.csc.util; +package redis.clients.jedis.csc; import java.util.Arrays; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; -import redis.clients.jedis.csc.ClientSideCache; public class MapCSC extends ClientSideCache { diff --git a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java index 41e2c5e06f..e5d9093acc 100644 --- a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java @@ -21,7 +21,6 @@ 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 { From ae3b2e3554bc06379ba8ea7397f322771c814734 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:34:14 +0600 Subject: [PATCH 10/14] Fix javadoc links --- 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 cb52e247d8..5e3bb5cb33 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -12,8 +12,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.util.CaffeineCSC CaffeineCSC} or - * {@link redis.clients.jedis.csc.util.GuavaCSC GuavaCSC} or a custom implementation of their own. + * 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. */ public abstract class ClientSideCache { From b3e449491554848bc1fcaba3dbf2f5422662cf54 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Tue, 5 Mar 2024 23:36:58 +0600 Subject: [PATCH 11/14] Added ClientSideCacheable interface and removed ClientSideCacheConfig interface --- .../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 | 8 +++---- .../redis/clients/jedis/UnifiedJedis.java | 18 +++++++-------- .../redis/clients/jedis/csc/CaffeineCSC.java | 16 +++++++++++++- .../clients/jedis/csc/ClientSideCache.java | 13 +++++++++-- .../jedis/csc/ClientSideCacheConfig.java | 15 ------------- .../jedis/csc/ClientSideCacheable.java | 8 +++++++ .../csc/DefaultClientSideCacheConfig.java | 22 ------------------- .../jedis/csc/DefaultClientSideCacheable.java | 15 +++++++++++++ .../redis/clients/jedis/csc/GuavaCSC.java | 20 ++++++++++++++++- ...hiteListBlackListClientSideCacheable.java} | 14 +++--------- .../providers/ClusterConnectionProvider.java | 8 +++---- .../providers/PooledConnectionProvider.java | 6 ++--- .../SentineledConnectionProvider.java | 12 +++++----- .../clients/jedis/util/JedisURIHelper.java | 9 ++++---- .../jedis/csc/ClientSideCacheLibsTest.java | 11 +++++----- .../csc/JedisClusterClientSideCacheTest.java | 8 +++---- .../csc/JedisPooledClientSideCacheTest.java | 10 +++++---- .../JedisSentineledClientSideCacheTest.java | 16 +++++++------- .../java/redis/clients/jedis/csc/MapCSC.java | 9 ++++++-- ...WhiteListBlackListClientSideCacheTest.java | 10 ++++----- 27 files changed, 164 insertions(+), 142 deletions(-) delete mode 100644 src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java create mode 100644 src/main/java/redis/clients/jedis/csc/ClientSideCacheable.java delete mode 100644 src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java create mode 100644 src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheable.java rename src/main/java/redis/clients/jedis/csc/{StringWhiteListBlackListClientSideCacheConfig.java => StringWhiteListBlackListClientSideCacheable.java} (69%) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index dddd2270ca..79842e34ce 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -17,7 +17,6 @@ import redis.clients.jedis.Protocol.Keyword; import redis.clients.jedis.args.ClientAttributeOption; import redis.clients.jedis.args.Rawable; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; @@ -26,6 +25,7 @@ import redis.clients.jedis.util.IOUtils; import redis.clients.jedis.util.RedisInputStream; import redis.clients.jedis.util.RedisOutputStream; +import redis.clients.jedis.csc.ClientSideCache; public class Connection implements Closeable { @@ -35,7 +35,7 @@ public class Connection implements Closeable { private Socket socket; private RedisOutputStream outputStream; private RedisInputStream inputStream; - private ClientSideCacheConfig clientSideCache; + private ClientSideCache 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, ClientSideCacheConfig csCache) { + public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, ClientSideCache csCache) { this.socketFactory = socketFactory; this.soTimeout = clientConfig.getSocketTimeoutMillis(); this.infiniteSoTimeout = clientConfig.getBlockingSocketTimeoutMillis(); @@ -520,7 +520,7 @@ public boolean ping() { return true; } - private void initializeClientSideCache(ClientSideCacheConfig csCache) { + private void initializeClientSideCache(ClientSideCache 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 190b4b53dd..85a87d1126 100644 --- a/src/main/java/redis/clients/jedis/ConnectionFactory.java +++ b/src/main/java/redis/clients/jedis/ConnectionFactory.java @@ -6,8 +6,8 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisException; +import redis.clients.jedis.csc.ClientSideCache; /** * PoolableObjectFactory custom impl. @@ -18,7 +18,7 @@ public class ConnectionFactory implements PooledObjectFactory { private final JedisSocketFactory jedisSocketFactory; private final JedisClientConfig clientConfig; - private ClientSideCacheConfig clientSideCache = null; + private ClientSideCache 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, ClientSideCacheConfig csCache) { + public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache 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 fe04cfdd64..7af4d52252 100644 --- a/src/main/java/redis/clients/jedis/ConnectionPool.java +++ b/src/main/java/redis/clients/jedis/ConnectionPool.java @@ -2,8 +2,8 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.util.Pool; +import redis.clients.jedis.csc.ClientSideCache; 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, ClientSideCacheConfig csCache) { + public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache 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 8b1019ba4d..0b5e426e46 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -6,9 +6,9 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.ClusterConnectionProvider; import redis.clients.jedis.util.JedisClusterCRC16; +import redis.clients.jedis.csc.ClientSideCache; public class JedisCluster extends UnifiedJedis { @@ -216,31 +216,31 @@ private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Durati super(provider, maxAttempts, maxTotalRetriesDuration, protocol); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache) { + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache) { this(clusterNodes, clientConfig, clientSideCache, DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis())); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache, int maxAttempts, Duration maxTotalRetriesDuration) { this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache), maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache); } - public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig clientSideCache, + public JedisCluster(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig clientSideCache) { + RedisProtocol protocol, ClientSideCache 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 233e3e4538..47b8fba410 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -21,12 +21,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.SafeEncoder; import static redis.clients.jedis.JedisCluster.INIT_NO_ERROR_PROPERTY; +import redis.clients.jedis.csc.ClientSideCache; public class JedisClusterInfoCache { @@ -43,7 +43,7 @@ public class JedisClusterInfoCache { private final GenericObjectPoolConfig poolConfig; private final JedisClientConfig clientConfig; - private final ClientSideCacheConfig clientSideCache; + private final ClientSideCache 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, ClientSideCache 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, ClientSideCacheConfig csCache, + public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache 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.getClientSideCache().clear(); + clientSideCache.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 ed6ea9c9c0..27b62f2186 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -7,10 +7,10 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.PooledConnectionProvider; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.Pool; +import redis.clients.jedis.csc.ClientSideCache; public class JedisPooled extends UnifiedJedis { @@ -76,7 +76,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client super(hostAndPort, clientConfig); } - public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache 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 7643865c64..3a7bd07129 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -2,8 +2,8 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.providers.SentineledConnectionProvider; +import redis.clients.jedis.csc.ClientSideCache; 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, ClientSideCacheConfig clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache 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, ClientSideCacheConfig clientSideCache, + public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache 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 4451504421..e55d985159 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -12,11 +12,11 @@ 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; import redis.clients.jedis.util.SafeEncoder; +import redis.clients.jedis.csc.ClientSideCache; public final class Protocol { @@ -230,17 +230,17 @@ public static Object read(final RedisInputStream is) { return process(is); } - public static Object read(final RedisInputStream is, final ClientSideCacheConfig cache) { + public static Object read(final RedisInputStream is, final ClientSideCache cache) { readPushes(is, cache); return process(is); } - private static void readPushes(final RedisInputStream is, final ClientSideCacheConfig cache) { + private static void readPushes(final RedisInputStream is, final ClientSideCache cache) { if (cache != null) { //System.out.println("PEEK: " + is.peekByte()); while (is.peek(GREATER_THAN_BYTE)) { is.readByte(); - processPush(is, cache.getClientSideCache()); + processPush(is, cache); } } } diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index fec08ada03..6f9f7c6390 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -17,7 +17,6 @@ import redis.clients.jedis.commands.SampleBinaryKeyedCommands; import redis.clients.jedis.commands.SampleKeyedCommands; import redis.clients.jedis.commands.RedisModuleCommands; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.executors.*; import redis.clients.jedis.gears.TFunctionListParams; @@ -44,13 +43,14 @@ import redis.clients.jedis.util.IOUtils; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.KeyValue; +import redis.clients.jedis.csc.ClientSideCache; public class UnifiedJedis implements JedisCommands, JedisBinaryCommands, SampleKeyedCommands, SampleBinaryKeyedCommands, RedisModuleCommands, AutoCloseable { @Deprecated protected RedisProtocol protocol = null; - private final ClientSideCacheConfig clientSideCache; + private final ClientSideCache 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, ClientSideCacheConfig clientSideCache) { + public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig clientSideCache) { + protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, ClientSideCache 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, ClientSideCacheConfig clientSideCache) { + RedisProtocol protocol, ClientSideCache 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, (ClientSideCacheConfig) null); + this(executor, provider, commandObjects, protocol, (ClientSideCache) null); } private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects, - RedisProtocol protocol, ClientSideCacheConfig clientSideCache) { + RedisProtocol protocol, ClientSideCache clientSideCache) { if (clientSideCache != null && protocol != RedisProtocol.RESP3) { throw new IllegalArgumentException("Client-side caching is only supported with RESP3."); @@ -302,9 +302,7 @@ public void setBroadcastAndRoundRobinConfig(JedisBroadcastAndRoundRobinConfig co private T checkAndClientSideCacheCommand(CommandObject command, Object... keys) { if (clientSideCache != null) { - if (clientSideCache.isCacheable(command.getArguments().getCommand(), (Object[]) keys)) { - return clientSideCache.getClientSideCache().get((cmd) -> executeCommand(cmd), command, keys); - } + return clientSideCache.get((cmd) -> executeCommand(cmd), command, keys); } return executeCommand(command); diff --git a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java index d0bfa1ec89..9625644473 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java @@ -15,10 +15,17 @@ public class CaffeineCSC extends ClientSideCache { private final LongHashFunction function; public CaffeineCSC(Cache caffeineCache, LongHashFunction hashFunction) { + super(); this.cache = caffeineCache; this.function = hashFunction; } + public CaffeineCSC(Cache caffeineCache, LongHashFunction function, ClientSideCacheable cacheable) { + super(cacheable); + this.cache = caffeineCache; + this.function = function; + } + @Override protected final void invalidateAllHashes() { cache.invalidateAll(); @@ -62,6 +69,8 @@ public static class Builder { private LongHashFunction hashFunction = DEFAULT_HASH_FUNCTION; + private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; + private Builder() { } public Builder maximumSize(int size) { @@ -79,6 +88,11 @@ public Builder hashFunction(LongHashFunction function) { return this; } + public Builder cacheable(ClientSideCacheable cacheable) { + this.cacheable = cacheable; + return this; + } + public CaffeineCSC build() { Caffeine cb = Caffeine.newBuilder(); @@ -86,7 +100,7 @@ public CaffeineCSC build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return new CaffeineCSC(cb.build(), hashFunction); + return new CaffeineCSC(cb.build(), hashFunction, cacheable); } } } diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index 5e3bb5cb33..e006ea8063 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -20,10 +20,15 @@ public abstract class ClientSideCache { protected static final int DEFAULT_MAXIMUM_SIZE = 10_000; protected static final int DEFAULT_EXPIRE_SECONDS = 100; - private final Map> keyToCommandHashes; + private final Map> keyToCommandHashes = new ConcurrentHashMap<>(); + private final ClientSideCacheable cacheable; protected ClientSideCache() { - this.keyToCommandHashes = new ConcurrentHashMap<>(); + this.cacheable = DefaultClientSideCacheable.INSTANCE; + } + + protected ClientSideCache(ClientSideCacheable cacheable) { + this.cacheable = cacheable; } protected abstract void invalidateAllHashes(); @@ -70,6 +75,10 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) { public final T get(Function, T> loader, CommandObject command, Object... keys) { + if (!cacheable.isCacheable(command.getArguments().getCommand(), keys)) { + return loader.apply(command); + } + final long hash = getHash(command); T value = (T) getValue(hash); diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java deleted file mode 100644 index b7704baa82..0000000000 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCacheConfig.java +++ /dev/null @@ -1,15 +0,0 @@ -package redis.clients.jedis.csc; - -import redis.clients.jedis.commands.ProtocolCommand; - -public interface ClientSideCacheConfig { - - /** - * MUST NOT be {@code null}. - */ - ClientSideCache getClientSideCache(); - - default boolean isCacheable(ProtocolCommand command, Object... keys) { - return true; - } -} diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCacheable.java b/src/main/java/redis/clients/jedis/csc/ClientSideCacheable.java new file mode 100644 index 0000000000..d3c782ad00 --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCacheable.java @@ -0,0 +1,8 @@ +package redis.clients.jedis.csc; + +import redis.clients.jedis.commands.ProtocolCommand; + +public interface ClientSideCacheable { + + 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 deleted file mode 100644 index 2fae420029..0000000000 --- a/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheConfig.java +++ /dev/null @@ -1,22 +0,0 @@ -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/DefaultClientSideCacheable.java b/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheable.java new file mode 100644 index 0000000000..d97ca6a2ee --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/DefaultClientSideCacheable.java @@ -0,0 +1,15 @@ +package redis.clients.jedis.csc; + +import redis.clients.jedis.commands.ProtocolCommand; + +public class DefaultClientSideCacheable implements ClientSideCacheable { + + public static final DefaultClientSideCacheable INSTANCE = new DefaultClientSideCacheable(); + + public DefaultClientSideCacheable() { } + + @Override + public boolean isCacheable(ProtocolCommand command, Object... keys) { + return true; + } +} diff --git a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java index f435978cda..a6f55d73dd 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/GuavaCSC.java @@ -19,10 +19,21 @@ public GuavaCSC(Cache guavaCache) { } public GuavaCSC(Cache guavaCache, HashFunction hashFunction) { + super(); this.cache = guavaCache; this.function = hashFunction; } + public GuavaCSC(Cache guavaCache, ClientSideCacheable cacheable) { + this(guavaCache, DEFAULT_HASH_FUNCTION, cacheable); + } + + public GuavaCSC(Cache cache, HashFunction function, ClientSideCacheable cacheable) { + super(cacheable); + this.cache = cache; + this.function = function; + } + @Override protected final void invalidateAllHashes() { cache.invalidateAll(); @@ -63,6 +74,8 @@ public static class Builder { private HashFunction hashFunction = DEFAULT_HASH_FUNCTION; + private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE; + private Builder() { } public Builder maximumSize(int size) { @@ -80,6 +93,11 @@ public Builder hashFunction(HashFunction function) { return this; } + public Builder cacheable(ClientSideCacheable cacheable) { + this.cacheable = cacheable; + return this; + } + public GuavaCSC build() { CacheBuilder cb = CacheBuilder.newBuilder(); @@ -87,7 +105,7 @@ public GuavaCSC build() { cb.expireAfterWrite(expireTime, expireTimeUnit); - return new GuavaCSC(cb.build(), hashFunction); + return new GuavaCSC(cb.build(), hashFunction, cacheable); } } } diff --git a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java similarity index 69% rename from src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java rename to src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java index 0b0c421454..ee101bb2fc 100644 --- a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheConfig.java +++ b/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java @@ -3,9 +3,7 @@ import java.util.Set; import redis.clients.jedis.commands.ProtocolCommand; -public class StringWhiteListBlackListClientSideCacheConfig implements ClientSideCacheConfig { - - private final ClientSideCache csCache; +public class StringWhiteListBlackListClientSideCacheable implements ClientSideCacheable { private final Set whiteCommands; private final Set blackCommands; @@ -13,28 +11,22 @@ public class StringWhiteListBlackListClientSideCacheConfig implements ClientSide private final Set whiteKeys; private final Set blackKeys; - public StringWhiteListBlackListClientSideCacheConfig(ClientSideCache clientSideCache, + public StringWhiteListBlackListClientSideCacheable( 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 (whiteCommands != null && !whiteCommands.contains(command)) return false; if (blackCommands != null && blackCommands.contains(command)) return false; for (Object key : keys) { - if (!(key instanceof String)) throw new IllegalArgumentException(this.getClass() + " can only process String keys."); + if (!(key instanceof String)) return false; if (whiteKeys != null && !whiteKeys.contains((String) key)) return false; if (blackKeys != null && blackKeys.contains((String) key)) return false; } diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index b496153584..ab754ee037 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -15,11 +15,11 @@ import redis.clients.jedis.Connection; import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.JedisClusterInfoCache; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisClusterOperationException; import redis.clients.jedis.exceptions.JedisException; import static redis.clients.jedis.JedisCluster.INIT_NO_ERROR_PROPERTY; +import redis.clients.jedis.csc.ClientSideCache; public class ClusterConnectionProvider implements ConnectionProvider { @@ -30,7 +30,7 @@ public ClusterConnectionProvider(Set clusterNodes, JedisClientConfi initializeSlotsCache(clusterNodes, clientConfig); } - public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public ClusterConnectionProvider(Set clusterNodes, JedisClientConfig clientConfig, ClientSideCache 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 9765a4578e..277704cd1a 100644 --- a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java @@ -11,8 +11,8 @@ import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisClientConfig; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.util.Pool; +import redis.clients.jedis.csc.ClientSideCache; public class PooledConnectionProvider implements ConnectionProvider { @@ -29,7 +29,7 @@ public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clien this.connectionMapKey = hostAndPort; } - public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCacheConfig csCache) { + public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache 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, ClientSideCacheConfig csCache, + public PooledConnectionProvider(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache 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 de5b02d111..79b56e8891 100644 --- a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java @@ -17,10 +17,10 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPubSub; -import redis.clients.jedis.csc.ClientSideCacheConfig; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.util.IOUtils; +import redis.clients.jedis.csc.ClientSideCache; public class SentineledConnectionProvider implements ConnectionProvider { @@ -36,7 +36,7 @@ public class SentineledConnectionProvider implements ConnectionProvider { private final JedisClientConfig masterClientConfig; - private final ClientSideCacheConfig clientSideCache; + private final ClientSideCache clientSideCache; private final GenericObjectPoolConfig masterPoolConfig; @@ -54,7 +54,7 @@ public SentineledConnectionProvider(String masterName, final JedisClientConfig m } public SentineledConnectionProvider(String masterName, final JedisClientConfig masterClientConfig, - ClientSideCacheConfig clientSideCache, Set sentinels, final JedisClientConfig sentinelClientConfig) { + ClientSideCache 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, - ClientSideCacheConfig clientSideCache, final GenericObjectPoolConfig poolConfig, + ClientSideCache 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, - ClientSideCacheConfig clientSideCache, final GenericObjectPoolConfig poolConfig, + ClientSideCache 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.getClientSideCache().clear(); + clientSideCache.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 6a6f846396..89d7b196a5 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -5,10 +5,9 @@ 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.GuavaCSC; import redis.clients.jedis.csc.CaffeineCSC; +import redis.clients.jedis.csc.ClientSideCache; public final class JedisURIHelper { @@ -78,7 +77,7 @@ public static RedisProtocol getRedisProtocol(URI uri) { private static final Integer ZERO_INTEGER = 0; - public static ClientSideCacheConfig getClientSideCache(URI uri) { + public static ClientSideCache getClientSideCache(URI uri) { if (uri.getQuery() == null) return null; boolean guava = false, caffeine = false; // cache_lib @@ -142,12 +141,12 @@ public static ClientSideCacheConfig getClientSideCache(URI uri) { GuavaCSC.Builder guavaBuilder = GuavaCSC.builder(); if (maxSize != null) guavaBuilder.maximumSize(maxSize); if (ttl != null) guavaBuilder.ttl(ttl); - return new DefaultClientSideCacheConfig(guavaBuilder.build()); + return guavaBuilder.build(); } else if (caffeine) { CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder(); if (maxSize != null) caffeineBuilder.maximumSize(maxSize); if (ttl != null) caffeineBuilder.ttl(ttl); - return new DefaultClientSideCacheConfig(caffeineBuilder.build()); + return caffeineBuilder.build(); } return null; // null (default) when not defined diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index eda1200962..8c437326b8 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -56,7 +56,7 @@ public void tearDown() throws Exception { public void guavaSimple() { GuavaCSC guava = GuavaCSC.builder().maximumSize(10).ttl(10) .hashFunction(com.google.common.hash.Hashing.farmHashFingerprint64()).build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(guava))) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), guava)) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -69,8 +69,8 @@ public void guavaMore() { com.google.common.cache.Cache guava = CacheBuilder.newBuilder().recordStats().build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new DefaultClientSideCacheConfig(new GuavaCSC(guava)), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new GuavaCSC(guava), + singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, guava.size()); assertEquals("bar", jedis.get("foo")); @@ -93,7 +93,7 @@ public void guavaMore() { @Test public void caffeineSimple() { CaffeineCSC caffeine = CaffeineCSC.builder().maximumSize(10).ttl(10).hashFunction(LongHashFunction.xx()).build(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(caffeine))) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), caffeine)) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -107,8 +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 DefaultClientSideCacheConfig(new CaffeineCSC(caffeine, LongHashFunction.city_1_1())), - singleConnectionPoolConfig.get())) { + new CaffeineCSC(caffeine, LongHashFunction.city_1_1()), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertEquals(0, caffeine.estimatedSize()); assertEquals("bar", jedis.get("foo")); diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 946eb94f3d..8cae0562b6 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java @@ -37,7 +37,7 @@ public class JedisClusterClientSideCacheTest extends JedisClusterTestBase { @Test public void simple() { - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC()))) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC())) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.del("foo"); @@ -48,7 +48,7 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(map), singleConnectionPoolConfig.get())) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -67,7 +67,7 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC()))) { + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC())) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.flushAll(); @@ -78,7 +78,7 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), + try (JedisCluster jedis = new JedisCluster(hnp, clientConfig.get(), new MapCSC(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 cc0d7c6133..149b1730a0 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 DefaultClientSideCacheConfig(new MapCSC()))) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC())) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.del("foo"); @@ -61,7 +61,8 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC(map), + singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -79,7 +80,7 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC()))) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new MapCSC())) { control.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); control.flushAll(); @@ -90,7 +91,8 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new DefaultClientSideCacheConfig(new MapCSC(map)), singleConnectionPoolConfig.get())) { + try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), 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/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 1ced5d0787..3efbeb77f3 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -33,8 +33,8 @@ public class JedisSentineledClientSideCacheTest { @Test public void simple() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, - new DefaultClientSideCacheConfig(new MapCSC()), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), + sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.del("foo"); @@ -45,8 +45,8 @@ public void simple() { @Test public void simpleWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, - new DefaultClientSideCacheConfig(new MapCSC(map)), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(map), + sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); assertEquals("bar", jedis.get("foo")); @@ -64,8 +64,8 @@ public void simpleWithSimpleMap() { @Test public void flushAll() { - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, - new DefaultClientSideCacheConfig(new MapCSC()), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, new MapCSC(), + sentinels, sentinelClientConfig)) { jedis.set("foo", "bar"); assertEquals("bar", jedis.get("foo")); jedis.flushAll(); @@ -76,8 +76,8 @@ public void flushAll() { @Test public void flushAllWithSimpleMap() { HashMap map = new HashMap<>(); - try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, - new DefaultClientSideCacheConfig(new MapCSC(map)), sentinels, sentinelClientConfig)) { + try (JedisSentineled jedis = new JedisSentineled(MASTER_NAME, masterClientConfig, 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/csc/MapCSC.java b/src/test/java/redis/clients/jedis/csc/MapCSC.java index b6772ec33b..031b903c15 100644 --- a/src/test/java/redis/clients/jedis/csc/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/MapCSC.java @@ -1,8 +1,8 @@ package redis.clients.jedis.csc; import java.util.Arrays; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; @@ -11,13 +11,18 @@ public class MapCSC extends ClientSideCache { private final Map cache; public MapCSC() { - this(new ConcurrentHashMap<>()); + this(new HashMap<>()); } public MapCSC(Map map) { this.cache = map; } + public MapCSC(Map cache, ClientSideCacheable cacheable) { + super(cacheable); + this.cache = cache; + } + @Override protected final void invalidateAllHashes() { cache.clear(); diff --git a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java index e5d9093acc..ecb9802d31 100644 --- a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java @@ -53,7 +53,7 @@ public void tearDown() throws Exception { public void none() { HashMap map = new HashMap<>(); try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), - new StringWhiteListBlackListClientSideCacheConfig(new MapCSC(map), null, null, null, null), + new MapCSC(map, new StringWhiteListBlackListClientSideCacheable(null, null, null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -66,7 +66,7 @@ public void none() { 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), + new MapCSC(map, new StringWhiteListBlackListClientSideCacheable(singleton(Protocol.Command.GET), null, null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -79,7 +79,7 @@ public void whiteListCommand() { 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), + new MapCSC(map, new StringWhiteListBlackListClientSideCacheable(null, singleton(Protocol.Command.GET), null, null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -92,7 +92,7 @@ public void blackListCommand() { 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), + new MapCSC(map, new StringWhiteListBlackListClientSideCacheable(null, null, singleton("foo"), null)), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); @@ -105,7 +105,7 @@ public void whiteListKey() { 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")), + new MapCSC(map, new StringWhiteListBlackListClientSideCacheable(null, null, null, singleton("foo"))), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0)); From 158b941f9b623559ab7d8e68a2705cd1ace10aa7 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 00:06:52 +0600 Subject: [PATCH 12/14] Format imports --- src/main/java/redis/clients/jedis/Connection.java | 2 +- src/main/java/redis/clients/jedis/ConnectionFactory.java | 2 +- src/main/java/redis/clients/jedis/ConnectionPool.java | 2 +- src/main/java/redis/clients/jedis/JedisCluster.java | 2 +- src/main/java/redis/clients/jedis/JedisClusterInfoCache.java | 2 +- src/main/java/redis/clients/jedis/JedisPooled.java | 2 +- src/main/java/redis/clients/jedis/JedisSentineled.java | 2 +- src/main/java/redis/clients/jedis/Protocol.java | 1 - src/main/java/redis/clients/jedis/UnifiedJedis.java | 2 +- .../clients/jedis/providers/ClusterConnectionProvider.java | 2 +- .../clients/jedis/providers/PooledConnectionProvider.java | 2 +- .../clients/jedis/providers/SentineledConnectionProvider.java | 2 +- src/main/java/redis/clients/jedis/util/JedisURIHelper.java | 1 + .../java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java | 3 +-- .../clients/jedis/csc/JedisClusterClientSideCacheTest.java | 1 + .../clients/jedis/csc/JedisSentineledClientSideCacheTest.java | 1 - src/test/java/redis/clients/jedis/csc/MapCSC.java | 1 + 17 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 79842e34ce..b2d2aac27e 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -18,6 +18,7 @@ import redis.clients.jedis.args.ClientAttributeOption; import redis.clients.jedis.args.Rawable; import redis.clients.jedis.commands.ProtocolCommand; +import redis.clients.jedis.csc.ClientSideCache; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; @@ -25,7 +26,6 @@ import redis.clients.jedis.util.IOUtils; import redis.clients.jedis.util.RedisInputStream; import redis.clients.jedis.util.RedisOutputStream; -import redis.clients.jedis.csc.ClientSideCache; public class Connection implements Closeable { diff --git a/src/main/java/redis/clients/jedis/ConnectionFactory.java b/src/main/java/redis/clients/jedis/ConnectionFactory.java index 85a87d1126..ecfceb77ef 100644 --- a/src/main/java/redis/clients/jedis/ConnectionFactory.java +++ b/src/main/java/redis/clients/jedis/ConnectionFactory.java @@ -6,8 +6,8 @@ import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import redis.clients.jedis.exceptions.JedisException; import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.exceptions.JedisException; /** * PoolableObjectFactory custom impl. diff --git a/src/main/java/redis/clients/jedis/ConnectionPool.java b/src/main/java/redis/clients/jedis/ConnectionPool.java index 7af4d52252..59f416649c 100644 --- a/src/main/java/redis/clients/jedis/ConnectionPool.java +++ b/src/main/java/redis/clients/jedis/ConnectionPool.java @@ -2,8 +2,8 @@ import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.util.Pool; 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 0b5e426e46..72a9495617 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -7,8 +7,8 @@ import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.providers.ClusterConnectionProvider; -import redis.clients.jedis.util.JedisClusterCRC16; import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.util.JedisClusterCRC16; public class JedisCluster extends UnifiedJedis { diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 47b8fba410..482fe12cb5 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -21,12 +21,12 @@ 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; import static redis.clients.jedis.JedisCluster.INIT_NO_ERROR_PROPERTY; -import redis.clients.jedis.csc.ClientSideCache; public class JedisClusterInfoCache { diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index 27b62f2186..4bda1d1ebb 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -7,10 +7,10 @@ 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; -import redis.clients.jedis.csc.ClientSideCache; public class JedisPooled extends UnifiedJedis { diff --git a/src/main/java/redis/clients/jedis/JedisSentineled.java b/src/main/java/redis/clients/jedis/JedisSentineled.java index 3a7bd07129..164c665d3f 100644 --- a/src/main/java/redis/clients/jedis/JedisSentineled.java +++ b/src/main/java/redis/clients/jedis/JedisSentineled.java @@ -2,8 +2,8 @@ import java.util.Set; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; -import redis.clients.jedis.providers.SentineledConnectionProvider; 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 e55d985159..dc84974db7 100644 --- a/src/main/java/redis/clients/jedis/Protocol.java +++ b/src/main/java/redis/clients/jedis/Protocol.java @@ -16,7 +16,6 @@ import redis.clients.jedis.util.RedisInputStream; import redis.clients.jedis.util.RedisOutputStream; import redis.clients.jedis.util.SafeEncoder; -import redis.clients.jedis.csc.ClientSideCache; public final class Protocol { diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 6f9f7c6390..57fea68497 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; @@ -43,7 +44,6 @@ import redis.clients.jedis.util.IOUtils; import redis.clients.jedis.util.JedisURIHelper; import redis.clients.jedis.util.KeyValue; -import redis.clients.jedis.csc.ClientSideCache; public class UnifiedJedis implements JedisCommands, JedisBinaryCommands, SampleKeyedCommands, SampleBinaryKeyedCommands, RedisModuleCommands, diff --git a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java index ab754ee037..6b148a5e77 100644 --- a/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/ClusterConnectionProvider.java @@ -15,11 +15,11 @@ 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; import static redis.clients.jedis.JedisCluster.INIT_NO_ERROR_PROPERTY; -import redis.clients.jedis.csc.ClientSideCache; public class ClusterConnectionProvider implements ConnectionProvider { diff --git a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java index 277704cd1a..0d85fabad2 100644 --- a/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/PooledConnectionProvider.java @@ -11,8 +11,8 @@ import redis.clients.jedis.ConnectionPool; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisClientConfig; -import redis.clients.jedis.util.Pool; 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 79b56e8891..1d7c5790c2 100644 --- a/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java +++ b/src/main/java/redis/clients/jedis/providers/SentineledConnectionProvider.java @@ -17,10 +17,10 @@ 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; -import redis.clients.jedis.csc.ClientSideCache; public class SentineledConnectionProvider implements ConnectionProvider { diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 89d7b196a5..f536eae9b2 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -1,6 +1,7 @@ package redis.clients.jedis.util; import java.net.URI; + import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index 8c437326b8..b9a2cf8d22 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -6,11 +6,10 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.google.common.cache.CacheBuilder; - import java.util.function.Supplier; import net.openhft.hashing.LongHashFunction; -import org.apache.commons.pool2.impl.GenericObjectPoolConfig; +import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; diff --git a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java index 8cae0562b6..3b52072560 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisClusterClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/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.Connection; import redis.clients.jedis.ConnectionPoolConfig; import redis.clients.jedis.DefaultJedisClientConfig; diff --git a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java index 3efbeb77f3..6390ab3194 100644 --- a/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/JedisSentineledClientSideCacheTest.java @@ -8,7 +8,6 @@ import java.util.HashMap; import java.util.Set; import java.util.stream.Collectors; - import org.hamcrest.Matchers; import org.junit.Test; diff --git a/src/test/java/redis/clients/jedis/csc/MapCSC.java b/src/test/java/redis/clients/jedis/csc/MapCSC.java index 031b903c15..ee08efb588 100644 --- a/src/test/java/redis/clients/jedis/csc/MapCSC.java +++ b/src/test/java/redis/clients/jedis/csc/MapCSC.java @@ -3,6 +3,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; + import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; From bbc351446d6faa2a15f67e907fb262fb1d53907e Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:13:49 +0600 Subject: [PATCH 13/14] Re-create csc.util package --- .../clients/jedis/csc/ClientSideCache.java | 4 ++-- .../jedis/csc/{ => util}/CaffeineCSC.java | 18 +++++++++++------- .../clients/jedis/csc/{ => util}/GuavaCSC.java | 14 +++++++++----- ...gWhiteListBlackListClientSideCacheable.java | 3 ++- .../clients/jedis/util/JedisURIHelper.java | 4 ++-- .../jedis/csc/ClientSideCacheLibsTest.java | 2 ++ .../WhiteListBlackListClientSideCacheTest.java | 1 + 7 files changed, 29 insertions(+), 17 deletions(-) rename src/main/java/redis/clients/jedis/csc/{ => util}/CaffeineCSC.java (83%) rename src/main/java/redis/clients/jedis/csc/{ => util}/GuavaCSC.java (88%) rename src/main/java/redis/clients/jedis/csc/{ => util}/StringWhiteListBlackListClientSideCacheable.java (93%) diff --git a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java index e006ea8063..22509e4e2c 100644 --- a/src/main/java/redis/clients/jedis/csc/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/csc/ClientSideCache.java @@ -12,8 +12,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.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/csc/CaffeineCSC.java b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java similarity index 83% rename from src/main/java/redis/clients/jedis/csc/CaffeineCSC.java rename to src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java index 9625644473..4362169550 100644 --- a/src/main/java/redis/clients/jedis/csc/CaffeineCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/CaffeineCSC.java @@ -1,29 +1,33 @@ -package redis.clients.jedis.csc; +package redis.clients.jedis.csc.util; import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import java.util.concurrent.TimeUnit; import net.openhft.hashing.LongHashFunction; + import redis.clients.jedis.CommandObject; import redis.clients.jedis.args.Rawable; +import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheable; +import redis.clients.jedis.csc.DefaultClientSideCacheable; public class CaffeineCSC extends ClientSideCache { private static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3(); private final Cache cache; - private final LongHashFunction function; + private final LongHashFunction hashFunction; public CaffeineCSC(Cache caffeineCache, LongHashFunction hashFunction) { super(); this.cache = caffeineCache; - this.function = hashFunction; + this.hashFunction = hashFunction; } public CaffeineCSC(Cache caffeineCache, LongHashFunction function, ClientSideCacheable cacheable) { super(cacheable); this.cache = caffeineCache; - this.function = function; + this.hashFunction = function; } @Override @@ -51,10 +55,10 @@ protected final long getHash(CommandObject command) { long[] nums = new long[command.getArguments().size() + 1]; int idx = 0; for (Rawable raw : command.getArguments()) { - nums[idx++] = function.hashBytes(raw.getRaw()); + nums[idx++] = hashFunction.hashBytes(raw.getRaw()); } - nums[idx] = function.hashInt(command.getBuilder().hashCode()); - return function.hashLongs(nums); + nums[idx] = hashFunction.hashInt(command.getBuilder().hashCode()); + return hashFunction.hashLongs(nums); } public static Builder builder() { diff --git a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java similarity index 88% rename from src/main/java/redis/clients/jedis/csc/GuavaCSC.java rename to src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java index a6f55d73dd..e1c9e4f434 100644 --- a/src/main/java/redis/clients/jedis/csc/GuavaCSC.java +++ b/src/main/java/redis/clients/jedis/csc/util/GuavaCSC.java @@ -1,18 +1,22 @@ -package redis.clients.jedis.csc; +package redis.clients.jedis.csc.util; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.hash.HashFunction; import com.google.common.hash.Hasher; import java.util.concurrent.TimeUnit; + import redis.clients.jedis.CommandObject; +import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.ClientSideCacheable; +import redis.clients.jedis.csc.DefaultClientSideCacheable; public class GuavaCSC extends ClientSideCache { private static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011(); private final Cache cache; - private final HashFunction function; + private final HashFunction hashFunction; public GuavaCSC(Cache guavaCache) { this(guavaCache, DEFAULT_HASH_FUNCTION); @@ -21,7 +25,7 @@ public GuavaCSC(Cache guavaCache) { public GuavaCSC(Cache guavaCache, HashFunction hashFunction) { super(); this.cache = guavaCache; - this.function = hashFunction; + this.hashFunction = hashFunction; } public GuavaCSC(Cache guavaCache, ClientSideCacheable cacheable) { @@ -31,7 +35,7 @@ public GuavaCSC(Cache guavaCache, ClientSideCacheable cacheable) { public GuavaCSC(Cache cache, HashFunction function, ClientSideCacheable cacheable) { super(cacheable); this.cache = cache; - this.function = function; + this.hashFunction = function; } @Override @@ -56,7 +60,7 @@ protected Object getValue(long hash) { @Override protected final long getHash(CommandObject command) { - Hasher hasher = function.newHasher(); + Hasher hasher = hashFunction.newHasher(); command.getArguments().forEach(raw -> hasher.putBytes(raw.getRaw())); hasher.putInt(command.getBuilder().hashCode()); return hasher.hash().asLong(); diff --git a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java b/src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java similarity index 93% rename from src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java rename to src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java index ee101bb2fc..5977d8b8bb 100644 --- a/src/main/java/redis/clients/jedis/csc/StringWhiteListBlackListClientSideCacheable.java +++ b/src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java @@ -1,7 +1,8 @@ -package redis.clients.jedis.csc; +package redis.clients.jedis.csc.util; import java.util.Set; import redis.clients.jedis.commands.ProtocolCommand; +import redis.clients.jedis.csc.ClientSideCacheable; public class StringWhiteListBlackListClientSideCacheable implements ClientSideCacheable { diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index f536eae9b2..e8e62ae9b5 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -6,9 +6,9 @@ import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; -import redis.clients.jedis.csc.GuavaCSC; -import redis.clients.jedis.csc.CaffeineCSC; import redis.clients.jedis.csc.ClientSideCache; +import redis.clients.jedis.csc.util.GuavaCSC; +import redis.clients.jedis.csc.util.CaffeineCSC; public final class JedisURIHelper { diff --git a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java index b9a2cf8d22..31589458f8 100644 --- a/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java +++ b/src/test/java/redis/clients/jedis/csc/ClientSideCacheLibsTest.java @@ -23,6 +23,8 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPooled; +import redis.clients.jedis.csc.util.CaffeineCSC; +import redis.clients.jedis.csc.util.GuavaCSC; public class ClientSideCacheLibsTest { diff --git a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java index ecb9802d31..d8f5174c26 100644 --- a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java @@ -21,6 +21,7 @@ import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.Protocol; +import redis.clients.jedis.csc.util.StringWhiteListBlackListClientSideCacheable; public class WhiteListBlackListClientSideCacheTest { From 5df309383fcffb07ec3b68f838e4e4e82a1887aa Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 6 Mar 2024 13:24:12 +0600 Subject: [PATCH 14/14] Rename to allow/deny instead of white/black --- .../util/AllowAndDenyListWithStringKeys.java | 36 ++++++++++++++++++ ...WhiteListBlackListClientSideCacheable.java | 37 ------------------- ... AllowAndDenyListClientSideCacheTest.java} | 14 +++---- 3 files changed, 43 insertions(+), 44 deletions(-) create mode 100644 src/main/java/redis/clients/jedis/csc/util/AllowAndDenyListWithStringKeys.java delete mode 100644 src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java rename src/test/java/redis/clients/jedis/csc/{WhiteListBlackListClientSideCacheTest.java => AllowAndDenyListClientSideCacheTest.java} (83%) diff --git a/src/main/java/redis/clients/jedis/csc/util/AllowAndDenyListWithStringKeys.java b/src/main/java/redis/clients/jedis/csc/util/AllowAndDenyListWithStringKeys.java new file mode 100644 index 0000000000..e9adbea37c --- /dev/null +++ b/src/main/java/redis/clients/jedis/csc/util/AllowAndDenyListWithStringKeys.java @@ -0,0 +1,36 @@ +package redis.clients.jedis.csc.util; + +import java.util.Set; +import redis.clients.jedis.commands.ProtocolCommand; +import redis.clients.jedis.csc.ClientSideCacheable; + +public class AllowAndDenyListWithStringKeys implements ClientSideCacheable { + + private final Set allowCommands; + private final Set denyCommands; + + private final Set allowKeys; + private final Set denyKeys; + + public AllowAndDenyListWithStringKeys(Set allowCommands, Set denyCommands, + Set allowKeys, Set denyKeys) { + this.allowCommands = allowCommands; + this.denyCommands = denyCommands; + this.allowKeys = allowKeys; + this.denyKeys = denyKeys; + } + + @Override + public boolean isCacheable(ProtocolCommand command, Object... keys) { + if (allowCommands != null && !allowCommands.contains(command)) return false; + if (denyCommands != null && denyCommands.contains(command)) return false; + + for (Object key : keys) { + if (!(key instanceof String)) return false; + if (allowKeys != null && !allowKeys.contains((String) key)) return false; + if (denyKeys != null && denyKeys.contains((String) key)) return false; + } + + return true; + } +} diff --git a/src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java b/src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java deleted file mode 100644 index 5977d8b8bb..0000000000 --- a/src/main/java/redis/clients/jedis/csc/util/StringWhiteListBlackListClientSideCacheable.java +++ /dev/null @@ -1,37 +0,0 @@ -package redis.clients.jedis.csc.util; - -import java.util.Set; -import redis.clients.jedis.commands.ProtocolCommand; -import redis.clients.jedis.csc.ClientSideCacheable; - -public class StringWhiteListBlackListClientSideCacheable implements ClientSideCacheable { - - private final Set whiteCommands; - private final Set blackCommands; - - private final Set whiteKeys; - private final Set blackKeys; - - public StringWhiteListBlackListClientSideCacheable( - Set whiteListCommands, Set blackListCommands, - Set whiteListKeys, Set blackListKeys) { - this.whiteCommands = whiteListCommands; - this.blackCommands = blackListCommands; - this.whiteKeys = whiteListKeys; - this.blackKeys = blackListKeys; - } - - @Override - public boolean isCacheable(ProtocolCommand command, Object... keys) { - if (whiteCommands != null && !whiteCommands.contains(command)) return false; - if (blackCommands != null && blackCommands.contains(command)) return false; - - for (Object key : keys) { - if (!(key instanceof String)) return false; - 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/AllowAndDenyListClientSideCacheTest.java similarity index 83% rename from src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java rename to src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java index d8f5174c26..86c6dbc42f 100644 --- a/src/test/java/redis/clients/jedis/csc/WhiteListBlackListClientSideCacheTest.java +++ b/src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java @@ -21,9 +21,9 @@ import redis.clients.jedis.JedisClientConfig; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.Protocol; -import redis.clients.jedis.csc.util.StringWhiteListBlackListClientSideCacheable; +import redis.clients.jedis.csc.util.AllowAndDenyListWithStringKeys; -public class WhiteListBlackListClientSideCacheTest { +public class AllowAndDenyListClientSideCacheTest { protected static final HostAndPort hnp = HostAndPorts.getRedisServers().get(1); @@ -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 StringWhiteListBlackListClientSideCacheable(null, null, null, null)), + new MapCSC(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 StringWhiteListBlackListClientSideCacheable(singleton(Protocol.Command.GET), null, null, null)), + new MapCSC(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 StringWhiteListBlackListClientSideCacheable(null, singleton(Protocol.Command.GET), null, null)), + new MapCSC(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 StringWhiteListBlackListClientSideCacheable(null, null, singleton("foo"), null)), + new MapCSC(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 StringWhiteListBlackListClientSideCacheable(null, null, null, singleton("foo"))), + new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))), singleConnectionPoolConfig.get())) { control.set("foo", "bar"); assertThat(map, Matchers.aMapWithSize(0));