From 1b767fb744d5a058dbebc1b7b0e7bc16363ee3d4 Mon Sep 17 00:00:00 2001 From: Chris Povirk Date: Tue, 3 Dec 2024 14:12:04 -0500 Subject: [PATCH] Allow async loads to return `null`. This makes them match what `CacheLoader` already allows for synchronous loading and reloading via `@Nullable V load(K key)` and `@Nullable V reload(K key, V oldValue)`. --- .../com/github/benmanes/caffeine/cache/AsyncCacheLoader.java | 5 +++-- .../java/com/github/benmanes/caffeine/cache/CacheLoader.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java index 745922807a..8abff8d7e1 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java @@ -25,6 +25,7 @@ import java.util.function.Function; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; /** * Computes or retrieves values asynchronously based on a key, for use in populating a @@ -62,7 +63,7 @@ public interface AsyncCacheLoader { * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ - CompletableFuture asyncLoad(K key, Executor executor) throws Exception; + CompletableFuture asyncLoad(K key, Executor executor) throws Exception; /** * Asynchronously computes or retrieves the values corresponding to {@code keys}. This method is @@ -114,7 +115,7 @@ public interface AsyncCacheLoader { * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ - default CompletableFuture asyncReload( + default CompletableFuture asyncReload( K key, V oldValue, Executor executor) throws Exception { return asyncLoad(key, executor); } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java index 217c81a868..3d8b9ca642 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java @@ -101,7 +101,7 @@ public interface CacheLoader extends AsyncCacheLoader { * @return the future value associated with {@code key} */ @Override - default CompletableFuture asyncLoad(K key, Executor executor) throws Exception { + default CompletableFuture asyncLoad(K key, Executor executor) throws Exception { requireNonNull(key); requireNonNull(executor); return CompletableFuture.supplyAsync(() -> { @@ -193,7 +193,7 @@ default CompletableFuture asyncLoad(K key, Executor executor) throw * {@code null} if the mapping is to be removed */ @Override - default CompletableFuture asyncReload( + default CompletableFuture asyncReload( K key, V oldValue, Executor executor) throws Exception { requireNonNull(key); requireNonNull(executor);