1
1
package redis .clients .jedis ;
2
2
3
3
import java .nio .ByteBuffer ;
4
- import java .util .Collections ;
5
4
import java .util .HashSet ;
6
5
import java .util .List ;
7
6
import java .util .Map ;
8
7
import java .util .Set ;
9
8
import java .util .concurrent .ConcurrentHashMap ;
10
- import java .util .concurrent .locks .Lock ;
11
- import java .util .concurrent .locks .ReentrantReadWriteLock ;
9
+ import java .util .concurrent .locks .ReentrantLock ;
12
10
import java .util .function .Function ;
13
11
import redis .clients .jedis .util .SafeEncoder ;
14
12
15
13
public abstract class ClientSideCache {
16
14
17
- private final Map <ByteBuffer , Set <Long >> keyHashes ; // TODO: clean-up
18
- private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
19
- private final Lock readLock = rwl .readLock ();
20
- private final Lock writeLock = rwl .writeLock ();
15
+ private final Map <ByteBuffer , Set <Long >> keyHashes ;
16
+ private final ReentrantLock writeLock = new ReentrantLock ();
21
17
22
18
protected ClientSideCache () {
23
19
this .keyHashes = new ConcurrentHashMap <>();
@@ -33,24 +29,25 @@ final void invalidate(List list) {
33
29
return ;
34
30
}
35
31
36
- Set <Long > hashes = new HashSet <>();
37
- list .forEach (key -> hashes .addAll (getHashes (key )));
38
- invalidateAll (hashes );
39
- // TODO: clean-up keyHashes
32
+ list .forEach (this ::invalidate0 );
40
33
}
41
34
42
- private Set < Long > getHashes (Object key ) {
35
+ private void invalidate0 (Object key ) {
43
36
if (!(key instanceof byte [])) {
44
37
throw new AssertionError ("" + key .getClass ().getSimpleName () + " is not supported. Value: " + String .valueOf (key ));
45
38
}
46
39
47
40
final ByteBuffer mapKey = makeKey ((byte []) key );
48
- readLock .lock ();
49
- try {
50
- Set <Long > hashes = keyHashes .get (mapKey );
51
- return hashes != null ? hashes : Collections .emptySet ();
52
- } finally {
53
- readLock .unlock ();
41
+
42
+ Set <Long > hashes = keyHashes .get (mapKey );
43
+ if (hashes != null ) {
44
+ writeLock .lock ();
45
+ try {
46
+ invalidateAll (hashes );
47
+ keyHashes .remove (mapKey );
48
+ } finally {
49
+ writeLock .unlock ();
50
+ }
54
51
}
55
52
}
56
53
0 commit comments