public abstract class ExpiryCache<K,V> extends java.lang.Object implements Cache<K,V>
Entities interested in expiry events can register themselves as listeners
and they will receive an event notification
when such
events occur.
This implementation is not synchronized. Access to any of the public methods
of this class (including get(Object)
) modify the internal state of the cache, and must
be synchronized externally.
Modifier and Type | Class and Description |
---|---|
protected static class |
ExpiryCache.Node<K,V>
A sortable node containing the hashable key and value of a cache entry.
|
Modifier and Type | Field and Description |
---|---|
protected java.util.Map<K,ExpiryCache.Node<K,V>> |
cache
Map of keys to sortable, key-value nodes
|
private int |
cleanLimit
Number of elements to clear from cache when it is full and capacity is needed
|
protected int |
limit
Maximum number of elements allowed in the cache
|
private java.util.List<CacheExpiryListener<K,V>> |
listeners
List of cache expiry listeners
|
protected static java.util.logging.Logger |
log
Logger
|
protected java.lang.String |
name
Cache name, for debug and logging purposes
|
protected java.util.TreeSet<ExpiryCache.Node<K,V>> |
sorted
Sorted set of key-value nodes
|
Modifier | Constructor and Description |
---|---|
protected |
ExpiryCache(int limit)
Constructor.
|
protected |
ExpiryCache(java.lang.String name,
int limit)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addCacheExpiryListener(CacheExpiryListener<K,V> listener)
Add the given object to the list of listeners interested in receiving cache expiry events
from this cache.
|
protected abstract ExpiryCache.Node<K,V> |
createNode(K key,
V value)
Create a node of the appropriate type for the given key and value.
|
protected void |
ensureCapacity()
Ensure there is sufficient capacity to add a new record to the cache, by expiring the least
desirable element(s), according to the policy of the cache.
|
V |
get(K key)
Retrieve a value, given its key.
|
protected abstract 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.
|
java.lang.Object |
put(K key,
V value)
Add an entry to the cache, pushing out the least desirable element(s) if the cache is full,
and this entry does not replace an existing entry with the same key.
|
V |
remove(K key)
Remove the entry associated with the given key from the cache.
|
void |
removeCacheExpiryListener(CacheExpiryListener<K,V> listener)
Remove the given object from the list of listeners interested in receiving cache expiry
events from this cache.
|
protected static final java.util.logging.Logger log
protected final java.lang.String name
protected final int limit
protected final java.util.Map<K,ExpiryCache.Node<K,V>> cache
protected final java.util.TreeSet<ExpiryCache.Node<K,V>> sorted
private final int cleanLimit
private java.util.List<CacheExpiryListener<K,V>> listeners
protected ExpiryCache(int limit)
limit
- Maximum number of elements allowed in the cache.java.lang.IllegalArgumentException
- if the specified limit is less than one.protected ExpiryCache(java.lang.String name, int limit)
name
- Cache name, for debug and logging purposes.limit
- Maximum number of elements allowed in the cache.java.lang.IllegalArgumentException
- if the specified limit is less than one.public V get(K key)
Note for multi-threaded access: this method makes changes to the state of the cache. A call to this method should therefore be treated as write access for concurrency purposes.
public java.lang.Object put(K key, V value)
CacheExpiryEvent
being sent to registered listeners before this method returns.public void addCacheExpiryListener(CacheExpiryListener<K,V> listener)
Note that adding listeners (any number) to the cache will add a slight amount of overhead to cache expiry processing, in that the expired elements are collected in a linked hash map, to be sent with a cache expiry event. If no listeners are registered, this collection does not take place.
listener
- Listener to register.public void removeCacheExpiryListener(CacheExpiryListener<K,V> listener)
listener
- Listener to deregister.protected abstract int getCleanLimit()
protected abstract ExpiryCache.Node<K,V> createNode(K key, V value)
key
- Hashable key.value
- Cached value.protected void ensureCapacity()
When elements are expired from the cache, a cache expiry event is sent to all registered listeners.