Skip to content

Commit

Permalink
descriptive names and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Feb 12, 2024
1 parent 4dd02a4 commit fafca79
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
31 changes: 20 additions & 11 deletions src/main/java/redis/clients/jedis/ClientSideCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,34 @@ protected ClientSideCache() {
this.keyToCommandHashes = new ConcurrentHashMap<>();
}

public abstract void invalidateAll();
protected abstract void invalidateAllCommandHashes();

protected abstract void invalidateAll(Iterable<Long> hashes);
protected abstract void invalidateCommandHashes(Iterable<Long> hashes);

protected abstract void put(long hash, Object value);

protected abstract Object get(long hash);

protected abstract long getCommandHash(CommandObject command);

public final void clear() {
invalidateAllKeysAndCommandHashes();
}

final void invalidate(List list) {
if (list == null) {
invalidateAll();
invalidateAllKeysAndCommandHashes();
return;
}

list.forEach(this::invalidateKeyAndRespectiveCommandHashes);
}

private void invalidateAllKeysAndCommandHashes() {
invalidateAllCommandHashes();
keyToCommandHashes.clear();
}

private void invalidateKeyAndRespectiveCommandHashes(Object key) {
if (!(key instanceof byte[])) {
throw new AssertionError("" + key.getClass().getSimpleName() + " is not supported. Value: " + String.valueOf(key));
Expand All @@ -44,18 +59,14 @@ private void invalidateKeyAndRespectiveCommandHashes(Object key) {

Set<Long> hashes = keyToCommandHashes.get(mapKey);
if (hashes != null) {
invalidateAll(hashes);
invalidateCommandHashes(hashes);
keyToCommandHashes.remove(mapKey);
}
}

protected abstract void put(long hash, Object value);

protected abstract Object get(long hash);

final <T> T getValue(Function<CommandObject<T>, T> loader, CommandObject<T> command, String... keys) {

final long hash = getHash(command);
final long hash = getCommandHash(command);

T value = (T) get(hash);
if (value != null) {
Expand All @@ -80,8 +91,6 @@ final <T> T getValue(Function<CommandObject<T>, T> loader, CommandObject<T> comm
return value;
}

protected abstract long getHash(CommandObject command);

private ByteBuffer makeKeyForKeyToCommandHashes(String key) {
return makeKeyForKeyToCommandHashes(SafeEncoder.encode(key));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private void discoverClusterSlots(Connection jedis) {
Arrays.fill(slots, null);
Arrays.fill(slotNodes, null);
if (clientSideCache != null) {
clientSideCache.invalidateAll();
clientSideCache.clear();
}
Set<String> hostAndPortKeys = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void initMaster(HostAndPort master) {
pool = newPool;
LOG.info("Created connection pool to master at {}.", master);
if (clientSideCache != null) {
clientSideCache.invalidateAll();
clientSideCache.clear();
}

if (existingPool != null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/jedis/util/CaffeineCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public CaffeineCSC(Cache<Long, Object> caffeineCache, LongHashFunction hashFunct
}

@Override
public final void invalidateAll() {
protected final void invalidateAllCommandHashes() {
cache.invalidateAll();
}

@Override
protected void invalidateAll(Iterable<Long> hashes) {
protected void invalidateCommandHashes(Iterable<Long> hashes) {
cache.invalidateAll(hashes);
}

Expand All @@ -43,7 +43,7 @@ protected Object get(long hash) {
}

@Override
protected final long getHash(CommandObject command) {
protected final long getCommandHash(CommandObject command) {
long[] nums = new long[command.getArguments().size() + 1];
int idx = 0;
for (Rawable raw : command.getArguments()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/redis/clients/jedis/util/GuavaCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public GuavaCSC(Cache<Long, Object> guavaCache, HashFunction hashFunction) {
}

@Override
public final void invalidateAll() {
protected final void invalidateAllCommandHashes() {
cache.invalidateAll();
}

@Override
protected void invalidateAll(Iterable<Long> hashes) {
protected void invalidateCommandHashes(Iterable<Long> hashes) {
cache.invalidateAll(hashes);
}

Expand All @@ -43,7 +43,7 @@ protected Object get(long hash) {
}

@Override
protected final long getHash(CommandObject command) {
protected final long getCommandHash(CommandObject command) {
Hasher hasher = function.newHasher();
command.getArguments().forEach(raw -> hasher.putBytes(raw.getRaw()));
hasher.putInt(command.getBuilder().hashCode());
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/redis/clients/jedis/util/MapCSC.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public MapCSC(Map<Long, Object> map) {
}

@Override
public final void invalidateAll() {
protected final void invalidateAllCommandHashes() {
cache.clear();
}

@Override
protected void invalidateAll(Iterable<Long> hashes) {
protected void invalidateCommandHashes(Iterable<Long> hashes) {
hashes.forEach(hash -> cache.remove(hash));
}

Expand All @@ -40,7 +40,7 @@ protected Object get(long hash) {
}

@Override
protected final long getHash(CommandObject command) {
protected final long getCommandHash(CommandObject command) {
long result = 1;
for (Rawable raw : command.getArguments()) {
result = 31 * result + Arrays.hashCode(raw.getRaw());
Expand Down

0 comments on commit fafca79

Please sign in to comment.