public class LRUCache<K,V> extends ExpiryCache<K,V>
Registered listeners are notified of each expiry event.
This implementation is not synchronized. Any access to the methods of this class which modify the internal state of the cache must be synchronized externally.
Modifier and Type | Class and Description |
---|---|
private static class |
LRUCache.LRUEntry<K,V>
An entry in a doubly linked list organized from head to tail in the order of least recently
accessed entry to most recently accessed entry.
|
Cache.Entry<K,V>
Modifier and Type | Field and Description |
---|---|
private LRUCache.LRUEntry<K,V> |
head
Least recently used entry
|
private LRUCache.LRUEntry<K,V> |
tail
Most recently used entry
|
cache, limit, log, name
Constructor and Description |
---|
LRUCache(int limit)
Constructor.
|
LRUCache(java.lang.String name,
int limit)
Constructor.
|
LRUCache(java.lang.String name,
int limit,
java.util.function.Function<V,java.lang.Boolean> expirationTest,
CapacityPolicy capacityPolicy)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected Cache.Entry<K,V> |
createEntry(K key,
V value)
Create an entry of the appropriate type for the given key and value.
|
protected void |
entryAccessed(Cache.Entry<K,V> entry)
An entry in the cache was accessed by a call to
ExpiryCache.get(Object) . |
protected void |
entryAdded(Cache.Entry<K,V> entry)
An entry in the cache was added by a call to
ExpiryCache.put(Object, Object) . |
protected void |
entryRemoved(Cache.Entry<K,V> entry)
An entry in the cache was removed by a call to
ExpiryCache.remove(Object) , was replaced with a
call to ExpiryCache.put(Object, Object) , or was removed due to its expiration. |
protected int |
getCleanLimit()
Get the number of elements which will be expired from the cache each time it becomes full,
and capacity is required for a new element.
|
protected Cache.Entry<K,V> |
nextExpiredEntry()
Get the next expired entry to be reaped.
|
addCacheExpiryListener, ensureCapacity, entries, get, keys, put, putIfAbsent, remove, removeCacheExpiryListener, size, values
private LRUCache.LRUEntry<K,V> head
private LRUCache.LRUEntry<K,V> tail
public LRUCache(int limit)
limit
- Maximum number of elements allowed in the cache.java.lang.IllegalArgumentException
- if the specified limit is less than one.public LRUCache(java.lang.String name, int limit)
name
- Cache name, for debug and logging purposes. May be null
.limit
- Maximum number of elements allowed in the cache.java.lang.IllegalArgumentException
- if the specified limit is less than one.public LRUCache(java.lang.String name, int limit, java.util.function.Function<V,java.lang.Boolean> expirationTest, CapacityPolicy capacityPolicy)
name
- Cache name, for debug and logging purposes. May be null
.limit
- Maximum number of elements allowed in the cache.expirationTest
- A function which confirms whether an expiration candidate element may actually be expired.
If null
, no confirmation test is applied and every candidate offered as the next
element to be expired is removed when ensuring capacity for new elements. If supplied, this
function effectively provides a veto capability to disallow an element from being expired.capacityPolicy
- The policy to follow when capacity cannot be ensured, due to expirationTest
vetoing
expiration candidates. If null
, the CapacityPolicy.STRICT
policy is assumed.java.lang.IllegalArgumentException
- if the specified limit is less than one.protected int getCleanLimit()
getCleanLimit
in class ExpiryCache<K,V>
protected Cache.Entry<K,V> createEntry(K key, V value)
createEntry
in class ExpiryCache<K,V>
key
- Hashable key.value
- Cached value.protected Cache.Entry<K,V> nextExpiredEntry()
nextExpiredEntry
in class ExpiryCache<K,V>
protected void entryAdded(Cache.Entry<K,V> entry)
ExpiryCache.put(Object, Object)
. Add the entry as
the tail of the linked list, the most recently used position.entryAdded
in class ExpiryCache<K,V>
entry
- Entry which was added.protected void entryAccessed(Cache.Entry<K,V> entry)
ExpiryCache.get(Object)
. Move the entry to the
tail of the linked list, the most recently used position.entryAccessed
in class ExpiryCache<K,V>
entry
- Entry which was accessed.protected void entryRemoved(Cache.Entry<K,V> entry)
ExpiryCache.remove(Object)
, was replaced with a
call to ExpiryCache.put(Object, Object)
, or was removed due to its expiration. Remove it from
the linked list.entryRemoved
in class ExpiryCache<K,V>
entry
- Entry which was removed.