Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec395b5

Browse files
committed
Support Client-side caching through URI/URL
1 parent d3acff2 commit ec395b5

File tree

3 files changed

+81
-9
lines changed

3 files changed

+81
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 73 additions & 5 deletions
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,11 @@ 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 ("protocol".equals(param.substring(0, idx))) {
62+
String ver = param.substring(idx + 1);
6263
for (RedisProtocol proto : RedisProtocol.values()) {
6364
if (proto.version().equals(ver)) {
6465
return proto;
@@ -70,6 +71,73 @@ public static RedisProtocol getRedisProtocol(URI uri) {
7071
return null; // null (default) when not defined
7172
}
7273

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

0 commit comments

Comments
 (0)