K
- Key typeV
- Value typepublic class ExpiringMap<K,V>
extends java.lang.Object
implements java.util.concurrent.ConcurrentMap<K,V>
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();
限定符和类型 | 类和说明 |
---|---|
static class |
ExpiringMap.Builder<K,V>
Builds ExpiringMap instances.
|
限定符和类型 | 方法和说明 |
---|---|
void |
addAsyncExpirationListener(ExpirationListener<K,V> listener)
Adds an asynchronous expiration listener.
|
void |
addExpirationListener(ExpirationListener<K,V> listener)
Adds an expiration listener.
|
static ExpiringMap.Builder<java.lang.Object,java.lang.Object> |
builder()
Creates an ExpiringMap builder.
|
void |
clear() |
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
static <K,V> ExpiringMap<K,V> |
create()
Creates a new instance of ExpiringMap with ExpirationPolicy.CREATED and an expiration of 60 seconds.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
boolean |
equals(java.lang.Object obj) |
V |
get(java.lang.Object key) |
long |
getExpectedExpiration(K key)
Gets the expected expiration, in milliseconds from the current time, for the entry corresponding to the given
key . |
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 given
key . |
int |
getMaxSize()
Gets the maximum size of the map.
|
int |
hashCode() |
boolean |
isEmpty() |
java.util.Set<K> |
keySet() |
V |
put(K key,
V value)
Puts
value in the map for key . |
V |
put(K key,
V value,
ExpirationPolicy expirationPolicy) |
V |
put(K key,
V value,
ExpirationPolicy expirationPolicy,
long duration,
java.util.concurrent.TimeUnit timeUnit)
Puts
value in the map for key . |
V |
put(K key,
V value,
long duration,
java.util.concurrent.TimeUnit timeUnit) |
void |
putAll(java.util.Map<? extends K,? extends V> map) |
V |
putIfAbsent(K key,
V value) |
V |
remove(java.lang.Object key) |
boolean |
remove(java.lang.Object key,
java.lang.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 to
key . |
void |
setExpiration(K key,
long duration,
java.util.concurrent.TimeUnit timeUnit)
Sets the expiration duration for the entry corresponding to the given key.
|
void |
setExpiration(long duration,
java.util.concurrent.TimeUnit timeUnit)
Updates the default map entry expiration.
|
void |
setExpirationPolicy(ExpirationPolicy expirationPolicy)
Sets the global expiration policy for the map.
|
void |
setExpirationPolicy(K key,
ExpirationPolicy expirationPolicy)
Sets the expiration policy for the entry corresponding to the given key.
|
void |
setMaxSize(int maxSize)
Sets the maximum size of the map.
|
static void |
setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Sets the
ThreadFactory that is used to create expiration and listener callback threads for all ExpiringMap
instances. |
int |
size() |
java.lang.String |
toString() |
java.util.Collection<V> |
values() |
public static void setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
ThreadFactory
that is used to create expiration and listener callback threads for all ExpiringMap
instances.threadFactory
- ThreadFactoryjava.lang.NullPointerException
- if threadFactory
is nullpublic static ExpiringMap.Builder<java.lang.Object,java.lang.Object> builder()
public static <K,V> ExpiringMap<K,V> create()
K
- KV
- Vpublic void addExpirationListener(ExpirationListener<K,V> listener)
listener
- to addjava.lang.NullPointerException
- if listener
is nullpublic void addAsyncExpirationListener(ExpirationListener<K,V> listener)
listener
- to addjava.lang.NullPointerException
- if listener
is nullpublic boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public boolean equals(java.lang.Object obj)
public long getExpiration()
public long getExpiration(K key)
key
- Kjava.lang.NullPointerException
- if key
is nulljava.util.NoSuchElementException
- If no entry exists for the given keypublic ExpirationPolicy getExpirationPolicy(K key)
key
.key
- Kkey
java.lang.NullPointerException
- if key
is nulljava.util.NoSuchElementException
- If no entry exists for the given keypublic long getExpectedExpiration(K key)
key
.key
- Kjava.lang.NullPointerException
- if key
is nulljava.util.NoSuchElementException
- If no entry exists for the given keypublic int getMaxSize()
public int hashCode()
public V put(K key, V value)
value
in the map for key
. Resets the entry's expiration unless an entry already exists for the
same key
and value
.public V put(K key, V value, ExpirationPolicy expirationPolicy)
public V put(K key, V value, ExpirationPolicy expirationPolicy, long duration, java.util.concurrent.TimeUnit timeUnit)
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 forvalue
- Value to put for keyexpirationPolicy
- ExpirationPolicyduration
- the length of time after an entry is created that it should be removedtimeUnit
- the unit that duration
is expressed injava.lang.UnsupportedOperationException
- If variable expiration is not enabledjava.lang.NullPointerException
- if key
, expirationPolicy
or timeUnit
are nullpublic boolean remove(java.lang.Object key, java.lang.Object value)
public void removeExpirationListener(ExpirationListener<K,V> listener)
listener
- ExpirationListenerjava.lang.NullPointerException
- if listener
is nullpublic void removeAsyncExpirationListener(ExpirationListener<K,V> listener)
listener
- ExpirationListenerjava.lang.NullPointerException
- if listener
is nullpublic void resetExpiration(K key)
key
.key
- to reset expiration forjava.lang.NullPointerException
- if key
is nullpublic void setExpiration(K key, long duration, java.util.concurrent.TimeUnit timeUnit)
key
- Key to set expiration forduration
- the length of time after an entry is created that it should be removedtimeUnit
- the unit that duration
is expressed injava.lang.NullPointerException
- if key
or timeUnit
are nulljava.lang.UnsupportedOperationException
- If variable expiration is not enabledpublic void setExpiration(long duration, java.util.concurrent.TimeUnit timeUnit)
duration
- the length of time after an entry is created that it should be removedtimeUnit
- the unit that duration
is expressed injava.lang.NullPointerException
- timeUnit
is nulljava.lang.UnsupportedOperationException
- If variable expiration is not enabledpublic void setExpirationPolicy(ExpirationPolicy expirationPolicy)
expirationPolicy
- ExpirationPolicyjava.lang.NullPointerException
- expirationPolicy
is nullpublic void setExpirationPolicy(K key, ExpirationPolicy expirationPolicy)
key
- to set policy forexpirationPolicy
- to setjava.lang.NullPointerException
- if key
or expirationPolicy
are nulljava.lang.UnsupportedOperationException
- If variable expiration is not enabledpublic void setMaxSize(int maxSize)
maxSize
- The maximum size of the map.public java.lang.String toString()
toString
在类中 java.lang.Object