Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support caching null values #3939

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/java/redis/clients/jedis/csc/CacheConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ public <T> T executeCommand(final CommandObject<T> commandObject) {
// CACHE MISS !!
clientSideCache.getStats().miss();
T value = super.executeCommand(commandObject);
if (value != null) {
cacheEntry = new CacheEntry<>(cacheKey, value, this);
clientSideCache.set(cacheKey, cacheEntry);
// this line actually provides a deep copy of cached object instance
value = cacheEntry.getValue();
}
cacheEntry = new CacheEntry<>(cacheKey, value, this);
clientSideCache.set(cacheKey, cacheEntry);
// this line actually provides a deep copy of cached object instance
value = cacheEntry.getValue();
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
Expand Down Expand Up @@ -510,4 +511,38 @@ public void run() {
}
}

@Test
public void testNullValue() throws InterruptedException {
int MAX_SIZE = 20;
String nonExisting = "non-existing-key";
control.del(nonExisting);

TestCache cache = new TestCache(MAX_SIZE, new HashMap<>(), DefaultCacheable.INSTANCE);

try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) {
CacheStats stats = cache.getStats();

String val = jedis.get(nonExisting);
assertNull(val);
assertEquals(1, cache.getSize());
assertEquals(0, stats.getHitCount());
assertEquals(1, stats.getMissCount());

val = jedis.get(nonExisting);
assertNull(val);
assertEquals(1, cache.getSize());
assertNull(cache.getCacheEntries().iterator().next().getValue());
assertEquals(1, stats.getHitCount());
assertEquals(1, stats.getMissCount());

control.set(nonExisting, "bar");
val = jedis.get(nonExisting);
assertEquals("bar", val);
assertEquals(1, cache.getSize());
assertEquals("bar", cache.getCacheEntries().iterator().next().getValue());
assertEquals(1, stats.getHitCount());
assertEquals(2, stats.getMissCount());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public void simpleWithSimpleMap() {
control.del("foo");
assertThat(map, Matchers.aMapWithSize(1));
assertNull(jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(0));
assertThat(map, Matchers.aMapWithSize(0));
assertThat(map, Matchers.aMapWithSize(1));
assertNull(jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(0));
assertThat(map, Matchers.aMapWithSize(1));
}
}

Expand All @@ -84,9 +83,9 @@ public void flushAllWithSimpleMap() {
control.flushAll();
assertThat(map, Matchers.aMapWithSize(1));
assertNull(jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(0));
assertThat(map, Matchers.aMapWithSize(1));
assertNull(jedis.get("foo"));
assertThat(map, Matchers.aMapWithSize(0));
assertThat(map, Matchers.aMapWithSize(1));
}
}

Expand Down
Loading