Interface LoadingCache<K,V>
- Type Parameters:
K- the type of keys | 键类型V- the type of values | 值类型Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial (null values not allowed) - 空值安全: 部分(不允许 null 值)
- All Superinterfaces:
Cache<K,V>
Loading Cache Interface - Cache with automatic value loading
加载缓存接口 - 具有自动值加载功能的缓存
Extends Cache with automatic loading capabilities. When a key is
not present, the configured loader is automatically called to load the value.
扩展 Cache,具有自动加载功能。当键不存在时,自动调用配置的加载器加载值。
Features | 主要功能:
- Automatic loading on get - 获取时自动加载
- Batch loading support - 批量加载支持
- Refresh support - 刷新支持
- Async loading - 异步加载
Usage Examples | 使用示例:
// Create loading cache
LoadingCache<String, User> cache = LoadingCache.create(
"users",
key -> userService.findById(key),
CacheConfig.<String, User>builder()
.maximumSize(10000)
.expireAfterWrite(Duration.ofMinutes(30))
.build()
);
// Get value - automatically loads if not present
User user = cache.get("user:1001");
// Refresh value
cache.refresh("user:1001");
- Since:
- JDK 25, opencode-base-cache V2.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionGet async view of this loading cache 获取加载缓存的异步视图static <K,V> LoadingCache <K, V> Create a loading cache with default configuration 使用默认配置创建加载缓存static <K,V> LoadingCache <K, V> create(String name, Function<? super K, ? extends V> loader, CacheConfig<K, V> config) Create a loading cache with the given loader 使用给定的加载器创建加载缓存static <K,V> LoadingCache <K, V> createWithLoader(String name, CacheLoader<K, V> loader, CacheConfig<K, V> config) Create a loading cache with CacheLoader 使用 CacheLoader 创建加载缓存Get value, automatically loading if not present 获取值,不存在时自动加载Get all values for given keys, loading missing entries 获取所有键的值,加载缺失的条目loader()Get the loader function 获取加载器函数Refresh value for given key 刷新给定键的值default intrefreshAll(Predicate<K> predicate) Refresh all entries matching the predicate 刷新所有匹配条件的条目Methods inherited from interface Cache
asMap, async, cleanUp, clear, compute, computeIfAbsent, computeIfMatch, computeIfPresent, containsKey, containsValue, entries, entryIterator, entryParallelStream, entryStream, estimatedSize, forEach, forEachKey, forEachValue, get, getAll, getAndRemove, getByPattern, getIfPresent, getOptional, getOrDefault, getOrNull, invalidate, invalidateAll, invalidateAll, invalidateByPattern, invalidateByValue, invalidateIf, isEmpty, keyIterator, keyParallelStream, keys, keyStream, merge, metrics, name, put, putAll, putAllIfAbsent, putAllWithTtl, putIfAbsent, putIfAbsentWithTtl, putIfPresent, putWithTtl, removeIfEquals, removeIfPresent, replace, replace, replaceAll, replaceIf, resetStats, size, stats, updateAll, updateTtl, updateTtl, updateTtlAll, valueParallelStream, values, valueStream
-
Method Details
-
get
Get value, automatically loading if not present 获取值,不存在时自动加载Unlike
Cache.get(Object), this method never returns null for non-null keys if the loader can provide a value.与
Cache.get(Object)不同,如果加载器能提供值,此方法对非 null 键永不返回 null。- Specified by:
getin interfaceCache<K,V> - Parameters:
key- the key | 键- Returns:
- the value | 值
- Throws:
RuntimeException- if loading fails | 加载失败时抛出异常
-
getAll
-
refresh
Refresh value for given key 刷新给定键的值Asynchronously reloads the value for the key. If the key is not present, it will be loaded.
异步重新加载键的值。如果键不存在,将被加载。
- Parameters:
key- the key to refresh | 要刷新的键- Returns:
- future that completes when refresh is done | 刷新完成时完成的 Future
-
refreshAll
-
loader
-
asyncLoading
-
create
static <K,V> LoadingCache<K,V> create(String name, Function<? super K, ? extends V> loader, CacheConfig<K, V> config) Create a loading cache with the given loader 使用给定的加载器创建加载缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称loader- the loader function | 加载函数config- cache configuration | 缓存配置- Returns:
- loading cache | 加载缓存
-
create
Create a loading cache with default configuration 使用默认配置创建加载缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称loader- the loader function | 加载函数- Returns:
- loading cache | 加载缓存
-
createWithLoader
static <K,V> LoadingCache<K,V> createWithLoader(String name, CacheLoader<K, V> loader, CacheConfig<K, V> config) Create a loading cache with CacheLoader 使用 CacheLoader 创建加载缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称loader- the cache loader | 缓存加载器config- cache configuration | 缓存配置- Returns:
- loading cache | 加载缓存
-