Actual caching of null values in retrieve(key, valueLoader)

See gh-31637
This commit is contained in:
Juergen Hoeller
2023-11-22 15:56:26 +01:00
parent 441e210533
commit 824bc09d11
3 changed files with 14 additions and 5 deletions

View File

@@ -148,7 +148,14 @@ public class CaffeineCache extends AbstractValueAdaptingCache {
@SuppressWarnings("unchecked")
@Override
public <T> CompletableFuture<T> retrieve(Object key, Supplier<CompletableFuture<T>> valueLoader) {
return (CompletableFuture<T>) getAsyncCache().get(key, (k, e) -> valueLoader.get());
if (isAllowNullValues()) {
return (CompletableFuture<T>) getAsyncCache()
.get(key, (k, e) -> valueLoader.get().thenApply(this::toStoreValue))
.thenApply(this::fromStoreValue);
}
else {
return (CompletableFuture<T>) getAsyncCache().get(key, (k, e) -> valueLoader.get());
}
}
@Override

View File

@@ -181,7 +181,9 @@ class CaffeineCacheManagerTests {
assertThat(cache1.retrieve("key3", () -> CompletableFuture.completedFuture("value3")).join())
.isEqualTo("value3");
cache1.evict("key3");
assertThat(cache1.retrieve("key3")).isNull();
assertThat(cache1.retrieve("key3", () -> CompletableFuture.completedFuture(null)).join()).isNull();
assertThat(cache1.retrieve("key3").join()).isEqualTo(new SimpleValueWrapper(null));
assertThat(cache1.retrieve("key3", () -> CompletableFuture.completedFuture(null)).join()).isNull();
}