K - The type of the keys used in the cache.V - The type of the values used in the cache.public class LeastRecentlyUsedCache<K,V> extends Object
The cache keeps track of the values' last-access time automatically. Every
time a value is read from or put to the store, the access-time is updated.
This update may be suppressed for read access by updateOnReadAccess.
A value can be successfully added to the cache if any of the following conditions is met:
Depending on evictOnReadAccess the read access may evict expired
entries or returns them and updates the last-access time, if not suppressed
by updateOnReadAccess.
This implementation uses a HashMap as its backing store. In
addition to that the cache keeps a doubly-linked list of the entries in
access-time order.
Access to the cache's entries (e.g. put, get, remove, find) is not thread safe. Clients should explicitly serialize access to the cache from multiple threads.
Insertion, lookup and removal of entries is done in O(log n).
Note: if the expiration threshold is0, "stale" is not
applied in get(Object) (otherwise that get would never return
something).| Modifier and Type | Class and Description |
|---|---|
static interface |
LeastRecentlyUsedCache.EvictionListener<V>
A callback for getting notified about entries being evicted from the
cache.
|
static interface |
LeastRecentlyUsedCache.Predicate<V>
A predicate to be applied to cache entries to determine the result set
when searching for particular values.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_CAPACITY
The cache's default maximum capacity.
|
static int |
DEFAULT_INITIAL_CAPACITY
The cache's default initial capacity.
|
static long |
DEFAULT_THRESHOLD_SECS
The default number of seconds after which an entry is considered
stale if it hasn't been accessed for that amount of time.
|
| Constructor and Description |
|---|
LeastRecentlyUsedCache()
Creates a cache with an initial capacity of
DEFAULT_INITIAL_CAPACITY, a maximum capacity of
DEFAULT_CAPACITY entries and an expiration threshold of
DEFAULT_THRESHOLD_SECS seconds. |
LeastRecentlyUsedCache(int initialCapacity,
int maxCapacity,
long threshold)
Creates a cache based on given configuration parameters.
|
LeastRecentlyUsedCache(int capacity,
long threshold)
Creates a cache based on given configuration parameters.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEvictionListener(LeastRecentlyUsedCache.EvictionListener<V> listener)
Registers a listener to be notified about (stale) entries being evicted
from the cache.
|
void |
clear()
Removes all entries from the cache.
|
V |
find(LeastRecentlyUsedCache.Predicate<V> predicate)
Finds a value based on a predicate.
|
V |
find(LeastRecentlyUsedCache.Predicate<V> predicate,
boolean unique)
Finds a value based on a predicate.
|
V |
get(K key)
Gets a value from the cache.
|
int |
getCapacity()
Gets the maximum number of entries this cache can manage.
|
long |
getExpirationThreshold()
Gets the period of time after which an entry is considered stale
if it hasn't be accessed.
|
boolean |
isEvictingOnReadAccess()
Get evict mode on read access.
|
boolean |
isUpdatingOnReadAccess()
Get update last-access time mode on read access.
|
boolean |
put(K key,
V value)
Puts an entry to the cache.
|
int |
remainingCapacity()
Gets the number of entries that can be added to this cache without the
need for removing stale entries.
|
V |
remove(K key)
Removes an entry from the cache.
|
V |
remove(K key,
V value)
Removes provided entry from the cache.
|
void |
setCapacity(int capacity)
Sets the maximum number of entries this cache can manage.
|
void |
setEvictingOnReadAccess(boolean evict)
Set evict mode on read access.
|
void |
setExpirationThreshold(long newThreshold)
Sets the period of time after which an entry is to be considered stale if
it hasn't be accessed.
|
void |
setExpirationThreshold(long newThreshold,
TimeUnit unit)
Sets the period of time after which an entry is to be considered stale if
it hasn't be accessed.
|
void |
setUpdatingOnReadAccess(boolean update)
Set update last-access time mode on read access.
|
int |
size()
Gets the cache's current number of entries.
|
boolean |
update(K key)
Update the last-access time.
|
Collection<V> |
values()
Gets all connections contained in this cache.
|
Iterator<V> |
valuesIterator()
Gets iterator over all connections contained in this cache.
|
public static final int DEFAULT_INITIAL_CAPACITY
public static final long DEFAULT_THRESHOLD_SECS
public static final int DEFAULT_CAPACITY
public LeastRecentlyUsedCache()
DEFAULT_INITIAL_CAPACITY, a maximum capacity of
DEFAULT_CAPACITY entries and an expiration threshold of
DEFAULT_THRESHOLD_SECS seconds.public LeastRecentlyUsedCache(int capacity,
long threshold)
The cache's initial capacity is set to the lesser of
DEFAULT_INITIAL_CAPACITY and capacity.
capacity - the maximum number of entries the cache can managethreshold - the period of time of inactivity (in seconds) after
which an entry is considered stale and can be evicted from the
cache if a new entry is to be added to the cachepublic LeastRecentlyUsedCache(int initialCapacity,
int maxCapacity,
long threshold)
initialCapacity - The initial number of entries the cache will be
initialized to support. The cache's capacity will be doubled
dynamically every time 0.75 percent of its current capacity is
used but it will never exceed maxCapacity.maxCapacity - The maximum number of entries the cache can managethreshold - The period of time of inactivity (in seconds) after
which an entry is considered stale and can be evicted from the
cache if a new entry is to be added to the cachepublic void addEvictionListener(LeastRecentlyUsedCache.EvictionListener<V> listener)
listener - the listenerpublic boolean isEvictingOnReadAccess()
true, if entries are evicted on read access, when
expired, false, if not.public void setEvictingOnReadAccess(boolean evict)
evict - true, if entries are evicted on read access, when
expired, false, if not.public boolean isUpdatingOnReadAccess()
true, if entries last-access time is updated on read
access, false, if not.public void setUpdatingOnReadAccess(boolean update)
update - true,if entries last-access time is updated on read
access, false, if not.public final long getExpirationThreshold()
public final void setExpirationThreshold(long newThreshold)
newThreshold - the threshold in secondsput(Object, Object),
get(Object),
find(Predicate)public final void setExpirationThreshold(long newThreshold,
TimeUnit unit)
newThreshold - the thresholdunit - TimeUnit for thresholdput(Object, Object),
get(Object),
find(Predicate)public final int getCapacity()
public final void setCapacity(int capacity)
capacity - the maximum number of entries the cache can manageput(Object, Object),
get(Object)public final int size()
public final int remainingCapacity()
public final void clear()
public final boolean put(K key, V value)
EvictionListeners.key - the key to store the value undervalue - the value to storetrue if the entry could be added to the cache,
false otherwise, e.g. because the cache's remaining
capacity is zero and no stale entries can be evictedaddEvictionListener(EvictionListener)public final V get(K key)
key - the key to look up in the cachenull otherwisepublic final boolean update(K key)
updateOnReadAccess.key - the key to update the last-access time.true, if updated, false, otherwise.public final V remove(K key)
EvictionListeners.key - the key of the entry to removenull, if the cache does not contain
the keypublic final V remove(K key, V value)
EvictionListeners.key - the key of the entry to removevalue - value of the entry to removenull, if the cache does not contain
the key or entrypublic final V find(LeastRecentlyUsedCache.Predicate<V> predicate)
predicate - the condition to match. Assumed to match entries in a
unique manner.null, if no value matchespublic final V find(LeastRecentlyUsedCache.Predicate<V> predicate, boolean unique)
evictOnReadAccess
setting.predicate - the condition to matchunique - true, if the predicate matches entries in a unique
manner, false, if more entries may be matched.null, if no value matchespublic final Iterator<V> valuesIterator()
The iterator returned is directly backed by this cache's underlying map. If the cache is modified while an iteration over the values is in progress, the results of the iteration are undefined.
Removal of connections from the iterator is unsupported.
public final Collection<V> values()
UnsupportedOperationException.
Note: the evictOnReadAccess feature may alter the underlying map
even for read access. Therefore the clients should explicitly serialize
access to the returned collection from multiple threads. The returned
size doesn't reflect potential eviction on read-access.Copyright © 2019 Eclipse Foundation. All rights reserved.