Skip to content

Commit 4b82cae

Browse files
authored
Merge e5a7659 into c02e5be
2 parents c02e5be + e5a7659 commit 4b82cae

16 files changed

+106
-103
lines changed

Diff for: src/main/java/redis/clients/jedis/csc/CaffeineCSC.java renamed to src/main/java/redis/clients/jedis/csc/CaffeineClientSideCache.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
import com.github.benmanes.caffeine.cache.Caffeine;
55
import java.util.concurrent.TimeUnit;
66

7-
import redis.clients.jedis.csc.hash.CommandLongHashing;
8-
import redis.clients.jedis.csc.hash.OpenHftHashing;
7+
import redis.clients.jedis.csc.hash.CommandLongHasher;
8+
import redis.clients.jedis.csc.hash.OpenHftCommandHasher;
99

10-
public class CaffeineCSC extends ClientSideCache {
10+
public class CaffeineClientSideCache extends ClientSideCache {
1111

1212
private final Cache<Long, Object> cache;
1313

14-
public CaffeineCSC(Cache<Long, Object> caffeineCache) {
15-
this(caffeineCache, new OpenHftHashing(OpenHftHashing.DEFAULT_HASH_FUNCTION), DefaultClientSideCacheable.INSTANCE);
14+
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache) {
15+
this(caffeineCache, DefaultClientSideCacheable.INSTANCE);
1616
}
1717

18-
public CaffeineCSC(Cache<Long, Object> caffeineCache, ClientSideCacheable cacheable) {
19-
this(caffeineCache, new OpenHftHashing(OpenHftHashing.DEFAULT_HASH_FUNCTION), cacheable);
18+
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, ClientSideCacheable cacheable) {
19+
this(caffeineCache, new OpenHftCommandHasher(OpenHftCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
2020
}
2121

22-
public CaffeineCSC(Cache<Long, Object> caffeineCache, CommandLongHashing hashing, ClientSideCacheable cacheable) {
23-
super(hashing, cacheable);
22+
public CaffeineClientSideCache(Cache<Long, Object> caffeineCache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
23+
super(commandHasher, cacheable);
2424
this.cache = caffeineCache;
2525
}
2626

@@ -55,7 +55,7 @@ public static class Builder {
5555
private final TimeUnit expireTimeUnit = TimeUnit.SECONDS;
5656

5757
// not using a default value to avoid an object creation like 'new OpenHftHashing(hashFunction)'
58-
private CommandLongHashing longHashing = null;
58+
private CommandLongHasher commandHasher = null;
5959

6060
private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;
6161

@@ -71,8 +71,8 @@ public Builder ttl(int seconds) {
7171
return this;
7272
}
7373

74-
public Builder hashing(CommandLongHashing hashing) {
75-
this.longHashing = hashing;
74+
public Builder commandHasher(CommandLongHasher commandHasher) {
75+
this.commandHasher = commandHasher;
7676
return this;
7777
}
7878

@@ -81,16 +81,15 @@ public Builder cacheable(ClientSideCacheable cacheable) {
8181
return this;
8282
}
8383

84-
public CaffeineCSC build() {
84+
public CaffeineClientSideCache build() {
8585
Caffeine cb = Caffeine.newBuilder();
8686

8787
cb.maximumSize(maximumSize);
8888

8989
cb.expireAfterWrite(expireTime, expireTimeUnit);
9090

91-
return longHashing != null
92-
? new CaffeineCSC(cb.build(), longHashing, cacheable)
93-
: new CaffeineCSC(cb.build(), cacheable);
91+
return commandHasher != null ? new CaffeineClientSideCache(cb.build(), commandHasher, cacheable)
92+
: new CaffeineClientSideCache(cb.build(), cacheable);
9493
}
9594
}
9695
}

Diff for: src/main/java/redis/clients/jedis/csc/ClientSideCache.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@
77
import java.util.Set;
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.function.Function;
10+
1011
import redis.clients.jedis.CommandObject;
11-
import redis.clients.jedis.csc.hash.CommandLongHashing;
12+
import redis.clients.jedis.csc.hash.CommandLongHasher;
1213
import redis.clients.jedis.util.SafeEncoder;
1314

1415
/**
1516
* The class to manage the client-side caching. User can provide any of implementation of this class to the client
16-
* object; e.g. {@link redis.clients.jedis.csc.CaffeineCSC CaffeineCSC} or
17-
* {@link redis.clients.jedis.csc.GuavaCSC GuavaCSC} or a custom implementation of their own.
17+
* object; e.g. {@link redis.clients.jedis.csc.CaffeineClientSideCache CaffeineClientSideCache} or
18+
* {@link redis.clients.jedis.csc.GuavaClientSideCache GuavaClientSideCache} or a custom implementation of their own.
1819
*/
1920
public abstract class ClientSideCache {
2021

2122
protected static final int DEFAULT_MAXIMUM_SIZE = 10_000;
2223
protected static final int DEFAULT_EXPIRE_SECONDS = 100;
2324

2425
private final Map<ByteBuffer, Set<Long>> keyToCommandHashes = new ConcurrentHashMap<>();
25-
private final CommandLongHashing commandHashing;
26+
private final CommandLongHasher commandHasher;
2627
private final ClientSideCacheable cacheable;
2728

28-
protected ClientSideCache(CommandLongHashing commandHashing) {
29-
this(commandHashing, DefaultClientSideCacheable.INSTANCE);
29+
protected ClientSideCache(CommandLongHasher commandHasher) {
30+
this(commandHasher, DefaultClientSideCacheable.INSTANCE);
3031
}
3132

32-
protected ClientSideCache(CommandLongHashing commandHashing, ClientSideCacheable cacheable) {
33-
this.commandHashing = commandHashing;
33+
protected ClientSideCache(CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
34+
this.commandHasher = commandHasher;
3435
this.cacheable = cacheable;
3536
}
3637

@@ -80,7 +81,7 @@ public final <T> T get(Function<CommandObject<T>, T> loader, CommandObject<T> co
8081
return loader.apply(command);
8182
}
8283

83-
final long hash = commandHashing.hash(command);
84+
final long hash = commandHasher.hash(command);
8485

8586
T value = (T) getValue(hash);
8687
if (value != null) {

Diff for: src/main/java/redis/clients/jedis/csc/GuavaCSC.java renamed to src/main/java/redis/clients/jedis/csc/GuavaClientSideCache.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@
55
import com.google.common.hash.HashFunction;
66
import java.util.concurrent.TimeUnit;
77

8-
import redis.clients.jedis.csc.hash.CommandLongHashing;
9-
import redis.clients.jedis.csc.hash.GuavaHashing;
8+
import redis.clients.jedis.csc.hash.CommandLongHasher;
9+
import redis.clients.jedis.csc.hash.GuavaCommandHasher;
1010

11-
public class GuavaCSC extends ClientSideCache {
11+
public class GuavaClientSideCache extends ClientSideCache {
1212

1313
private final Cache<Long, Object> cache;
1414

15-
public GuavaCSC(Cache<Long, Object> guavaCache) {
16-
this(guavaCache, GuavaHashing.DEFAULT_HASH_FUNCTION);
15+
public GuavaClientSideCache(Cache<Long, Object> guavaCache) {
16+
this(guavaCache, GuavaCommandHasher.DEFAULT_HASH_FUNCTION);
1717
}
1818

19-
public GuavaCSC(Cache<Long, Object> guavaCache, HashFunction hashFunction) {
20-
this(guavaCache, new GuavaHashing(hashFunction));
19+
public GuavaClientSideCache(Cache<Long, Object> guavaCache, HashFunction hashFunction) {
20+
this(guavaCache, new GuavaCommandHasher(hashFunction));
2121
}
2222

23-
public GuavaCSC(Cache<Long, Object> guavaCache, CommandLongHashing hashing) {
24-
super(hashing);
23+
public GuavaClientSideCache(Cache<Long, Object> guavaCache, CommandLongHasher commandHasher) {
24+
super(commandHasher);
2525
this.cache = guavaCache;
2626
}
2727

28-
public GuavaCSC(Cache<Long, Object> guavaCache, ClientSideCacheable cacheable) {
29-
this(guavaCache, new GuavaHashing(GuavaHashing.DEFAULT_HASH_FUNCTION), cacheable);
28+
public GuavaClientSideCache(Cache<Long, Object> guavaCache, ClientSideCacheable cacheable) {
29+
this(guavaCache, new GuavaCommandHasher(GuavaCommandHasher.DEFAULT_HASH_FUNCTION), cacheable);
3030
}
3131

32-
public GuavaCSC(Cache<Long, Object> cache, CommandLongHashing hashing, ClientSideCacheable cacheable) {
33-
super(hashing, cacheable);
32+
public GuavaClientSideCache(Cache<Long, Object> cache, CommandLongHasher commandHasher, ClientSideCacheable cacheable) {
33+
super(commandHasher, cacheable);
3434
this.cache = cache;
3535
}
3636

@@ -66,7 +66,7 @@ public static class Builder {
6666

6767
// not using a default value to avoid an object creation like 'new GuavaHashing(hashFunction)'
6868
private HashFunction hashFunction = null;
69-
private CommandLongHashing longHashing = null;
69+
private CommandLongHasher commandHasher = null;
7070

7171
private ClientSideCacheable cacheable = DefaultClientSideCacheable.INSTANCE;
7272

@@ -84,12 +84,12 @@ public Builder ttl(int seconds) {
8484

8585
public Builder hashFunction(HashFunction function) {
8686
this.hashFunction = function;
87-
this.longHashing = null;
87+
this.commandHasher = null;
8888
return this;
8989
}
9090

91-
public Builder hashing(CommandLongHashing hashing) {
92-
this.longHashing = hashing;
91+
public Builder commandHasher(CommandLongHasher commandHasher) {
92+
this.commandHasher = commandHasher;
9393
this.hashFunction = null;
9494
return this;
9595
}
@@ -99,16 +99,16 @@ public Builder cacheable(ClientSideCacheable cacheable) {
9999
return this;
100100
}
101101

102-
public GuavaCSC build() {
102+
public GuavaClientSideCache build() {
103103
CacheBuilder cb = CacheBuilder.newBuilder();
104104

105105
cb.maximumSize(maximumSize);
106106

107107
cb.expireAfterWrite(expireTime, expireTimeUnit);
108108

109-
return longHashing != null ? new GuavaCSC(cb.build(), longHashing, cacheable)
110-
: hashFunction != null ? new GuavaCSC(cb.build(), new GuavaHashing(hashFunction), cacheable)
111-
: new GuavaCSC(cb.build(), cacheable);
109+
return hashFunction != null ? new GuavaClientSideCache(cb.build(), new GuavaCommandHasher(hashFunction), cacheable)
110+
: commandHasher != null ? new GuavaClientSideCache(cb.build(), commandHasher, cacheable)
111+
: new GuavaClientSideCache(cb.build(), cacheable);
112112
}
113113
}
114114
}

Diff for: src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHashing.java renamed to src/main/java/redis/clients/jedis/csc/hash/AbstractCommandHasher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import redis.clients.jedis.CommandObject;
55
import redis.clients.jedis.args.Rawable;
66

7-
public abstract class AbstractCommandHashing implements CommandLongHashing {
7+
public abstract class AbstractCommandHasher implements CommandLongHasher {
88

99
@Override
1010
public final long hash(CommandObject command) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package redis.clients.jedis.csc.hash;
2+
3+
import redis.clients.jedis.CommandObject;
4+
5+
/**
6+
* The interface for hashing a command object to support client-side caching.
7+
*/
8+
public interface CommandLongHasher {
9+
10+
/**
11+
* Produce a 64-bit signed hash value from a command object.
12+
* @param command the command object
13+
* @return 64-bit signed hash value
14+
*/
15+
long hash(CommandObject command);
16+
}

Diff for: src/main/java/redis/clients/jedis/csc/hash/CommandLongHashing.java

-16
This file was deleted.

Diff for: src/main/java/redis/clients/jedis/csc/hash/GuavaHashing.java renamed to src/main/java/redis/clients/jedis/csc/hash/GuavaCommandHasher.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import com.google.common.hash.Hasher;
55
import redis.clients.jedis.CommandObject;
66

7-
public class GuavaHashing implements CommandLongHashing {
7+
public class GuavaCommandHasher implements CommandLongHasher {
88

99
public static final HashFunction DEFAULT_HASH_FUNCTION = com.google.common.hash.Hashing.fingerprint2011();
1010

1111
private final HashFunction function;
1212

13-
public GuavaHashing(HashFunction function) {
13+
public GuavaCommandHasher(HashFunction function) {
1414
this.function = function;
1515
}
1616

Diff for: src/main/java/redis/clients/jedis/csc/hash/OpenHftHashing.java renamed to src/main/java/redis/clients/jedis/csc/hash/OpenHftCommandHasher.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import net.openhft.hashing.LongHashFunction;
44

5-
public class OpenHftHashing extends PrimitiveArrayHashing implements CommandLongHashing {
5+
public class OpenHftCommandHasher extends PrimitiveArrayCommandHasher implements CommandLongHasher {
66

77
public static final LongHashFunction DEFAULT_HASH_FUNCTION = LongHashFunction.xx3();
88

99
private final LongHashFunction function;
1010

11-
public OpenHftHashing(LongHashFunction function) {
11+
public OpenHftCommandHasher(LongHashFunction function) {
1212
this.function = function;
1313
}
1414

Diff for: src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayHashing.java renamed to src/main/java/redis/clients/jedis/csc/hash/PrimitiveArrayCommandHasher.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import redis.clients.jedis.Builder;
44
import redis.clients.jedis.args.Rawable;
55

6-
public abstract class PrimitiveArrayHashing extends AbstractCommandHashing {
6+
/**
7+
* It is possible to extend {@link PrimitiveArrayCommandHasher this abstract class} in order to implement
8+
* {@link CommandLongHasher} as {@link PrimitiveArrayCommandHasher#hashLongs(long[])} and
9+
* {@link PrimitiveArrayCommandHasher#hashBytes(byte[])} can be supported by almost all Java hashing libraries.
10+
*/
11+
public abstract class PrimitiveArrayCommandHasher extends AbstractCommandHasher {
712

813
@Override
914
protected final long hashRawable(Rawable raw) {

Diff for: src/main/java/redis/clients/jedis/util/JedisURIHelper.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import redis.clients.jedis.HostAndPort;
55
import redis.clients.jedis.Protocol;
66
import redis.clients.jedis.RedisProtocol;
7-
import redis.clients.jedis.csc.CaffeineCSC;
7+
import redis.clients.jedis.csc.CaffeineClientSideCache;
88
import redis.clients.jedis.csc.ClientSideCache;
9-
import redis.clients.jedis.csc.GuavaCSC;
9+
import redis.clients.jedis.csc.GuavaClientSideCache;
1010

1111
public final class JedisURIHelper {
1212

@@ -137,12 +137,12 @@ public static ClientSideCache getClientSideCache(URI uri) {
137137
}
138138

139139
if (guava) {
140-
GuavaCSC.Builder guavaBuilder = GuavaCSC.builder();
140+
GuavaClientSideCache.Builder guavaBuilder = GuavaClientSideCache.builder();
141141
if (maxSize != null) guavaBuilder.maximumSize(maxSize);
142142
if (ttl != null) guavaBuilder.ttl(ttl);
143143
return guavaBuilder.build();
144144
} else if (caffeine) {
145-
CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder();
145+
CaffeineClientSideCache.Builder caffeineBuilder = CaffeineClientSideCache.builder();
146146
if (maxSize != null) caffeineBuilder.maximumSize(maxSize);
147147
if (ttl != null) caffeineBuilder.ttl(ttl);
148148
return caffeineBuilder.build();

Diff for: src/test/java/redis/clients/jedis/csc/AllowAndDenyListClientSideCacheTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void tearDown() throws Exception {
5454
public void none() {
5555
HashMap<Long, Object> map = new HashMap<>();
5656
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
57-
new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, null, null)),
57+
new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, null)),
5858
singleConnectionPoolConfig.get())) {
5959
control.set("foo", "bar");
6060
assertThat(map, Matchers.aMapWithSize(0));
@@ -67,7 +67,7 @@ public void none() {
6767
public void whiteListCommand() {
6868
HashMap<Long, Object> map = new HashMap<>();
6969
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
70-
new MapCSC(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)),
70+
new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(singleton(Protocol.Command.GET), null, null, null)),
7171
singleConnectionPoolConfig.get())) {
7272
control.set("foo", "bar");
7373
assertThat(map, Matchers.aMapWithSize(0));
@@ -80,7 +80,7 @@ public void whiteListCommand() {
8080
public void blackListCommand() {
8181
HashMap<Long, Object> map = new HashMap<>();
8282
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
83-
new MapCSC(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)),
83+
new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, singleton(Protocol.Command.GET), null, null)),
8484
singleConnectionPoolConfig.get())) {
8585
control.set("foo", "bar");
8686
assertThat(map, Matchers.aMapWithSize(0));
@@ -93,7 +93,7 @@ public void blackListCommand() {
9393
public void whiteListKey() {
9494
HashMap<Long, Object> map = new HashMap<>();
9595
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
96-
new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)),
96+
new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, singleton("foo"), null)),
9797
singleConnectionPoolConfig.get())) {
9898
control.set("foo", "bar");
9999
assertThat(map, Matchers.aMapWithSize(0));
@@ -106,7 +106,7 @@ public void whiteListKey() {
106106
public void blackListKey() {
107107
HashMap<Long, Object> map = new HashMap<>();
108108
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
109-
new MapCSC(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))),
109+
new MapClientSideCache(map, new AllowAndDenyListWithStringKeys(null, null, null, singleton("foo"))),
110110
singleConnectionPoolConfig.get())) {
111111
control.set("foo", "bar");
112112
assertThat(map, Matchers.aMapWithSize(0));

0 commit comments

Comments
 (0)