From d3acff22529e6469bb371cc909feaf3752df59e2 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:02:39 +0600 Subject: [PATCH] Clean-up keyHashes map --- .../redis/clients/jedis/ClientSideCache.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/redis/clients/jedis/ClientSideCache.java b/src/main/java/redis/clients/jedis/ClientSideCache.java index 2b15fb75df..1371a2d376 100644 --- a/src/main/java/redis/clients/jedis/ClientSideCache.java +++ b/src/main/java/redis/clients/jedis/ClientSideCache.java @@ -1,23 +1,19 @@ package redis.clients.jedis; import java.nio.ByteBuffer; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; import redis.clients.jedis.util.SafeEncoder; public abstract class ClientSideCache { - private final Map> keyHashes; // TODO: clean-up - private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); - private final Lock readLock = rwl.readLock(); - private final Lock writeLock = rwl.writeLock(); + private final Map> keyHashes; + private final ReentrantLock writeLock = new ReentrantLock(); protected ClientSideCache() { this.keyHashes = new ConcurrentHashMap<>(); @@ -33,24 +29,25 @@ final void invalidate(List list) { return; } - Set hashes = new HashSet<>(); - list.forEach(key -> hashes.addAll(getHashes(key))); - invalidateAll(hashes); - // TODO: clean-up keyHashes + list.forEach(this::invalidate0); } - private Set getHashes(Object key) { + private void invalidate0(Object key) { if (!(key instanceof byte[])) { throw new AssertionError("" + key.getClass().getSimpleName() + " is not supported. Value: " + String.valueOf(key)); } final ByteBuffer mapKey = makeKey((byte[]) key); - readLock.lock(); - try { - Set hashes = keyHashes.get(mapKey); - return hashes != null ? hashes : Collections.emptySet(); - } finally { - readLock.unlock(); + + Set hashes = keyHashes.get(mapKey); + if (hashes != null) { + writeLock.lock(); + try { + invalidateAll(hashes); + keyHashes.remove(mapKey); + } finally { + writeLock.unlock(); + } } }