1
- package redis .clients .jedis ;
1
+ package redis .clients .jedis . csc ;
2
2
3
3
import java .nio .ByteBuffer ;
4
4
import java .util .HashSet ;
5
5
import java .util .List ;
6
6
import java .util .Map ;
7
7
import java .util .Set ;
8
8
import java .util .concurrent .ConcurrentHashMap ;
9
-
10
9
import java .util .function .Function ;
10
+ import redis .clients .jedis .CommandObject ;
11
11
import redis .clients .jedis .csc .hash .CommandLongHashing ;
12
12
import redis .clients .jedis .util .SafeEncoder ;
13
13
14
14
/**
15
15
* 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.util.CaffeineCSC CaffeineCSC} or
17
- * {@link redis.clients.jedis.util.GuavaCSC GuavaCSC} or a custom implementation of their own.
16
+ * object; e.g. {@link redis.clients.jedis.csc. util.CaffeineCSC CaffeineCSC} or
17
+ * {@link redis.clients.jedis.csc. util.GuavaCSC GuavaCSC} or a custom implementation of their own.
18
18
*/
19
19
public abstract class ClientSideCache {
20
20
21
21
protected static final int DEFAULT_MAXIMUM_SIZE = 10_000 ;
22
22
protected static final int DEFAULT_EXPIRE_SECONDS = 100 ;
23
23
24
+ private final Map <ByteBuffer , Set <Long >> keyToCommandHashes = new ConcurrentHashMap <>();
24
25
private final CommandLongHashing commandHashing ;
25
- private final Map < ByteBuffer , Set < Long >> keyToCommandHashes ;
26
+ private final ClientSideCacheable cacheable ;
26
27
27
28
protected ClientSideCache (CommandLongHashing commandHashing ) {
29
+ this (commandHashing , DefaultClientSideCacheable .INSTANCE );
30
+ }
31
+
32
+ protected ClientSideCache (CommandLongHashing commandHashing , ClientSideCacheable cacheable ) {
28
33
this .commandHashing = commandHashing ;
29
- this .keyToCommandHashes = new ConcurrentHashMap <>() ;
34
+ this .cacheable = cacheable ;
30
35
}
31
36
32
- protected abstract void invalidateAllCommandHashes ();
37
+ protected abstract void invalidateAllHashes ();
33
38
34
- protected abstract void invalidateCommandHashes (Iterable <Long > hashes );
39
+ protected abstract void invalidateHashes (Iterable <Long > hashes );
35
40
36
- protected abstract void put (long hash , Object value );
41
+ protected abstract void putValue (long hash , Object value );
37
42
38
- protected abstract Object get (long hash );
43
+ protected abstract Object getValue (long hash );
39
44
40
45
public final void clear () {
41
46
invalidateAllKeysAndCommandHashes ();
42
47
}
43
48
44
- final void invalidate (List list ) {
49
+ public final void invalidate (List list ) {
45
50
if (list == null ) {
46
51
invalidateAllKeysAndCommandHashes ();
47
52
return ;
@@ -51,7 +56,7 @@ final void invalidate(List list) {
51
56
}
52
57
53
58
private void invalidateAllKeysAndCommandHashes () {
54
- invalidateAllCommandHashes ();
59
+ invalidateAllHashes ();
55
60
keyToCommandHashes .clear ();
56
61
}
57
62
@@ -64,23 +69,27 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) {
64
69
65
70
Set <Long > hashes = keyToCommandHashes .get (mapKey );
66
71
if (hashes != null ) {
67
- invalidateCommandHashes (hashes );
72
+ invalidateHashes (hashes );
68
73
keyToCommandHashes .remove (mapKey );
69
74
}
70
75
}
71
76
72
- final <T > T getValue (Function <CommandObject <T >, T > loader , CommandObject <T > command , Object ... keys ) {
77
+ public final <T > T get (Function <CommandObject <T >, T > loader , CommandObject <T > command , Object ... keys ) {
78
+
79
+ if (!cacheable .isCacheable (command .getArguments ().getCommand (), keys )) {
80
+ return loader .apply (command );
81
+ }
73
82
74
83
final long hash = commandHashing .hash (command );
75
84
76
- T value = (T ) get (hash );
85
+ T value = (T ) getValue (hash );
77
86
if (value != null ) {
78
87
return value ;
79
88
}
80
89
81
90
value = loader .apply (command );
82
91
if (value != null ) {
83
- put (hash , value );
92
+ putValue (hash , value );
84
93
for (Object key : keys ) {
85
94
ByteBuffer mapKey = makeKeyForKeyToCommandHashes (key );
86
95
if (keyToCommandHashes .containsKey (mapKey )) {
0 commit comments