Skip to content

Commit 2480b02

Browse files
authored
Support Client-side caching through URI/URL (#3703)
* Support Client-side caching through URI/URL * check idx of '=' sign * nicer exception * edit/fix condition * rename param * Throw IllegalArgumentException at all such cases
1 parent 4cada22 commit 2480b02

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public JedisPooled() {
2626
* @param url
2727
*/
2828
public JedisPooled(final String url) {
29-
this(URI.create(url));
29+
super(url);
3030
}
3131

3232
/**
@@ -76,7 +76,7 @@ public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig client
7676
}
7777

7878
public JedisPooled(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, ClientSideCache csCache) {
79-
super(new PooledConnectionProvider(hostAndPort, clientConfig, csCache), clientConfig.getRedisProtocol(), csCache);
79+
super(hostAndPort, clientConfig, csCache);
8080
}
8181

8282
public JedisPooled(PooledObjectFactory<Connection> factory) {

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public UnifiedJedis(final URI uri) {
7272
this(JedisURIHelper.getHostAndPort(uri), DefaultJedisClientConfig.builder()
7373
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
7474
.database(JedisURIHelper.getDBIndex(uri)).protocol(JedisURIHelper.getRedisProtocol(uri))
75-
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build());
75+
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build(), JedisURIHelper.getClientSideCache(uri));
7676
}
7777

7878
public UnifiedJedis(final URI uri, JedisClientConfig config) {
@@ -85,13 +85,17 @@ public UnifiedJedis(final URI uri, JedisClientConfig config) {
8585
.protocol(JedisURIHelper.getRedisProtocol(uri))
8686
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(config.getSslSocketFactory())
8787
.sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier())
88-
.build());
88+
.build(), JedisURIHelper.getClientSideCache(uri));
8989
}
9090

9191
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
9292
this(new PooledConnectionProvider(hostAndPort, clientConfig), clientConfig.getRedisProtocol());
9393
}
9494

95+
public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache) {
96+
this(new PooledConnectionProvider(hostAndPort, clientConfig, clientSideCache), clientConfig.getRedisProtocol(), clientSideCache);
97+
}
98+
9599
public UnifiedJedis(ConnectionProvider provider) {
96100
this(new DefaultCommandExecutor(provider), provider);
97101
}

src/main/java/redis/clients/jedis/util/JedisURIHelper.java

+84-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redis.clients.jedis.util;
22

33
import java.net.URI;
4+
import redis.clients.jedis.ClientSideCache;
45
import redis.clients.jedis.HostAndPort;
56
import redis.clients.jedis.Protocol;
67
import redis.clients.jedis.RedisProtocol;
@@ -54,11 +55,12 @@ public static int getDBIndex(URI uri) {
5455
public static RedisProtocol getRedisProtocol(URI uri) {
5556
if (uri.getQuery() == null) return null;
5657

57-
String[] pairs = uri.getQuery().split("&");
58-
for (String pair : pairs) {
59-
int idx = pair.indexOf("=");
60-
if ("protocol".equals(pair.substring(0, idx))) {
61-
String ver = pair.substring(idx + 1);
58+
String[] params = uri.getQuery().split("&");
59+
for (String param : params) {
60+
int idx = param.indexOf("=");
61+
if (idx < 0) continue;
62+
if ("protocol".equals(param.substring(0, idx))) {
63+
String ver = param.substring(idx + 1);
6264
for (RedisProtocol proto : RedisProtocol.values()) {
6365
if (proto.version().equals(ver)) {
6466
return proto;
@@ -70,6 +72,83 @@ public static RedisProtocol getRedisProtocol(URI uri) {
7072
return null; // null (default) when not defined
7173
}
7274

75+
private static final Integer ZERO_INTEGER = 0;
76+
77+
public static ClientSideCache getClientSideCache(URI uri) {
78+
if (uri.getQuery() == null) return null;
79+
80+
boolean guava = false, caffeine = false; // cache_lib
81+
Integer maxSize = null; // cache_max_size --> 0 = disbale
82+
Integer ttl = null; // cache_ttl --> 0 = no ttl
83+
// cache-max-idle
84+
85+
String[] params = uri.getQuery().split("&");
86+
for (String param : params) {
87+
int idx = param.indexOf("=");
88+
if (idx < 0) continue;
89+
90+
String key = param.substring(0, idx);
91+
String val = param.substring(idx + 1);
92+
93+
switch (key) {
94+
95+
case "cache_lib":
96+
switch (val) {
97+
case "guava":
98+
guava = true;
99+
break;
100+
case "caffeine":
101+
caffeine = true;
102+
break;
103+
default:
104+
throw new IllegalArgumentException("Unsupported library " + val);
105+
}
106+
break;
107+
108+
case "cache_max_size":
109+
try {
110+
maxSize = Integer.parseInt(val);
111+
} catch (NumberFormatException nfe) {
112+
throw new IllegalArgumentException("Value of cache_max_size must be an integer.", nfe);
113+
}
114+
break;
115+
116+
case "cache_ttl":
117+
try {
118+
ttl = Integer.parseInt(val);
119+
} catch (NumberFormatException nfe) {
120+
throw new IllegalArgumentException("Value of cache_ttl must be an integer denoting seconds.", nfe);
121+
}
122+
break;
123+
}
124+
}
125+
126+
// special cases
127+
if (ZERO_INTEGER.equals(maxSize)) {
128+
return null;
129+
}
130+
if (!guava && !caffeine && (maxSize != null || ttl != null)) {
131+
throw new IllegalArgumentException("The cache library (guava OR caffeine) must be selected.");
132+
}
133+
if (ZERO_INTEGER.equals(ttl)) {
134+
ttl = null; // below, only null will be checked
135+
}
136+
137+
if (guava) {
138+
GuavaCSC.Builder guavaBuilder = GuavaCSC.builder();
139+
if (maxSize != null) guavaBuilder.maximumSize(maxSize);
140+
if (ttl != null) guavaBuilder.ttl(ttl);
141+
return guavaBuilder.build();
142+
} else if (caffeine) {
143+
CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder();
144+
if (maxSize != null) caffeineBuilder.maximumSize(maxSize);
145+
if (ttl != null) caffeineBuilder.ttl(ttl);
146+
return caffeineBuilder.build();
147+
}
148+
149+
return null; // null (default) when not defined
150+
}
151+
73152
public static boolean isValid(URI uri) {
74153
if (isEmpty(uri.getScheme()) || isEmpty(uri.getHost()) || uri.getPort() == -1) {
75154
return false;

0 commit comments

Comments
 (0)