Skip to content

Commit fc137c0

Browse files
committed
- added 'Cache' interface and 'DefaultCache' implementation in regard to design doc
- added 'EvictionPolicy' interface and LRU implementation - move cache object validation and cache control stuf from 'ClientSideCache' into 'CacheConnection' - make guava and caffeine caches experimental
1 parent f7c4fbf commit fc137c0

29 files changed

+699
-293
lines changed

src/main/java/redis/clients/jedis/ConnectionFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import org.slf4j.LoggerFactory;
88

99
import redis.clients.jedis.annots.Experimental;
10+
import redis.clients.jedis.csc.Cache;
1011
import redis.clients.jedis.csc.CacheConnection;
11-
import redis.clients.jedis.csc.ClientSideCache;
1212
import redis.clients.jedis.exceptions.JedisException;
1313

1414
/**
@@ -20,7 +20,7 @@ public class ConnectionFactory implements PooledObjectFactory<Connection> {
2020

2121
private final JedisSocketFactory jedisSocketFactory;
2222
private final JedisClientConfig clientConfig;
23-
private ClientSideCache clientSideCache = null;
23+
private Cache clientSideCache = null;
2424

2525
public ConnectionFactory(final HostAndPort hostAndPort) {
2626
this.clientConfig = DefaultJedisClientConfig.builder().build();
@@ -33,7 +33,7 @@ public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig
3333
}
3434

3535
@Experimental
36-
public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache) {
36+
public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache csCache) {
3737
this.clientConfig = clientConfig;
3838
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig);
3939
this.clientSideCache = csCache;

src/main/java/redis/clients/jedis/ConnectionPool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.apache.commons.pool2.PooledObjectFactory;
44
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
55
import redis.clients.jedis.annots.Experimental;
6-
import redis.clients.jedis.csc.ClientSideCache;
6+
import redis.clients.jedis.csc.Cache;
77
import redis.clients.jedis.util.Pool;
88

99
public class ConnectionPool extends Pool<Connection> {
@@ -13,7 +13,7 @@ public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
1313
}
1414

1515
@Experimental
16-
public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache) {
16+
public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache clientSideCache) {
1717
this(new ConnectionFactory(hostAndPort, clientConfig, clientSideCache));
1818
}
1919

@@ -27,7 +27,7 @@ public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig,
2727
}
2828

2929
@Experimental
30-
public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache,
30+
public ConnectionPool(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache clientSideCache,
3131
GenericObjectPoolConfig<Connection> poolConfig) {
3232
this(new ConnectionFactory(hostAndPort, clientConfig, clientSideCache), poolConfig);
3333
}

src/main/java/redis/clients/jedis/JedisCluster.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
99
import redis.clients.jedis.annots.Experimental;
1010
import redis.clients.jedis.providers.ClusterConnectionProvider;
11-
import redis.clients.jedis.csc.ClientSideCache;
11+
import redis.clients.jedis.csc.Cache;
1212
import redis.clients.jedis.util.JedisClusterCRC16;
1313

1414
public class JedisCluster extends UnifiedJedis {
@@ -218,35 +218,35 @@ private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Durati
218218
}
219219

220220
@Experimental
221-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache) {
221+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache) {
222222
this(clusterNodes, clientConfig, clientSideCache, DEFAULT_MAX_ATTEMPTS,
223223
Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis()));
224224
}
225225

226226
@Experimental
227-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache,
227+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache,
228228
int maxAttempts, Duration maxTotalRetriesDuration) {
229229
this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache), maxAttempts, maxTotalRetriesDuration,
230230
clientConfig.getRedisProtocol(), clientSideCache);
231231
}
232232

233233
@Experimental
234-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache,
234+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache,
235235
int maxAttempts, Duration maxTotalRetriesDuration, GenericObjectPoolConfig<Connection> poolConfig) {
236236
this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig),
237237
maxAttempts, maxTotalRetriesDuration, clientConfig.getRedisProtocol(), clientSideCache);
238238
}
239239

240240
@Experimental
241-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache,
241+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache,
242242
GenericObjectPoolConfig<Connection> poolConfig) {
243243
this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig),
244244
DEFAULT_MAX_ATTEMPTS, Duration.ofMillis(DEFAULT_MAX_ATTEMPTS * clientConfig.getSocketTimeoutMillis()),
245245
clientConfig.getRedisProtocol(), clientSideCache);
246246
}
247247

248248
@Experimental
249-
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, ClientSideCache clientSideCache,
249+
public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfig, Cache clientSideCache,
250250
GenericObjectPoolConfig<Connection> poolConfig, Duration topologyRefreshPeriod, int maxAttempts,
251251
Duration maxTotalRetriesDuration) {
252252
this(new ClusterConnectionProvider(clusterNodes, clientConfig, clientSideCache, poolConfig, topologyRefreshPeriod),
@@ -255,7 +255,7 @@ public JedisCluster(Set<HostAndPort> clusterNodes, JedisClientConfig clientConfi
255255

256256
@Experimental
257257
private JedisCluster(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration,
258-
RedisProtocol protocol, ClientSideCache clientSideCache) {
258+
RedisProtocol protocol, Cache clientSideCache) {
259259
super(provider, maxAttempts, maxTotalRetriesDuration, protocol, clientSideCache);
260260
}
261261

src/main/java/redis/clients/jedis/JedisClusterInfoCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import redis.clients.jedis.annots.Experimental;
2727
import redis.clients.jedis.annots.Internal;
28-
import redis.clients.jedis.csc.ClientSideCache;
28+
import redis.clients.jedis.csc.Cache;
2929
import redis.clients.jedis.exceptions.JedisClusterOperationException;
3030
import redis.clients.jedis.exceptions.JedisException;
3131
import redis.clients.jedis.util.SafeEncoder;
@@ -48,7 +48,7 @@ public class JedisClusterInfoCache {
4848

4949
private final GenericObjectPoolConfig<Connection> poolConfig;
5050
private final JedisClientConfig clientConfig;
51-
private final ClientSideCache clientSideCache;
51+
private final Cache clientSideCache;
5252
private final Set<HostAndPort> startNodes;
5353

5454
private static final int MASTER_NODE_INDEX = 2;
@@ -72,7 +72,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig, final Set<Hos
7272
}
7373

7474
@Experimental
75-
public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache clientSideCache,
75+
public JedisClusterInfoCache(final JedisClientConfig clientConfig, Cache clientSideCache,
7676
final Set<HostAndPort> startNodes) {
7777
this(clientConfig, clientSideCache, null, startNodes);
7878
}
@@ -83,7 +83,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig,
8383
}
8484

8585
@Experimental
86-
public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache clientSideCache,
86+
public JedisClusterInfoCache(final JedisClientConfig clientConfig, Cache clientSideCache,
8787
final GenericObjectPoolConfig<Connection> poolConfig, final Set<HostAndPort> startNodes) {
8888
this(clientConfig, clientSideCache, poolConfig, startNodes, null);
8989
}
@@ -95,7 +95,7 @@ public JedisClusterInfoCache(final JedisClientConfig clientConfig,
9595
}
9696

9797
@Experimental
98-
public JedisClusterInfoCache(final JedisClientConfig clientConfig, ClientSideCache clientSideCache,
98+
public JedisClusterInfoCache(final JedisClientConfig clientConfig, Cache clientSideCache,
9999
final GenericObjectPoolConfig<Connection> poolConfig, final Set<HostAndPort> startNodes,
100100
final Duration topologyRefreshPeriod) {
101101
this.poolConfig = poolConfig;

src/main/java/redis/clients/jedis/JedisPooled.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.commons.pool2.PooledObjectFactory;
99
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
1010
import redis.clients.jedis.annots.Experimental;
11-
import redis.clients.jedis.csc.ClientSideCache;
11+
import redis.clients.jedis.csc.Cache;
1212
import redis.clients.jedis.providers.PooledConnectionProvider;
1313
import redis.clients.jedis.util.JedisURIHelper;
1414
import redis.clients.jedis.util.Pool;
@@ -78,7 +78,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client
7878
}
7979

8080
@Experimental
81-
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache clientSideCache) {
81+
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache) {
8282
super(hostAndPort, clientConfig, clientSideCache);
8383
}
8484

@@ -383,7 +383,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client
383383
}
384384

385385
@Experimental
386-
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache clientSideCache,
386+
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache clientSideCache,
387387
final GenericObjectPoolConfig<Connection> poolConfig) {
388388
super(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache, poolConfig),
389389
clientConfig.getRedisProtocol(), clientSideCache);

src/main/java/redis/clients/jedis/JedisSentineled.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Set;
44
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
55
import redis.clients.jedis.annots.Experimental;
6-
import redis.clients.jedis.csc.ClientSideCache;
6+
import redis.clients.jedis.csc.Cache;
77
import redis.clients.jedis.providers.SentineledConnectionProvider;
88

99
public class JedisSentineled extends UnifiedJedis {
@@ -15,7 +15,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo
1515
}
1616

1717
@Experimental
18-
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache clientSideCache,
18+
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache,
1919
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig) {
2020
super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache,
2121
sentinels, sentinelClientConfig), masterClientConfig.getRedisProtocol(), clientSideCache);
@@ -29,7 +29,7 @@ public JedisSentineled(String masterName, final JedisClientConfig masterClientCo
2929
}
3030

3131
@Experimental
32-
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, ClientSideCache clientSideCache,
32+
public JedisSentineled(String masterName, final JedisClientConfig masterClientConfig, Cache clientSideCache,
3333
final GenericObjectPoolConfig<Connection> poolConfig,
3434
Set<HostAndPort> sentinels, final JedisClientConfig sentinelClientConfig) {
3535
super(new SentineledConnectionProvider(masterName, masterClientConfig, clientSideCache, poolConfig,

src/main/java/redis/clients/jedis/Protocol.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import redis.clients.jedis.exceptions.*;
1313
import redis.clients.jedis.args.Rawable;
1414
import redis.clients.jedis.commands.ProtocolCommand;
15-
import redis.clients.jedis.csc.ClientSideCache;
15+
import redis.clients.jedis.csc.Cache;
1616
import redis.clients.jedis.util.KeyValue;
1717
import redis.clients.jedis.util.RedisInputStream;
1818
import redis.clients.jedis.util.RedisOutputStream;
@@ -214,7 +214,7 @@ public static Object read(final RedisInputStream is) {
214214
}
215215

216216
@Experimental
217-
public static void readPushes(final RedisInputStream is, final ClientSideCache cache, boolean onlyPendingBuffer) {
217+
public static void readPushes(final RedisInputStream is, final Cache cache, boolean onlyPendingBuffer) {
218218
if (onlyPendingBuffer) {
219219
try {
220220
while (is.available() > 0 && is.peek(GREATER_THAN_BYTE)) {
@@ -234,11 +234,11 @@ public static void readPushes(final RedisInputStream is, final ClientSideCache c
234234
}
235235
}
236236

237-
private static void processPush(final RedisInputStream is, ClientSideCache cache) {
237+
private static void processPush(final RedisInputStream is, Cache cache) {
238238
List<Object> list = processMultiBulkReply(is);
239239
if (list.size() == 2 && list.get(0) instanceof byte[]
240240
&& Arrays.equals(INVALIDATE_BYTES, (byte[]) list.get(0))) {
241-
cache.invalidate((List) list.get(1));
241+
cache.deleteByRedisKeys((List) list.get(1));
242242
}
243243
}
244244

src/main/java/redis/clients/jedis/UnifiedJedis.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import redis.clients.jedis.commands.SampleBinaryKeyedCommands;
2020
import redis.clients.jedis.commands.SampleKeyedCommands;
2121
import redis.clients.jedis.commands.RedisModuleCommands;
22-
import redis.clients.jedis.csc.ClientSideCache;
22+
import redis.clients.jedis.csc.Cache;
2323
import redis.clients.jedis.exceptions.JedisException;
2424
import redis.clients.jedis.executors.*;
2525
import redis.clients.jedis.gears.TFunctionListParams;
@@ -95,7 +95,7 @@ public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
9595
}
9696

9797
@Experimental
98-
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache) {
98+
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, Cache clientSideCache) {
9999
this(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache), clientConfig.getRedisProtocol(),
100100
clientSideCache);
101101
}
@@ -109,7 +109,7 @@ protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol) {
109109
}
110110

111111
@Experimental
112-
protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, ClientSideCache clientSideCache) {
112+
protected UnifiedJedis(ConnectionProvider provider, RedisProtocol protocol, Cache clientSideCache) {
113113
this(new DefaultCommandExecutor(provider), provider, new CommandObjects(), protocol, clientSideCache);
114114
}
115115

@@ -183,7 +183,7 @@ protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Dura
183183

184184
@Experimental
185185
protected UnifiedJedis(ClusterConnectionProvider provider, int maxAttempts, Duration maxTotalRetriesDuration,
186-
RedisProtocol protocol, ClientSideCache clientSideCache) {
186+
RedisProtocol protocol, Cache clientSideCache) {
187187
this(new ClusterCommandExecutor(provider, maxAttempts, maxTotalRetriesDuration), provider,
188188
new ClusterCommandObjects(), protocol, clientSideCache);
189189
}
@@ -254,12 +254,12 @@ public UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, Comma
254254
@Experimental
255255
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects,
256256
RedisProtocol protocol) {
257-
this(executor, provider, commandObjects, protocol, (ClientSideCache) null);
257+
this(executor, provider, commandObjects, protocol, (Cache) null);
258258
}
259259

260260
@Experimental
261261
private UnifiedJedis(CommandExecutor executor, ConnectionProvider provider, CommandObjects commandObjects,
262-
RedisProtocol protocol, ClientSideCache clientSideCache) {
262+
RedisProtocol protocol, Cache clientSideCache) {
263263

264264
if (clientSideCache != null && protocol != RedisProtocol.RESP3) {
265265
throw new IllegalArgumentException("Client-side caching is only supported with RESP3.");
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package redis.clients.jedis.csc;
2+
3+
import java.util.Collection;
4+
import java.util.List;
5+
6+
/**
7+
* The cache that is used by a connection
8+
*/
9+
public interface Cache {
10+
11+
/**
12+
* @return The size of the cache
13+
*/
14+
int getMaxSize();
15+
16+
/**
17+
* @return The current size of the cache
18+
*/
19+
int getSize();
20+
21+
/**
22+
* @return All the entries within the cache
23+
*/
24+
Collection<CacheEntry> getCacheEntries();
25+
26+
/**
27+
* Fetches a value from the cache
28+
*
29+
* @param cacheKey The key within the cache
30+
* @return The entry within the cache
31+
*/
32+
CacheEntry get(CacheKey cacheKey);
33+
34+
/**
35+
* Puts a value into the cache
36+
*
37+
* @param cacheKey The key by which the value can be accessed within the cache
38+
* @param value The value to be put into the cache
39+
* @return The cache entry
40+
*/
41+
CacheEntry set(CacheKey cacheKey, CacheEntry value);
42+
43+
/**
44+
* Delete an entry by cache key
45+
* @param cacheKey The cache key of the entry in the cache
46+
* @return True if the entry could be deleted, false if the entry wasn't found.
47+
*/
48+
Boolean delete(CacheKey cacheKey);
49+
50+
/**
51+
* Delete entries by cache key from the cache
52+
*
53+
* @param cacheKeys The cache keys of the entries that should be deleted
54+
* @return True for every entry that could be deleted. False if the entry was not there.
55+
*/
56+
List<Boolean> delete(List<CacheKey> cacheKeys);
57+
58+
/**
59+
* Delete an entry by the Redis key from the cache
60+
*
61+
* @param key The Redis key as binary
62+
* @return True if the entry could be deleted. False if the entry was not there.
63+
*/
64+
List<CacheKey> deleteByRedisKey(Object key);
65+
66+
/**
67+
* Delete entries by the Redis key from the cache
68+
*
69+
* @param keys The Redis keys as binaries
70+
* @return True for every entry that could be deleted. False if the entry was not there.
71+
*/
72+
List<CacheKey> deleteByRedisKeys(List keys);
73+
74+
/**
75+
* Flushes the entire cache
76+
*
77+
* @return Return the number of entries that were flushed
78+
*/
79+
int flush();
80+
81+
/**
82+
* @param cacheKey The key of the cache entry
83+
* @return True if the entry is cachable, false otherwise
84+
*/
85+
Boolean isCacheable(CacheKey cacheKey);
86+
87+
/**
88+
*
89+
* @param cacheKey The key of the cache entry
90+
* @return True if the cache already contains the key
91+
*/
92+
Boolean hasCacheKey(CacheKey cacheKey);
93+
94+
/**
95+
* @return The eviction policy that is used by the cache
96+
*/
97+
EvictionPolicy getEvictionPolicy();
98+
}

0 commit comments

Comments
 (0)