接口 Cache<K,V>

所有超级接口:
AutoCloseable, Closeable
所有已知子接口:
ProxyCache<K,V>
所有已知实现类:
AbstractCache, AbstractEmbeddedCache, AbstractExternalCache, CaffeineCache, LinkedHashMapCache, LoadingCache, MockRemoteCache, MultiLevelCache, RefreshCache, SimpleProxyCache

public interface Cache<K,V> extends Closeable
The cache interface, null value is supported.
作者:
huangli
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    static final org.slf4j.Logger
     
  • 方法概要

    修饰符和类型
    方法
    说明
    default void
    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.
    Get the config of this cache.
    default V
    get(K key)
    Gets an entry from the cache.
    GET(K key)
    Gets an entry from the cache.
    GET_ALL(Set<? extends K> keys)
    Gets a collection of entries 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 void
    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.
    default CacheResult
    PUT(K key, V value)
    Associates the specified value with the specified key in the cache.
    PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
    Associates the specified value with the specified key in the cache.
    default CacheResult
    PUT_ALL(Map<? extends K,? extends V> map)
    Copies all of the entries from the specified map to the cache.
    PUT_ALL(Map<? extends K,? extends V> map, long expireAfterWrite, TimeUnit timeUnit)
    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
    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.
    default boolean
    remove(K key)
    Removes the mapping for a key from this cache if it is present.
    REMOVE(K key)
    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
    removeAll(Set<? extends K> keys)
    Removes entries for the specified keys.
    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.
  • 字段详细资料

    • logger

      static final org.slf4j.Logger logger
  • 方法详细资料

    • get

      default V get(K key) throws CacheInvokeException
      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

      default Map<K,V> getAll(Set<? extends K> keys) throws CacheInvokeException
      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

      default void put(K key, V value)
      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 associated
      value - value to be associated with the specified key
      另请参阅:
    • putAll

      default void putAll(Map<? extends K,? extends V> map)
      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

      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.

      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 associated
      value - 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

      default boolean remove(K key)
      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

      default void removeAll(Set<? extends K> keys)
      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

      <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.

      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

      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. 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 - lockKey
      expire - lock expire time
      timeUnit - 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

      default boolean tryLockAndRun(K key, long expire, TimeUnit timeUnit, Runnable action)
      Use this cache to try run an action exclusively.

      MultiLevelCache will use the last level cache to support this operation.

      examples:
       cache.tryLock("MyKey",100, TimeUnit.SECONDS),() -> {
           //do something
       });
       
      参数:
      key - lockKey
      expire - lock expire time
      timeUnit - lock expire time unit
      action - the action need to execute
      返回:
      true if successfully get the lock and the action is executed
    • GET

      CacheGetResult<V> GET(K key)
      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

      MultiGetResult<K,V> GET_ALL(Set<? extends K> keys)
      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

      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.
      参数:
      key - the key
      loader - the value loader
      返回:
      the value
      另请参阅:
    • computeIfAbsent

      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.
      参数:
      key - the key
      loader - the value loader
      cacheNullWhenLoaderReturnNull - 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 key
      loader - the value loader
      cacheNullWhenLoaderReturnNull - true if null value returned by loader should put into cache use the key
      expireAfterWrite - the TTL(time to live) of the KV association
      timeUnit - the time unit of expireAfterWrite
      返回:
      the value
    • put

      default void put(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
      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 associated
      value - value to be associated with the specified key
      expireAfterWrite - the TTL(time to live) of the KV association
      timeUnit - the time unit of expireAfterWrite
      另请参阅:
    • PUT

      default CacheResult PUT(K key, V value)
      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 associated
      value - value to be associated with the specified key
      返回:
      the result
    • PUT

      CacheResult PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit)
      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 associated
      value - value to be associated with the specified key
      expireAfterWrite - the TTL(time to live) of the KV association
      timeUnit - the time unit of expireAfterWrite
      返回:
      the result
    • putAll

      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.

      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 association
      timeUnit - the time unit of expireAfterWrite
      另请参阅:
    • PUT_ALL

      default CacheResult PUT_ALL(Map<? extends K,? extends V> map)
      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

      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.

      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 association
      timeUnit - the time unit of expireAfterWrite
      返回:
      the result
    • REMOVE

      CacheResult REMOVE(K key)
      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

      CacheResult REMOVE_ALL(Set<? extends K> keys)
      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

      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.

      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 associated
      value - value to be associated with the specified key
      expireAfterWrite - the TTL(time to live) of the KV association
      timeUnit - 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.