Interface AsyncCache<K,V>
- Type Parameters:
K- the type of keys | 键类型V- the type of values | 值类型
public interface AsyncCache<K,V>
Async Cache Interface - Non-blocking cache operations based on CompletableFuture
异步缓存接口 - 基于 CompletableFuture 的非阻塞缓存操作
Provides asynchronous cache operations for high-concurrency scenarios.
为高并发场景提供异步缓存操作。
Features | 主要功能:
- Async get/put/invalidate operations - 异步获取/放入/失效操作
- CompletableFuture based API - 基于 CompletableFuture 的 API
- Virtual thread support - 虚拟线程支持
- Sync view conversion - 同步视图转换
Usage Examples | 使用示例:
AsyncCache<String, User> async = cache.async();
// Async get - 异步获取
async.getAsync("user:1001")
.thenAccept(user -> System.out.println(user));
// Async get with loader - 异步获取带加载器
async.getAsync("user:1002", (key, executor) ->
CompletableFuture.supplyAsync(() -> userService.findById(key), executor))
.thenAccept(this::processUser);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial - 空值安全: 部分
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault CompletableFuture<V> computeAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Async compute 异步计算default CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Async compute if present 异步计算(如果存在)getAllAsync(Iterable<? extends K> keys) Async batch get 异步批量获取default CompletableFuture<V> getAndRemoveAsync(K key) Async get and remove 异步获取并删除Async get value by key 异步获取值getAsync(K key, BiFunction<? super K, ? super Executor, ? extends CompletableFuture<V>> loader) Async get with loader 异步获取,不存在时加载invalidateAllAsync(Iterable<? extends K> keys) Async batch invalidate 异步批量失效invalidateAsync(K key) Async invalidate key 异步失效putAllAsync(Map<? extends K, ? extends V> map) Async batch put 异步批量放入default CompletableFuture<Void> putAllAsync(Map<? extends K, ? extends V> map, Duration ttl) Async batch put with TTL 异步批量放入带 TTLAsync put value 异步放入值default CompletableFuture<Void> Async put with TTL 异步放入带 TTLdefault CompletableFuture<Boolean> putIfAbsentAsync(K key, V value) Async put if absent 异步放入(如果不存在)default CompletableFuture<Boolean> putIfAbsentAsync(K key, V value, Duration ttl) Async put if absent with TTL 异步放入带 TTL(如果不存在)sync()Get sync view of this async cache 获取同步视图
-
Method Details
-
getAsync
Async get value by key 异步获取值- Parameters:
key- the key | 键- Returns:
- future containing value or null | 包含值或 null 的 Future
-
getAsync
CompletableFuture<V> getAsync(K key, BiFunction<? super K, ? super Executor, ? extends CompletableFuture<V>> loader) Async get with loader 异步获取,不存在时加载- Parameters:
key- the key | 键loader- the async loader | 异步加载函数- Returns:
- future containing value | 包含值的 Future
-
getAllAsync
-
putAsync
Async put value 异步放入值- Parameters:
key- the key | 键value- the value | 值- Returns:
- future for completion | 完成信号 Future
-
putAllAsync
Async batch put 异步批量放入- Parameters:
map- the key-value pairs | 键值对 Map- Returns:
- future for completion | 完成信号 Future
-
putAsync
Async put with TTL 异步放入带 TTL- Parameters:
key- the key | 键value- the value | 值ttl- time-to-live | 存活时间- Returns:
- future for completion | 完成信号 Future
- Since:
- V2.0.2
-
putAllAsync
-
putIfAbsentAsync
Async put if absent 异步放入(如果不存在)- Parameters:
key- the key | 键value- the value | 值- Returns:
- future containing true if put succeeded | 包含是否成功的 Future
- Since:
- V2.0.2
-
putIfAbsentAsync
Async put if absent with TTL 异步放入带 TTL(如果不存在)- Parameters:
key- the key | 键value- the value | 值ttl- time-to-live | 存活时间- Returns:
- future containing true if put succeeded | 包含是否成功的 Future
- Since:
- V2.0.2
-
computeAsync
default CompletableFuture<V> computeAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Async compute 异步计算- Parameters:
key- the key | 键remappingFunction- the function to compute value | 计算值的函数- Returns:
- future containing the new value | 包含新值的 Future
- Since:
- V2.0.2
-
computeIfPresentAsync
default CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Async compute if present 异步计算(如果存在)- Parameters:
key- the key | 键remappingFunction- the function to compute value | 计算值的函数- Returns:
- future containing the new value or null | 包含新值或 null 的 Future
- Since:
- V2.0.2
-
getAndRemoveAsync
Async get and remove 异步获取并删除- Parameters:
key- the key | 键- Returns:
- future containing the removed value | 包含被删除值的 Future
- Since:
- V2.0.2
-
invalidateAsync
Async invalidate key 异步失效- Parameters:
key- the key | 键- Returns:
- future for completion | 完成信号 Future
-
invalidateAllAsync
Async batch invalidate 异步批量失效- Parameters:
keys- the keys | 键集合- Returns:
- future for completion | 完成信号 Future
-
sync
-