Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove client side cache support through uri/url #3892

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public UnifiedJedis(final URI uri) {
this(JedisURIHelper.getHostAndPort(uri), DefaultJedisClientConfig.builder()
.user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri))
.database(JedisURIHelper.getDBIndex(uri)).protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build(), JedisURIHelper.getClientSideCache(uri));
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).build());
}

public UnifiedJedis(final URI uri, JedisClientConfig config) {
Expand All @@ -87,8 +87,7 @@ public UnifiedJedis(final URI uri, JedisClientConfig config) {
.database(JedisURIHelper.getDBIndex(uri)).clientName(config.getClientName())
.protocol(JedisURIHelper.getRedisProtocol(uri))
.ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(config.getSslSocketFactory())
.sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier())
.build(), JedisURIHelper.getClientSideCache(uri));
.sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier()).build());
}

public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) {
Expand Down
82 changes: 0 additions & 82 deletions src/main/java/redis/clients/jedis/util/JedisURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.RedisProtocol;
import redis.clients.jedis.annots.Experimental;
import redis.clients.jedis.csc.CaffeineClientSideCache;
import redis.clients.jedis.csc.ClientSideCache;
import redis.clients.jedis.csc.GuavaClientSideCache;

public final class JedisURIHelper {

Expand Down Expand Up @@ -75,84 +71,6 @@ public static RedisProtocol getRedisProtocol(URI uri) {
return null; // null (default) when not defined
}

private static final Integer ZERO_INTEGER = 0;

@Experimental
public static ClientSideCache getClientSideCache(URI uri) {
if (uri.getQuery() == null) return null;

boolean guava = false, caffeine = false; // cache_lib
Integer maxSize = null; // cache_max_size --> 0 = disbale
Integer ttl = null; // cache_ttl --> 0 = no ttl
// cache-max-idle

String[] params = uri.getQuery().split("&");
for (String param : params) {
int idx = param.indexOf("=");
if (idx < 0) continue;

String key = param.substring(0, idx);
String val = param.substring(idx + 1);

switch (key) {

case "cache_lib":
switch (val) {
case "guava":
guava = true;
break;
case "caffeine":
caffeine = true;
break;
default:
throw new IllegalArgumentException("Unsupported library " + val);
}
break;

case "cache_max_size":
try {
maxSize = Integer.parseInt(val);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Value of cache_max_size must be an integer (no of commands).", nfe);
}
break;

case "cache_ttl":
try {
ttl = Integer.parseInt(val);
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("Value of cache_ttl must be an integer (in seconds).", nfe);
}
break;
}
}

// special cases
if (ZERO_INTEGER.equals(maxSize)) {
return null;
}
if (!guava && !caffeine && (maxSize != null || ttl != null)) {
throw new IllegalArgumentException("A supported caching library (guava OR caffeine) must be selected.");
}
if (ZERO_INTEGER.equals(ttl)) {
ttl = null; // below, only null will be checked
}

if (guava) {
GuavaClientSideCache.Builder guavaBuilder = GuavaClientSideCache.builder();
if (maxSize != null) guavaBuilder.maximumSize(maxSize);
if (ttl != null) guavaBuilder.ttl(ttl);
return guavaBuilder.build();
} else if (caffeine) {
CaffeineClientSideCache.Builder caffeineBuilder = CaffeineClientSideCache.builder();
if (maxSize != null) caffeineBuilder.maximumSize(maxSize);
if (ttl != null) caffeineBuilder.ttl(ttl);
return caffeineBuilder.build();
}

return null; // null (default) when not defined
}

public static boolean isValid(URI uri) {
if (isEmpty(uri.getScheme()) || isEmpty(uri.getHost()) || uri.getPort() == -1) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,4 @@ public void timeToLive() throws InterruptedException {
assertThat(caffeine.stats().evictionCount(), Matchers.equalTo((long) count));
}

@Test
public void uriSimple() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(CaffeineClientSideCache.class));
}

@Test
public void uriAllParams() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine&cache_max_size=1000&cache_ttl=10");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(CaffeineClientSideCache.class));
}

@Test
public void uriMaxSizeZeroMeansNull() {
URI uri = URI.create(baseUrl + "?cache_lib=caffeine&cache_max_size=0");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.nullValue());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,4 @@ public void multiKeyOperation() {
}
}

@Test
public void uriNoParam() {
URI uri = URI.create(baseUrl + "?");
assertNull(JedisURIHelper.getClientSideCache(uri));
}

@Test
public void uriUnknownLib() {
URI uri = URI.create(baseUrl + "?cache_lib=unknown");
IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
() -> JedisURIHelper.getClientSideCache(uri));
assertEquals("Unsupported library unknown", iae.getMessage());
}

@Test
public void uriNoLib() {
String[] otherParams
= new String[]{
"?cache_max_size=1000",
"?cache_ttl=10",
"?cache_max_size=1000&cache_ttl=10"
};
Arrays.stream(otherParams).forEach(urlParams -> {
URI uri = URI.create(baseUrl + urlParams);
IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
() -> JedisURIHelper.getClientSideCache(uri));
assertEquals("A supported caching library (guava OR caffeine) must be selected.", iae.getMessage());
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,4 @@ public void timeToLive() throws InterruptedException {
assertThat(guava.stats().evictionCount(), Matchers.equalTo((long) count));
}

@Test
public void uriSimple() {
URI uri = URI.create(baseUrl + "?cache_lib=guava");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(GuavaClientSideCache.class));
}

@Test
public void uriAllParams() {
URI uri = URI.create(baseUrl + "?cache_lib=guava&cache_max_size=1000&cache_ttl=10");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.instanceOf(GuavaClientSideCache.class));
}

@Test
public void uriMaxSizeZeroMeansNull() {
URI uri = URI.create(baseUrl + "?cache_lib=guava&cache_max_size=0");
ClientSideCache cache = JedisURIHelper.getClientSideCache(uri);
assertThat(cache, Matchers.nullValue());
}

}
Loading