Skip to content

Commit e96e2e3

Browse files
authored
Support caching null values (#3939)
* caching null results * add more assertion
1 parent 747e391 commit e96e2e3

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

src/main/java/redis/clients/jedis/csc/CacheConnection.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ public <T> T executeCommand(final CommandObject<T> commandObject) {
8181
// CACHE MISS !!
8282
clientSideCache.getStats().miss();
8383
T value = super.executeCommand(commandObject);
84-
if (value != null) {
85-
cacheEntry = new CacheEntry<>(cacheKey, value, this);
86-
clientSideCache.set(cacheKey, cacheEntry);
87-
// this line actually provides a deep copy of cached object instance
88-
value = cacheEntry.getValue();
89-
}
84+
cacheEntry = new CacheEntry<>(cacheKey, value, this);
85+
clientSideCache.set(cacheKey, cacheEntry);
86+
// this line actually provides a deep copy of cached object instance
87+
value = cacheEntry.getValue();
9088
return value;
9189
}
9290

src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.hamcrest.Matchers.hasSize;
66
import static org.junit.Assert.assertEquals;
77
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertNull;
89
import static org.junit.Assert.assertTrue;
910

1011
import java.util.ArrayList;
@@ -510,4 +511,38 @@ public void run() {
510511
}
511512
}
512513

514+
@Test
515+
public void testNullValue() throws InterruptedException {
516+
int MAX_SIZE = 20;
517+
String nonExisting = "non-existing-key";
518+
control.del(nonExisting);
519+
520+
TestCache cache = new TestCache(MAX_SIZE, new HashMap<>(), DefaultCacheable.INSTANCE);
521+
522+
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), cache)) {
523+
CacheStats stats = cache.getStats();
524+
525+
String val = jedis.get(nonExisting);
526+
assertNull(val);
527+
assertEquals(1, cache.getSize());
528+
assertEquals(0, stats.getHitCount());
529+
assertEquals(1, stats.getMissCount());
530+
531+
val = jedis.get(nonExisting);
532+
assertNull(val);
533+
assertEquals(1, cache.getSize());
534+
assertNull(cache.getCacheEntries().iterator().next().getValue());
535+
assertEquals(1, stats.getHitCount());
536+
assertEquals(1, stats.getMissCount());
537+
538+
control.set(nonExisting, "bar");
539+
val = jedis.get(nonExisting);
540+
assertEquals("bar", val);
541+
assertEquals(1, cache.getSize());
542+
assertEquals("bar", cache.getCacheEntries().iterator().next().getValue());
543+
assertEquals(1, stats.getHitCount());
544+
assertEquals(2, stats.getMissCount());
545+
}
546+
}
547+
513548
}

src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ public void simpleWithSimpleMap() {
5656
control.del("foo");
5757
assertThat(map, Matchers.aMapWithSize(1));
5858
assertNull(jedis.get("foo"));
59-
assertThat(map, Matchers.aMapWithSize(0));
60-
assertThat(map, Matchers.aMapWithSize(0));
59+
assertThat(map, Matchers.aMapWithSize(1));
6160
assertNull(jedis.get("foo"));
62-
assertThat(map, Matchers.aMapWithSize(0));
61+
assertThat(map, Matchers.aMapWithSize(1));
6362
}
6463
}
6564

@@ -84,9 +83,9 @@ public void flushAllWithSimpleMap() {
8483
control.flushAll();
8584
assertThat(map, Matchers.aMapWithSize(1));
8685
assertNull(jedis.get("foo"));
87-
assertThat(map, Matchers.aMapWithSize(0));
86+
assertThat(map, Matchers.aMapWithSize(1));
8887
assertNull(jedis.get("foo"));
89-
assertThat(map, Matchers.aMapWithSize(0));
88+
assertThat(map, Matchers.aMapWithSize(1));
9089
}
9190
}
9291

0 commit comments

Comments
 (0)