接口 Cache<K,V>
- 所有超级接口:
AutoCloseable
,Closeable
- 所有已知子接口:
ProxyCache<K,
V>
- 所有已知实现类:
AbstractCache
,AbstractEmbeddedCache
,AbstractExternalCache
,CaffeineCache
,LinkedHashMapCache
,LoadingCache
,MockRemoteCache
,MultiLevelCache
,RefreshCache
,SimpleProxyCache
- 作者:
- huangli
-
字段概要
字段 -
方法概要
修饰符和类型方法说明default void
close()
Clean resources created by this cache.default V
computeIfAbsent
(K key, Function<K, V> loader) If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.computeIfAbsent
(K key, Function<K, V> loader, boolean cacheNullWhenLoaderReturnNull) If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.computeIfAbsent
(K key, Function<K, V> loader, boolean cacheNullWhenLoaderReturnNull, long expireAfterWrite, TimeUnit timeUnit) If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.config()
Get the config of this cache.default V
Gets an entry from the cache.Gets an entry from the cache.Gets a collection of entries from the Cache.Gets a collection of entries from the Cache, returning them as Map of the values associated with the set of keys requested.default void
Associates the specified value with the specified key in the cache.default void
Associates the specified value with the specified key in the cache.default CacheResult
Associates the specified value with the specified key in the cache.Associates the specified value with the specified key in the cache.default CacheResult
Copies all of the entries from the specified map to the cache.Copies all of the entries from the specified map to the cache.PUT_IF_ABSENT
(K key, V value, long expireAfterWrite, TimeUnit timeUnit) If the specified key is not already associated with a value, associate it with the given value.default void
Copies all of the entries from the specified map to the cache.default void
Copies all of the entries from the specified map to the cache.default boolean
putIfAbsent
(K key, V value) Atomically associates the specified key with the given value if it is not already associated with a value.default boolean
Removes the mapping for a key from this cache if it is present.Removes the mapping for a key from this cache if it is present.REMOVE_ALL
(Set<? extends K> keys) Removes entries for the specified keys.default void
Removes entries for the specified keys.default AutoReleaseLock
Use this cache attempt to acquire a exclusive lock specified by the key, this method will not block.default boolean
tryLockAndRun
(K key, long expire, TimeUnit timeUnit, Runnable action) Use this cache to try run an action exclusively.<T> T
Provides a standard way to access the underlying concrete cache entry implementation in order to provide access to further, proprietary features.
-
字段详细资料
-
logger
static final org.slf4j.Logger logger
-
-
方法详细资料
-
get
Gets an entry from the cache.If the cache's builder has specified a
CacheLoader
and there is no association in the cache , it will attempt to load the entry.If error occurs during cache access, the method return null instead of throwing an exception.
- 参数:
key
- the key whose associated value is to be returned- 返回:
- the associated value. null may indicates:
- the entry does not exist or expired
- the entry value is null
- error occurs during cache access(no exception throws)
- 抛出:
CacheInvokeException
- only if loader throws an exception- 另请参阅:
-
getAll
Gets a collection of entries from the Cache, returning them as Map of the values associated with the set of keys requested.If the cache's builder has specified a
CacheLoader
and there is no association in the cache , it will attempt to load the entry.If error occurs during cache access, the method will not throw an exception.
- 参数:
keys
- The keys whose associated values are to be returned.- 返回:
- A map of entries that were found for the given keys. Keys not found in the cache are not in the returned map.
- 抛出:
CacheInvokeException
- only if loader throws an exception- 另请参阅:
-
put
Associates the specified value with the specified key in the cache.If error occurs during cache access, the method will not throw an exception.
if the implementation supports asynchronous operation, the cache operation of this method is asynchronous.
- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- 另请参阅:
-
putAll
Copies all of the entries from the specified map to the cache.If error occurs during cache access, the method will not throw an exception.
if the implementation supports asynchronous operation, the cache operation of this method is asynchronous.
- 参数:
map
- mappings to be stored in this cache.- 另请参阅:
-
putIfAbsent
Atomically associates the specified key with the given value if it is not already associated with a value.If error occurs during cache access, the method will not throw an exception.
MultiLevelCache
does not support this method.- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- 返回:
- true if a value was set, false if the KV association does not exists in the cache, or error occurs during cache access.
- 另请参阅:
-
remove
Removes the mapping for a key from this cache if it is present.If error occurs during cache access, the method will not throw an exception.
- 参数:
key
- key whose mapping is to be removed from the cache- 返回:
- true if the key is removed successfully, false if the KV association does not exists in the cache, or error occurs during cache access.
- 另请参阅:
-
removeAll
Removes entries for the specified keys.If error occurs during cache access, the method will not throw an exception.
if the implementation supports asynchronous operation, the cache operation of this method is asynchronous.
- 参数:
keys
- the keys to remove- 另请参阅:
-
unwrap
Provides a standard way to access the underlying concrete cache entry implementation in order to provide access to further, proprietary features.If the implementation does not support the specified class, the
IllegalArgumentException
is thrown.- 参数:
clazz
- the proprietary class or interface of the underlying concrete cache. It is this type that is returned.- 返回:
- an instance of the underlying concrete cache
- 抛出:
IllegalArgumentException
- if the caching provider doesn't support the specified class.
-
close
default void close()Clean resources created by this cache.- 指定者:
close
在接口中AutoCloseable
- 指定者:
close
在接口中Closeable
-
config
CacheConfig<K,V> config()Get the config of this cache.- 返回:
- the cache config
-
tryLock
Use this cache attempt to acquire a exclusive lock specified by the key, this method will not block. examples:try(AutoReleaseLock lock = cache.tryLock("MyKey",100, TimeUnit.SECONDS)){ if(lock != null){ // do something } }
MultiLevelCache
will use the last level cache to support this operation.- 参数:
key
- lockKeyexpire
- lock expire timetimeUnit
- lock expire time unit- 返回:
- an AutoReleaseLock(implements java.lang.AutoCloseable) instance if success. or null if the attempt fails, which indicates there is an another thread/process/server has the lock, or error occurs during cache access.
- 另请参阅:
-
tryLockAndRun
Use this cache to try run an action exclusively.
examples:MultiLevelCache
will use the last level cache to support this operation.cache.tryLock("MyKey",100, TimeUnit.SECONDS),() -> { //do something });
- 参数:
key
- lockKeyexpire
- lock expire timetimeUnit
- lock expire time unitaction
- the action need to execute- 返回:
- true if successfully get the lock and the action is executed
-
GET
Gets an entry from the cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage()/getValue() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
key
- the key- 返回:
- the result
-
GET_ALL
Gets a collection of entries from the Cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage()/getValue() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
keys
- the key collection- 返回:
- the result
-
computeIfAbsent
If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.- 参数:
key
- the keyloader
- the value loader- 返回:
- the value
- 另请参阅:
-
computeIfAbsent
If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.- 参数:
key
- the keyloader
- the value loadercacheNullWhenLoaderReturnNull
- true if null value returned by loader should put into cache use the key- 返回:
- the value
-
computeIfAbsent
V computeIfAbsent(K key, Function<K, V> loader, boolean cacheNullWhenLoaderReturnNull, long expireAfterWrite, TimeUnit timeUnit) If there is a value associated with the key, return the value; otherwise use the loader load the value and return, and then update the cache.- 参数:
key
- the keyloader
- the value loadercacheNullWhenLoaderReturnNull
- true if null value returned by loader should put into cache use the keyexpireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 返回:
- the value
-
put
Associates the specified value with the specified key in the cache.If error occurs during cache access, the method will not throw an exception.
- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyexpireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 另请参阅:
-
PUT
Associates the specified value with the specified key in the cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- 返回:
- the result
-
PUT
Associates the specified value with the specified key in the cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyexpireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 返回:
- the result
-
putAll
Copies all of the entries from the specified map to the cache.If error occurs during cache access, the method will not throw an exception.
- 参数:
map
- mappings to be stored in this cache.expireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 另请参阅:
-
PUT_ALL
Copies all of the entries from the specified map to the cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
map
- mappings to be stored in this cache.- 返回:
- the result
-
PUT_ALL
Copies all of the entries from the specified map to the cache.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
map
- mappings to be stored in this cache.expireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 返回:
- the result
-
REMOVE
Removes the mapping for a key from this cache if it is present.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
key
- key whose mapping is to be removed from the cache- 返回:
- the result
-
REMOVE_ALL
Removes entries for the specified keys.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
keys
- the keys to remove- 返回:
- the result
-
PUT_IF_ABSENT
If the specified key is not already associated with a value, associate it with the given value.if the implementation supports asynchronous operation, the cache access may not completed after this method return. The invoke of getResultCode()/isSuccess()/getMessage() on the result will block until cache operation is completed. Call future() method on the result will get a CompletionStage instance for asynchronous programming.
- 参数:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyexpireAfterWrite
- the TTL(time to live) of the KV associationtimeUnit
- the time unit of expireAfterWrite- 返回:
- SUCCESS if the specified key is not already associated with a value, or EXISTS if the specified key is already associated with a value, or FAIL if error occurs.
-