public class LFUAgingCache<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.
Note: this implementation is rather heavyweight, in that it does some expensive work on calls
to ExpiryCache.get(Object) and when determining which elements to expire. It should only be
chosen if the specific expiration policy is important to the logic of client code. Otherwise,
LRUCache may be a better choice in terms of efficiency.
| Modifier and Type | Class and Description |
|---|---|
private static class |
LFUAgingCache.LFUEntry<K,V>
A sortable entry containing the hashable key and value of a cache entry.
|
Cache.Entry<K,V>| Modifier and Type | Field and Description |
|---|---|
private static double |
CLEAN_RATIO
Portion of cache limit to be expired when the cache is full and capacity is needed
|
private java.util.TreeSet<Cache.Entry<K,V>> |
sorted
Sorted set of key-value entries
|
cache, limit, log, name| Constructor and Description |
|---|
LFUAgingCache(int limit)
Constructor.
|
LFUAgingCache(java.lang.String name,
int limit)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
ageEntries()
Recompute the relevance of entries with older access ages.
|
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 |
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.
|
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), replaced by 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.
|
java.util.Map<K,V> |
getMFUEntries(int maxEntries)
Retrieve the Most Frequently Used items out of cache.
|
protected Cache.Entry<K,V> |
nextExpiredEntry()
Get the next expired entry to be reaped.
|
addCacheExpiryListener, containsKey, entries, get, keys, put, putIfAbsent, remove, removeCacheExpiryListener, size, valuesprivate static final double CLEAN_RATIO
private final java.util.TreeSet<Cache.Entry<K,V>> sorted
public LFUAgingCache(int limit)
limit - Maximum number of elements allowed in the cache.java.lang.IllegalArgumentException - if the specified limit is less than one.public LFUAgingCache(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 java.util.Map<K,V> getMFUEntries(int maxEntries)
maxEntries - - maximum number of entries to returnprotected 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 to
the sorted set.entryAdded in class ExpiryCache<K,V>entry - Entry which was added.protected void entryAccessed(Cache.Entry<K,V> entry)
ExpiryCache.get(Object). Re-order the entry
within the sorted set.entryAccessed in class ExpiryCache<K,V>entry - Entry which was accessed.protected void entryRemoved(Cache.Entry<K,V> entry)
ExpiryCache.remove(Object), replaced by a call
to ExpiryCache.put(Object, Object), or was removed due to its expiration. Remove the entry from
the sorted set.entryRemoved in class ExpiryCache<K,V>entry - Entry which was removed.protected void ensureCapacity()
When elements are expired from the cache, a cache expiry event is sent to all registered listeners.
ensureCapacity in class ExpiryCache<K,V>private void ageEntries()