类 ExpiringMap<K,V>
java.lang.Object
com.sh.util.concurrent.expiringmap.ExpiringMap<K,V>
- 类型参数:
K- Key typeV- Value type
- 所有已实现的接口:
ConcurrentMap<K,,V> Map<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();
-
嵌套类概要
嵌套类 -
方法概要
修饰符和类型方法说明voidaddAsyncExpirationListener(ExpirationListener<K, V> listener) Adds an asynchronous expiration listener.voidaddExpirationListener(ExpirationListener<K, V> listener) Adds an expiration listener.static ExpiringMap.Builder<Object,Object> builder()Creates an ExpiringMap builder.voidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) static <K,V> ExpiringMap<K, V> create()Creates a new instance of ExpiringMap with ExpirationPolicy.CREATED and an expiration of 60 seconds.entrySet()booleanlonggetExpectedExpiration(K key) Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the givenkey.longReturns the map's default expiration duration in milliseconds.longgetExpiration(K key) Gets the expiration duration in milliseconds for the entry corresponding to the given key.getExpirationPolicy(K key) Gets the ExpirationPolicy for the entry corresponding to the givenkey.intGets the maximum size of the map.inthashCode()booleanisEmpty()keySet()Putsvaluein the map forkey.put(K key, V value, ExpirationPolicy expirationPolicy) put(K key, V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit) Putsvaluein the map forkey.voidputIfAbsent(K key, V value) booleanvoidremoveAsyncExpirationListener(ExpirationListener<K, V> listener) Removes an asynchronous expiration listener.voidremoveExpirationListener(ExpirationListener<K, V> listener) Removes an expiration listener.booleanvoidresetExpiration(K key) Resets expiration for the entry corresponding tokey.voidsetExpiration(long duration, TimeUnit timeUnit) Updates the default map entry expiration.voidsetExpiration(K key, long duration, TimeUnit timeUnit) Sets the expiration duration for the entry corresponding to the given key.voidsetExpirationPolicy(ExpirationPolicy expirationPolicy) Sets the global expiration policy for the map.voidsetExpirationPolicy(K key, ExpirationPolicy expirationPolicy) Sets the expiration policy for the entry corresponding to the given key.voidsetMaxSize(int maxSize) Sets the maximum size of the map.static voidsetThreadFactory(ThreadFactory threadFactory) Sets theThreadFactorythat is used to create expiration and listener callback threads for all ExpiringMap instances.intsize()toString()values()从接口继承的方法 java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
方法详细资料
-
setThreadFactory
Sets theThreadFactorythat is used to create expiration and listener callback threads for all ExpiringMap instances.- 参数:
threadFactory- ThreadFactory- 抛出:
NullPointerException- ifthreadFactoryis null
-
builder
Creates an ExpiringMap builder.- 返回:
- New ExpiringMap builder
-
create
Creates a new instance of ExpiringMap with ExpirationPolicy.CREATED and an expiration of 60 seconds.- 类型参数:
K- KV- V- 返回:
- K,V
-
addExpirationListener
Adds an expiration listener.- 参数:
listener- to add- 抛出:
NullPointerException- iflisteneris null
-
addAsyncExpirationListener
Adds an asynchronous expiration listener.- 参数:
listener- to add- 抛出:
NullPointerException- iflisteneris null
-
clear
public void clear() -
containsKey
- 指定者:
containsKey在接口中Map<K,V>
-
containsValue
- 指定者:
containsValue在接口中Map<K,V>
-
entrySet
-
equals
-
get
-
getExpiration
public long getExpiration()Returns the map's default expiration duration in milliseconds.- 返回:
- The expiration duration (milliseconds)
-
getExpiration
Gets the expiration duration in milliseconds for the entry corresponding to the given key.- 参数:
key- K- 返回:
- The expiration duration in milliseconds
- 抛出:
NullPointerException- ifkeyis nullNoSuchElementException- If no entry exists for the given key
-
getExpirationPolicy
Gets the ExpirationPolicy for the entry corresponding to the givenkey.- 参数:
key- K- 返回:
- The ExpirationPolicy for the
key - 抛出:
NullPointerException- ifkeyis nullNoSuchElementException- If no entry exists for the given key
-
getExpectedExpiration
Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the givenkey.- 参数:
key- K- 返回:
- The expiration duration in milliseconds
- 抛出:
NullPointerException- ifkeyis nullNoSuchElementException- 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() -
isEmpty
public boolean isEmpty() -
keySet
-
put
Putsvaluein the map forkey. Resets the entry's expiration unless an entry already exists for the samekeyandvalue.- 指定者:
put在接口中Map<K,V> - 参数:
key- to put value forvalue- to put for key- 返回:
- the old value
- 抛出:
NullPointerException- ifkeyis null
-
put
-
put
-
put
Putsvaluein the map forkey. Resets the entry's expiration unless an entry already exists for the samekeyandvalue. Requires that variable expiration be enabled.- 参数:
key- Key to put value forvalue- Value to put for keyexpirationPolicy- ExpirationPolicyduration- the length of time after an entry is created that it should be removedtimeUnit- the unit thatdurationis expressed in- 返回:
- the old value
- 抛出:
UnsupportedOperationException- If variable expiration is not enabledNullPointerException- ifkey,expirationPolicyortimeUnitare null
-
putAll
-
putIfAbsent
- 指定者:
putIfAbsent在接口中ConcurrentMap<K,V> - 指定者:
putIfAbsent在接口中Map<K,V>
-
remove
-
remove
-
replace
-
replace
-
removeExpirationListener
Removes an expiration listener.- 参数:
listener- ExpirationListener- 抛出:
NullPointerException- iflisteneris null
-
removeAsyncExpirationListener
Removes an asynchronous expiration listener.- 参数:
listener- ExpirationListener- 抛出:
NullPointerException- iflisteneris null
-
resetExpiration
Resets expiration for the entry corresponding tokey.- 参数:
key- to reset expiration for- 抛出:
NullPointerException- ifkeyis null
-
setExpiration
Sets the expiration duration for the entry corresponding to the given key. Supported only if variable expiration is enabled.- 参数:
key- Key to set expiration forduration- the length of time after an entry is created that it should be removedtimeUnit- the unit thatdurationis expressed in- 抛出:
NullPointerException- ifkeyortimeUnitare nullUnsupportedOperationException- If variable expiration is not enabled
-
setExpiration
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 removedtimeUnit- the unit thatdurationis expressed in- 抛出:
NullPointerException-timeUnitis nullUnsupportedOperationException- If variable expiration is not enabled
-
setExpirationPolicy
Sets the global expiration policy for the map. Individual expiration policies may override the global policy.- 参数:
expirationPolicy- ExpirationPolicy- 抛出:
NullPointerException-expirationPolicyis null
-
setExpirationPolicy
Sets the expiration policy for the entry corresponding to the given key.- 参数:
key- to set policy forexpirationPolicy- to set- 抛出:
NullPointerException- ifkeyorexpirationPolicyare nullUnsupportedOperationException- 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() -
toString
-
values
-