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 void
addAsyncExpirationListener(ExpirationListener<K,V> listener)
Adds an asynchronous expiration listener.void
addExpirationListener(ExpirationListener<K,V> listener)
Adds an expiration listener.static ExpiringMap.Builder<Object,Object>
builder()
Creates an ExpiringMap builder.void
clear()
boolean
containsKey(Object key)
boolean
containsValue(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.boolean
equals(Object obj)
V
get(Object key)
long
getExpectedExpiration(K key)
Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the givenkey
.long
getExpiration()
Returns the map's default expiration duration in milliseconds.long
getExpiration(K key)
Gets the expiration duration in milliseconds for the entry corresponding to the given key.ExpirationPolicy
getExpirationPolicy(K key)
Gets the ExpirationPolicy for the entry corresponding to the givenkey
.int
getMaxSize()
Gets the maximum size of the map.int
hashCode()
boolean
isEmpty()
Set<K>
keySet()
Returns a copy of the map's keys, which can be iterated over safely by multiple threads.V
put(K key, V value)
Putsvalue
in the map forkey
.V
put(K key, V value, long duration, TimeUnit timeUnit)
V
put(K key, V value, ExpirationPolicy expirationPolicy)
V
put(K key, V value, ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit)
Putsvalue
in the map forkey
.void
putAll(Map<? extends K,? extends V> map)
V
putIfAbsent(K key, V value)
V
remove(Object key)
boolean
remove(Object key, Object value)
void
removeAsyncExpirationListener(ExpirationListener<K,V> listener)
Removes an asynchronous expiration listener.void
removeExpirationListener(ExpirationListener<K,V> listener)
Removes an expiration listener.V
replace(K key, V value)
boolean
replace(K key, V oldValue, V newValue)
void
resetExpiration(K key)
Resets expiration for the entry corresponding tokey
.void
setExpiration(long duration, TimeUnit timeUnit)
Updates the default map entry expiration.void
setExpiration(K key, long duration, TimeUnit timeUnit)
Sets the expiration duration for the entry corresponding to the given key.void
setExpirationPolicy(K key, ExpirationPolicy expirationPolicy)
Sets the expiration policy for the entry corresponding to the given key.void
setExpirationPolicy(ExpirationPolicy expirationPolicy)
Sets the global expiration policy for the map.void
setMaxSize(int maxSize)
Sets the maximum size of the map.static void
setThreadFactory(ThreadFactory threadFactory)
Sets theThreadFactory
that is used to create expiration and listener callback threads for all ExpiringMap instances.int
size()
String
toString()
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 theThreadFactory
that is used to create expiration and listener callback threads for all ExpiringMap instances.- Parameters:
threadFactory
-- Throws:
NullPointerException
- ifthreadFactory
is 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
- iflistener
is null
-
addAsyncExpirationListener
public void addAsyncExpirationListener(ExpirationListener<K,V> listener)
Adds an asynchronous expiration listener.- Parameters:
listener
- to add- Throws:
NullPointerException
- iflistener
is null
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in 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
- ifkey
is 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
- ifkey
is 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
- ifkey
is 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)
Putsvalue
in the map forkey
. Resets the entry's expiration unless an entry already exists for the samekey
andvalue
.- Specified by:
put
in interfaceMap<K,V>
- Parameters:
key
- to put value forvalue
- to put for key- Returns:
- the old value
- Throws:
NullPointerException
- ifkey
is 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)
Putsvalue
in the map forkey
. Resets the entry's expiration unless an entry already exists for the samekey
andvalue
. 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 thatduration
is expressed in- Returns:
- the old value
- Throws:
UnsupportedOperationException
- If variable expiration is not enabledNullPointerException
- ifkey
,expirationPolicy
ortimeUnit
are null
-
putIfAbsent
public V putIfAbsent(K key, V value)
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,V>
- Specified by:
putIfAbsent
in interfaceMap<K,V>
-
removeExpirationListener
public void removeExpirationListener(ExpirationListener<K,V> listener)
Removes an expiration listener.- Parameters:
listener
-- Throws:
NullPointerException
- iflistener
is null
-
removeAsyncExpirationListener
public void removeAsyncExpirationListener(ExpirationListener<K,V> listener)
Removes an asynchronous expiration listener.- Parameters:
listener
-- Throws:
NullPointerException
- iflistener
is null
-
resetExpiration
public void resetExpiration(K key)
Resets expiration for the entry corresponding tokey
.- Parameters:
key
- to reset expiration for- Throws:
NullPointerException
- ifkey
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.- Parameters:
key
- Key to set expiration forduration
- the length of time after an entry is created that it should be removedtimeUnit
- the unit thatduration
is expressed in- Throws:
NullPointerException
- ifkey
ortimeUnit
are 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 thatduration
is expressed in- Throws:
NullPointerException
-timeUnit
is 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
-expirationPolicy
is 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
- ifkey
orexpirationPolicy
are 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.
-
-