Package net.jodah.expiringmap
Class ExpiringMap<K,V>
- java.lang.Object
-
- net.jodah.expiringmap.ExpiringMap<K,V>
-
- Type Parameters:
K- Key typeV- Value type
- All Implemented Interfaces:
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();
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.Set<Map.Entry<K,V>>entrySet()Returns a copy of the map's entries, which can be iterated over safely by multiple threads.booleanequals(Object obj)Vget(Object key)longgetExpectedExpiration(K key)Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the givenkey.longgetExpiration()Returns 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.ExpirationPolicygetExpirationPolicy(K key)Gets the ExpirationPolicy for the entry corresponding to the givenkey.intgetMaxSize()Gets the maximum size of the map.inthashCode()booleanisEmpty()Set<K>keySet()Returns a copy of the map's keys, which can be iterated over safely by multiple threads.Vput(K key, V value)Putsvaluein the map forkey.Vput(K key, V value, long duration, TimeUnit timeUnit)Vput(K key, V value, ExpirationPolicy expirationPolicy)Vput(K key, V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit)Putsvaluein the map forkey.voidputAll(Map<? extends K,? extends V> map)VputIfAbsent(K key, V value)Vremove(Object key)booleanremove(Object key, Object value)voidremoveAsyncExpirationListener(ExpirationListener<K,V> listener)Removes an asynchronous expiration listener.voidremoveExpirationListener(ExpirationListener<K,V> listener)Removes an expiration listener.Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidresetExpiration(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(K key, ExpirationPolicy expirationPolicy)Sets the expiration policy for the entry corresponding to the given key.voidsetExpirationPolicy(ExpirationPolicy expirationPolicy)Sets the global expiration policy for the map.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()StringtoString()Collection<V>values()Returns a copy of the map's values, which can be iterated over safely by multiple threads.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
-
-
-
Method Detail
-
setThreadFactory
public static void setThreadFactory(ThreadFactory threadFactory)
Sets theThreadFactorythat is used to create expiration and listener callback threads for all ExpiringMap instances.- Parameters:
threadFactory-- Throws:
NullPointerException- ifthreadFactoryis null
-
builder
public static ExpiringMap.Builder<Object,Object> builder()
Creates an ExpiringMap builder.- Returns:
- 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.
-
addExpirationListener
public void addExpirationListener(ExpirationListener<K,V> listener)
Adds an expiration listener.- Parameters:
listener- to add- Throws:
NullPointerException- iflisteneris null
-
addAsyncExpirationListener
public void addAsyncExpirationListener(ExpirationListener<K,V> listener)
Adds an asynchronous expiration listener.- Parameters:
listener- to add- Throws:
NullPointerException- iflisteneris null
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K,V>
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Returns a copy of the map's entries, which can be iterated over safely by multiple threads.
-
equals
public boolean equals(Object obj)
-
getExpiration
public long getExpiration()
Returns the map's default expiration duration in milliseconds.- Returns:
- The expiration duration (milliseconds)
-
getExpiration
public long getExpiration(K key)
Gets the expiration duration in milliseconds for the entry corresponding to the given key.- Parameters:
key-- Returns:
- The expiration duration in milliseconds
- Throws:
NullPointerException- ifkeyis nullNoSuchElementException- If no entry exists for the given key
-
getExpirationPolicy
public ExpirationPolicy getExpirationPolicy(K key)
Gets the ExpirationPolicy for the entry corresponding to the givenkey.- Parameters:
key-- Returns:
- The ExpirationPolicy for the
key - Throws:
NullPointerException- ifkeyis nullNoSuchElementException- 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 givenkey.- Parameters:
key-- Returns:
- The expiration duration in milliseconds
- Throws:
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.- Returns:
- The maximum size of the map.
-
hashCode
public int hashCode()
-
keySet
public Set<K> keySet()
Returns a copy of the map's keys, which can be iterated over safely by multiple threads.
-
put
public V put(K key, V value)
Putsvaluein the map forkey. Resets the entry's expiration unless an entry already exists for the samekeyandvalue.- Specified by:
putin interfaceMap<K,V>- Parameters:
key- to put value forvalue- to put for key- Returns:
- the old value
- Throws:
NullPointerException- ifkeyis null
-
put
public V put(K key, V value, ExpirationPolicy expirationPolicy)
-
put
public V put(K key, V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit)
Putsvaluein the map forkey. Resets the entry's expiration unless an entry already exists for the samekeyandvalue. Requires that variable expiration be enabled.- Parameters:
key- Key to put value forvalue- Value to put for keyduration- the length of time after an entry is created that it should be removedtimeUnit- the unit thatdurationis expressed in- Returns:
- the old value
- Throws:
UnsupportedOperationException- If variable expiration is not enabledNullPointerException- ifkey,expirationPolicyortimeUnitare null
-
putIfAbsent
public V putIfAbsent(K key, V value)
- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V>- Specified by:
putIfAbsentin interfaceMap<K,V>
-
removeExpirationListener
public void removeExpirationListener(ExpirationListener<K,V> listener)
Removes an expiration listener.- Parameters:
listener-- Throws:
NullPointerException- iflisteneris null
-
removeAsyncExpirationListener
public void removeAsyncExpirationListener(ExpirationListener<K,V> listener)
Removes an asynchronous expiration listener.- Parameters:
listener-- Throws:
NullPointerException- iflisteneris null
-
resetExpiration
public void resetExpiration(K key)
Resets expiration for the entry corresponding tokey.- Parameters:
key- to reset expiration for- Throws:
NullPointerException- ifkeyis 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.- Parameters:
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- Throws:
NullPointerException- ifkeyortimeUnitare nullUnsupportedOperationException- 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.- Parameters:
duration- the length of time after an entry is created that it should be removedtimeUnit- the unit thatdurationis expressed in- Throws:
NullPointerException-timeUnitis nullUnsupportedOperationException- 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.- Parameters:
expirationPolicy-- Throws:
NullPointerException-expirationPolicyis null
-
setExpirationPolicy
public void setExpirationPolicy(K key, ExpirationPolicy expirationPolicy)
Sets the expiration policy for the entry corresponding to the given key.- Parameters:
key- to set policy forexpirationPolicy- to set- Throws:
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.- Parameters:
maxSize- The maximum size of the map.
-
-