Skip to content

Commit

Permalink
Actual caching of null values in retrieve(key, valueLoader)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Nov 22, 2023
1 parent 441e210 commit 824bc09
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ public CompletableFuture<?> retrieve(Object key) {
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ void asyncMode() {
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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ default CompletableFuture<?> retrieve(Object key) {
* <p>If possible, implementations should ensure that the loading operation
* is synchronized so that the specified {@code valueLoader} is only called
* once in case of concurrent access on the same key.
* <p>Null values are generally not supported by this method. The provided
* {@link CompletableFuture} handle produces a value or raises an exception.
* If the {@code valueLoader} raises an exception, it will be propagated
* to the {@code CompletableFuture} handle returned from here.
* <p>Null values always indicate a user-level {@code null} value with this
* method. The provided {@link CompletableFuture} handle produces a value
* or raises an exception. If the {@code valueLoader} raises an exception,
* it will be propagated to the returned {@code CompletableFuture} handle.
* @param key the key whose associated value is to be returned
* @return the value to which this cache maps the specified key, contained
* within a {@link CompletableFuture} which will never be {@code null}.
Expand Down

0 comments on commit 824bc09

Please sign in to comment.