Class AsyncReadThroughCache<K,V>
- Type Parameters:
K- key typeV- value type
- All Implemented Interfaces:
AutoCloseable
When a cache miss occurs, the AsyncCacheLoader is called to fetch the value
asynchronously. All returned futures are completed on a bounded executor.
Multiple concurrent requests for the same missing key are coalesced (single-flight):
only one loader call is made, and all callers receive the same CompletableFuture result.
This eliminates redundant upstream calls under heavy concurrent access patterns.
Uses a bounded executor for async operations with caller-runs backpressure.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAsyncReadThroughCache(NearCache<K, V> nearCache, AsyncCacheLoader<K, V> loader) Create a new async read-through cache. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Shuts down the async executor.getAllAsync(Collection<K> keys) Asynchronously get multiple values, loading any missing ones efficiently.Asynchronously get a value from the cache, loading it if necessary.getStats()Get underlying near cache statistics.voidRefresh all entries by clearing the near cache.refreshAsync(K key) Asynchronously refresh a specific key by forcing a reload from the source.
-
Constructor Details
-
AsyncReadThroughCache
Create a new async read-through cache.- Parameters:
nearCache- the underlying near cache (must not be null)loader- the async cache loader for cache misses (must not be null)- Throws:
NullPointerException- if any parameter is null
-
-
Method Details
-
getAsync
Asynchronously get a value from the cache, loading it if necessary.If multiple callers request the same missing key simultaneously, only one loader call is made; all callers receive the same future (single-flight).
- Parameters:
key- the key to retrieve (must not be null)- Returns:
- a future that completes with the cached or loaded value
- Throws:
NullPointerException- if key is null
-
getAllAsync
Asynchronously get multiple values, loading any missing ones efficiently.Hits are resolved immediately from the near cache. Misses are loaded using
AsyncCacheLoader.loadAllAsync(Collection)and cached on completion. In-flight loads for individual keys are still coalesced.- Parameters:
keys- the keys to retrieve (must not be null)- Returns:
- a future that completes with a map of keys to values (may be partial if some loads fail)
- Throws:
NullPointerException- if keys is null
-
refreshAsync
Asynchronously refresh a specific key by forcing a reload from the source.Invalidates the near cache entry first, then triggers a fresh load. Does not participate in single-flight coalescing — a fresh load is always issued.
- Parameters:
key- the key to refresh (must not be null)- Returns:
- a future that completes with the refreshed value
- Throws:
NullPointerException- if key is null
-
refreshAll
public void refreshAll()Refresh all entries by clearing the near cache. -
getStats
Get underlying near cache statistics. -
close
public void close()Shuts down the async executor. After calling this method, new async loads will be rejected.- Specified by:
closein interfaceAutoCloseable
-