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

Skip to content

Commit d3acff2

Browse files
committed
Clean-up keyHashes map
1 parent d7720a1 commit d3acff2

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package redis.clients.jedis;
22

33
import java.nio.ByteBuffer;
4-
import java.util.Collections;
54
import java.util.HashSet;
65
import java.util.List;
76
import java.util.Map;
87
import java.util.Set;
98
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;
1210
import java.util.function.Function;
1311
import redis.clients.jedis.util.SafeEncoder;
1412

1513
public abstract class ClientSideCache {
1614

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();
2117

2218
protected ClientSideCache() {
2319
this.keyHashes = new ConcurrentHashMap<>();
@@ -33,24 +29,25 @@ final void invalidate(List list) {
3329
return;
3430
}
3531

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);
4033
}
4134

42-
private Set<Long> getHashes(Object key) {
35+
private void invalidate0(Object key) {
4336
if (!(key instanceof byte[])) {
4437
throw new AssertionError("" + key.getClass().getSimpleName() + " is not supported. Value: " + String.valueOf(key));
4538
}
4639

4740
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+
}
5451
}
5552
}
5653

0 commit comments

Comments
 (0)