public interface Cache<K,V> extends Closeable
| 限定符和类型 | 字段和说明 |
|---|---|
static org.slf4j.Logger |
logger |
| 限定符和类型 | 方法和说明 |
|---|---|
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.
|
V |
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.
|
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.
|
CacheConfig<K,V> |
config()
Get the config of this cache.
|
MultiGetResult<K,V> |
GET_ALL(Set<? extends K> keys)
Gets a collection of entries from the Cache.
|
default V |
get(K key)
Gets an entry from the cache.
|
CacheGetResult<V> |
GET(K key)
Gets an entry from the cache.
|
default Map<K,V> |
getAll(Set<? extends K> keys)
Gets a collection of entries from the Cache, returning them as Map of the values associated with
the set of keys requested.
|
default CacheResult |
PUT_ALL(Map<? extends K,? extends V> map)
Copies all of the entries from the specified map to the cache.
|
CacheResult |
PUT_ALL(Map<? extends K,? extends V> map,
long expireAfterWrite,
TimeUnit timeUnit)
Copies all of the entries from the specified map to the cache.
|
CacheResult |
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 |
put(K key,
V value)
Associates the specified value with the specified key in the cache.
|
default CacheResult |
PUT(K key,
V value)
Associates the specified value with the specified key in the cache.
|
default void |
put(K key,
V value,
long expireAfterWrite,
TimeUnit timeUnit)
Associates the specified value with the specified key in the cache.
|
CacheResult |
PUT(K key,
V value,
long expireAfterWrite,
TimeUnit timeUnit)
Associates the specified value with the specified key in the cache.
|
default void |
putAll(Map<? extends K,? extends V> map)
Copies all of the entries from the specified map to the cache.
|
default void |
putAll(Map<? extends K,? extends V> map,
long expireAfterWrite,
TimeUnit timeUnit)
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.
|
CacheResult |
REMOVE_ALL(Set<? extends K> keys)
Removes entries for the specified keys.
|
default boolean |
remove(K key)
Removes the mapping for a key from this cache if it is present.
|
CacheResult |
REMOVE(K key)
Removes the mapping for a key from this cache if it is present.
|
default void |
removeAll(Set<? extends K> keys)
Removes entries for the specified keys.
|
default AutoReleaseLock |
tryLock(K key,
long expire,
TimeUnit timeUnit)
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 |
unwrap(Class<T> clazz)
Provides a standard way to access the underlying concrete cache entry
implementation in order to provide access to further, proprietary features.
|
default V get(K key) throws CacheInvokeException
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 returnedCacheInvokeException - only if loader throws an exceptionCacheLoader,
GET(Object)default Map<K,V> getAll(Set<? extends K> keys) throws CacheInvokeException
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.CacheInvokeException - only if loader throws an exceptionCacheLoader,
GET_ALL(Set)default void put(K key, V value)
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 keyPUT(Object, Object)default void putAll(Map<? extends K,? extends V> map)
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.PUT_ALL(Map)default boolean putIfAbsent(K key, V 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 keyPUT_IF_ABSENT(Object, Object, long, TimeUnit)default boolean remove(K key)
If error occurs during cache access, the method will not throw an exception.
key - key whose mapping is to be removed from the cacheREMOVE(Object)default void removeAll(Set<? extends K> 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 removeREMOVE_ALL(Set)<T> T unwrap(Class<T> clazz)
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.IllegalArgumentException - if the caching provider doesn't support
the specified class.default void close()
close 在接口中 AutoCloseableclose 在接口中 CloseableCacheConfig<K,V> config()
default AutoReleaseLock tryLock(K key, long expire, TimeUnit timeUnit)
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 unittryLockAndRun(Object, long, TimeUnit, Runnable)default boolean tryLockAndRun(K key, long expire, TimeUnit timeUnit, Runnable action)
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 executeCacheGetResult<V> GET(K key)
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 keyMultiGetResult<K,V> GET_ALL(Set<? extends K> keys)
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 collectiondefault V computeIfAbsent(K key, Function<K,V> loader)
key - the keyloader - the value loaderCacheConfig.isCacheNullValue()V computeIfAbsent(K key, Function<K,V> loader, boolean cacheNullWhenLoaderReturnNull)
key - the keyloader - the value loadercacheNullWhenLoaderReturnNull - true if null value returned by loader should put into cache use the keyV computeIfAbsent(K key, Function<K,V> loader, boolean cacheNullWhenLoaderReturnNull, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWritedefault void put(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWritePUT(Object, Object, long, TimeUnit)default CacheResult PUT(K key, V 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 keyCacheResult PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWritedefault void putAll(Map<? extends K,? extends V> map, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWritePUT_ALL(Map, long, TimeUnit)default CacheResult PUT_ALL(Map<? extends K,? extends V> map)
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.CacheResult PUT_ALL(Map<? extends K,? extends V> map, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWriteCacheResult REMOVE(K key)
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 cacheCacheResult REMOVE_ALL(Set<? extends K> 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 removeCacheResult PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
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 expireAfterWriteCopyright © 2013–2019. All rights reserved.