类 ExpiringMap<K,V>

java.lang.Object
com.sh.util.concurrent.expiringmap.ExpiringMap<K,V>
类型参数:
K - Key type
V - Value type
所有已实现的接口:
ConcurrentMap<K,V>, Map<K,V>

public class ExpiringMap<K,V> extends Object implements ConcurrentMap<K,V>
A thread-safe map that expires entries. Optional features include expiration policies, variable entry expiration, lazy entry loading, and expiration listeners.

Entries are tracked by expiration time and expired by a single thread.

Expiration listeners are called synchronously as entries are expired and block write operations to the map until they completed. Asynchronous expiration listeners are called on a separate thread pool and do not block map operations.

When variable expiration is disabled (default), put/remove operations have a time complexity O(1). When variable expiration is enabled, put/remove operations have time complexity of O(log n).

Example usages:

 
 Map<String, Integer> map = ExpiringMap.create(); 
 Map<String, Integer> map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).build();
 Map<String, Connection> map = ExpiringMap.builder()
   .expiration(10, TimeUnit.MINUTES)
   .entryLoader(new EntryLoader<String, Connection>() {
     public Connection load(String address) {
       return new Connection(address);
     }
   })
   .expirationListener(new ExpirationListener<String, Connection>() { 
     public void expired(String key, Connection connection) { 
       connection.close();
     } 
   })
   .build();
 
 
  • 方法详细资料

    • setThreadFactory

      public static void setThreadFactory(ThreadFactory threadFactory)
      Sets the ThreadFactory that is used to create expiration and listener callback threads for all ExpiringMap instances.
      参数:
      threadFactory - ThreadFactory
      抛出:
      NullPointerException - if threadFactory is null
    • builder

      public static ExpiringMap.Builder<Object,Object> builder()
      Creates an ExpiringMap builder.
      返回:
      New ExpiringMap builder
    • create

      public static <K, V> ExpiringMap<K,V> create()
      Creates a new instance of ExpiringMap with ExpirationPolicy.CREATED and an expiration of 60 seconds.
      类型参数:
      K - K
      V - V
      返回:
      K,V
    • addExpirationListener

      public void addExpirationListener(ExpirationListener<K,V> listener)
      Adds an expiration listener.
      参数:
      listener - to add
      抛出:
      NullPointerException - if listener is null
    • addAsyncExpirationListener

      public void addAsyncExpirationListener(ExpirationListener<K,V> listener)
      Adds an asynchronous expiration listener.
      参数:
      listener - to add
      抛出:
      NullPointerException - if listener is null
    • clear

      public void clear()
      指定者:
      clear 在接口中 Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      指定者:
      containsKey 在接口中 Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      指定者:
      containsValue 在接口中 Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      指定者:
      entrySet 在接口中 Map<K,V>
    • equals

      public boolean equals(Object obj)
      指定者:
      equals 在接口中 Map<K,V>
      覆盖:
      equals 在类中 Object
    • get

      public V get(Object key)
      指定者:
      get 在接口中 Map<K,V>
    • getExpiration

      public long getExpiration()
      Returns the map's default expiration duration in milliseconds.
      返回:
      The expiration duration (milliseconds)
    • getExpiration

      public long getExpiration(K key)
      Gets the expiration duration in milliseconds for the entry corresponding to the given key.
      参数:
      key - K
      返回:
      The expiration duration in milliseconds
      抛出:
      NullPointerException - if key is null
      NoSuchElementException - If no entry exists for the given key
    • getExpirationPolicy

      public ExpirationPolicy getExpirationPolicy(K key)
      Gets the ExpirationPolicy for the entry corresponding to the given key.
      参数:
      key - K
      返回:
      The ExpirationPolicy for the key
      抛出:
      NullPointerException - if key is null
      NoSuchElementException - If no entry exists for the given key
    • getExpectedExpiration

      public long getExpectedExpiration(K key)
      Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the given key.
      参数:
      key - K
      返回:
      The expiration duration in milliseconds
      抛出:
      NullPointerException - if key is null
      NoSuchElementException - If no entry exists for the given key
    • getMaxSize

      public int getMaxSize()
      Gets the maximum size of the map. Once this size has been reached, adding an additional entry will expire the first entry in line for expiration based on the expiration policy.
      返回:
      The maximum size of the map.
    • hashCode

      public int hashCode()
      指定者:
      hashCode 在接口中 Map<K,V>
      覆盖:
      hashCode 在类中 Object
    • isEmpty

      public boolean isEmpty()
      指定者:
      isEmpty 在接口中 Map<K,V>
    • keySet

      public Set<K> keySet()
      指定者:
      keySet 在接口中 Map<K,V>
    • put

      public V put(K key, V value)
      Puts value in the map for key. Resets the entry's expiration unless an entry already exists for the same key and value.
      指定者:
      put 在接口中 Map<K,V>
      参数:
      key - to put value for
      value - to put for key
      返回:
      the old value
      抛出:
      NullPointerException - if key is null
    • put

      public V put(K key, V value, ExpirationPolicy expirationPolicy)
    • put

      public V put(K key, V value, long duration, TimeUnit timeUnit)
    • put

      public V put(K key, V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit)
      Puts value in the map for key. Resets the entry's expiration unless an entry already exists for the same key and value. Requires that variable expiration be enabled.
      参数:
      key - Key to put value for
      value - Value to put for key
      expirationPolicy - ExpirationPolicy
      duration - the length of time after an entry is created that it should be removed
      timeUnit - the unit that duration is expressed in
      返回:
      the old value
      抛出:
      UnsupportedOperationException - If variable expiration is not enabled
      NullPointerException - if key, expirationPolicy or timeUnit are null
    • putAll

      public void putAll(Map<? extends K,? extends V> map)
      指定者:
      putAll 在接口中 Map<K,V>
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      指定者:
      putIfAbsent 在接口中 ConcurrentMap<K,V>
      指定者:
      putIfAbsent 在接口中 Map<K,V>
    • remove

      public V remove(Object key)
      指定者:
      remove 在接口中 Map<K,V>
    • remove

      public boolean remove(Object key, Object value)
      指定者:
      remove 在接口中 ConcurrentMap<K,V>
      指定者:
      remove 在接口中 Map<K,V>
    • replace

      public V replace(K key, V value)
      指定者:
      replace 在接口中 ConcurrentMap<K,V>
      指定者:
      replace 在接口中 Map<K,V>
    • replace

      public boolean replace(K key, V oldValue, V newValue)
      指定者:
      replace 在接口中 ConcurrentMap<K,V>
      指定者:
      replace 在接口中 Map<K,V>
    • removeExpirationListener

      public void removeExpirationListener(ExpirationListener<K,V> listener)
      Removes an expiration listener.
      参数:
      listener - ExpirationListener
      抛出:
      NullPointerException - if listener is null
    • removeAsyncExpirationListener

      public void removeAsyncExpirationListener(ExpirationListener<K,V> listener)
      Removes an asynchronous expiration listener.
      参数:
      listener - ExpirationListener
      抛出:
      NullPointerException - if listener is null
    • resetExpiration

      public void resetExpiration(K key)
      Resets expiration for the entry corresponding to key.
      参数:
      key - to reset expiration for
      抛出:
      NullPointerException - if key is null
    • setExpiration

      public void setExpiration(K key, long duration, TimeUnit timeUnit)
      Sets the expiration duration for the entry corresponding to the given key. Supported only if variable expiration is enabled.
      参数:
      key - Key to set expiration for
      duration - the length of time after an entry is created that it should be removed
      timeUnit - the unit that duration is expressed in
      抛出:
      NullPointerException - if key or timeUnit are null
      UnsupportedOperationException - If variable expiration is not enabled
    • setExpiration

      public void setExpiration(long duration, TimeUnit timeUnit)
      Updates the default map entry expiration. Supported only if variable expiration is enabled.
      参数:
      duration - the length of time after an entry is created that it should be removed
      timeUnit - the unit that duration is expressed in
      抛出:
      NullPointerException - timeUnit is null
      UnsupportedOperationException - If variable expiration is not enabled
    • setExpirationPolicy

      public void setExpirationPolicy(ExpirationPolicy expirationPolicy)
      Sets the global expiration policy for the map. Individual expiration policies may override the global policy.
      参数:
      expirationPolicy - ExpirationPolicy
      抛出:
      NullPointerException - expirationPolicy is null
    • setExpirationPolicy

      public void setExpirationPolicy(K key, ExpirationPolicy expirationPolicy)
      Sets the expiration policy for the entry corresponding to the given key.
      参数:
      key - to set policy for
      expirationPolicy - to set
      抛出:
      NullPointerException - if key or expirationPolicy are null
      UnsupportedOperationException - If variable expiration is not enabled
    • setMaxSize

      public void setMaxSize(int maxSize)
      Sets the maximum size of the map. Once this size has been reached, adding an additional entry will expire the first entry in line for expiration based on the expiration policy.
      参数:
      maxSize - The maximum size of the map.
    • size

      public int size()
      指定者:
      size 在接口中 Map<K,V>
    • toString

      public String toString()
      覆盖:
      toString 在类中 Object
    • values

      public Collection<V> values()
      指定者:
      values 在接口中 Map<K,V>