public abstract class OverflowingLRUCache<K,V> extends LRUCache<K,V>
OverflowingLRUCache is an LRUCache which attempts
to maintain a size equal or less than its fSpaceLimit
by removing the least recently used elements.
The cache will remove elements which successfully close and all elements which are explicitly removed.
If the cache cannot remove enough old elements to add new elements
it will grow beyond fSpaceLimit. Later, it will attempt to
shrink back to the maximum space limit.
The method close should attempt to close the element. If
the element is successfully closed it will return true and the element will
be removed from the cache. Otherwise the element will remain in the cache.
The cache implicitly attempts shrinks on calls to putand
setSpaceLimit. Explicitly calling the shrink method
will also cause the cache to attempt to shrink.
The cache calculates the used space of all elements which implement
ILRUCacheable. All other elements are assumed to be of size one.
Use the #peek(Object) and #disableTimestamps() method to
circumvent the timestamp feature of the cache. This feature is intended to be used
only when the #close(LRUCacheEntry) method causes changes to the cache.
For example, if a parent closes its children when #close(LRUCacheEntry) is called,
it should be careful not to change the LRU linked list. It can be sure it is not causing
problems by calling #peek(Object) instead of #get(Object) method.
LRUCacheLRUCache.LRUCacheEntry<K,V>, LRUCache.Stats| Modifier and Type | Field and Description |
|---|---|
protected double |
loadFactor
Indicates how much space should be reclaimed when the cache overflows.
|
protected int |
overflow
Indicates if the cache has been over filled and by how much.
|
protected boolean |
timestampsOn
Indicates whether or not timestamps should be updated
|
currentSpace, DEFAULT_SPACELIMIT, entryQueue, entryQueueTail, entryTable, spaceLimit, timestampCounter| Constructor and Description |
|---|
OverflowingLRUCache(int size)
Creates a OverflowingLRUCache.
|
OverflowingLRUCache(int size,
int overflow)
Creates a OverflowingLRUCache.
|
| Modifier and Type | Method and Description |
|---|---|
OverflowingLRUCache<K,V> |
clone()
Returns a new cache containing the same contents.
|
protected abstract boolean |
close(LRUCache.LRUCacheEntry<K,V> entry)
Returns true if the element is successfully closed and
removed from the cache, otherwise false.
|
java.util.Enumeration<V> |
elements()
Returns an enumerator of the values in the cache with the most
recently used first.
|
double |
fillingRatio() |
java.util.Hashtable<K,LRUCache.LRUCacheEntry<K,V>> |
getEntryTable()
For internal testing only.
|
double |
getLoadFactor()
Returns the load factor for the cache.
|
int |
getOverflow() |
protected boolean |
makeSpace(int space)
Ensures there is the specified amount of free space in the receiver,
by removing old entries if necessary.
|
protected abstract LRUCache<K,V> |
newInstance(int size,
int newOverflow)
Returns a new instance of the reciever.
|
void |
printStats()
For testing purposes only
|
protected void |
privateRemoveEntry(LRUCache.LRUCacheEntry<K,V> entry,
boolean shuffle)
Removes the entry from the entry queue.
|
protected void |
privateRemoveEntry(LRUCache.LRUCacheEntry<K,V> entry,
boolean shuffle,
boolean external)
Removes the entry from the entry queue.
|
V |
put(K key,
V value)
Sets the value in the cache at the given key.
|
V |
remove(K key)
Removes and returns the value in the cache for the given key.
|
void |
setLoadFactor(double newLoadFactor)
Sets the load factor for the cache.
|
void |
setSpaceLimit(int limit)
Sets the maximum amount of space that the cache can store
|
boolean |
shrink()
Attempts to shrink the cache if it has overflown.
|
java.lang.String |
toString()
Returns a String that represents the value of this object.
|
protected void |
updateTimestamp(LRUCache.LRUCacheEntry<K,V> entry)
Updates the timestamp for the given entry, ensuring that the queue is
kept in correct order.
|
flush, flush, get, getCurrentSpace, getKey, getNewestTimestampCounter, getOldestElement, getOldestTimestampCounter, getSpaceLimit, keys, keysAndValues, newInstance, peek, privateAdd, privateAddEntry, removeKey, spaceFor, toStringContents, toStringFillingRationprotected int overflow
protected boolean timestampsOn
protected double loadFactor
public OverflowingLRUCache(int size)
size - Size limit of cache.public OverflowingLRUCache(int size,
int overflow)
size - Size limit of cache.overflow - Size of the overflow.public OverflowingLRUCache<K,V> clone()
protected abstract boolean close(LRUCache.LRUCacheEntry<K,V> entry)
NOTE: this triggers an external remove from the cache by closing the object.
public java.util.Enumeration<V> elements()
public double fillingRatio()
fillingRatio in class LRUCache<K,V>public java.util.Hashtable<K,LRUCache.LRUCacheEntry<K,V>> getEntryTable()
public double getLoadFactor()
public int getOverflow()
protected boolean makeSpace(int space)
protected abstract LRUCache<K,V> newInstance(int size, int newOverflow)
public void printStats()
protected void privateRemoveEntry(LRUCache.LRUCacheEntry<K,V> entry, boolean shuffle)
privateRemoveEntry with the external functionality enabled.privateRemoveEntry in class LRUCache<K,V>shuffle - indicates whether we are just shuffling the queue
(in which case, the entry table is not modified).protected void privateRemoveEntry(LRUCache.LRUCacheEntry<K,V> entry, boolean shuffle, boolean external)
shuffle - indicates whether we are just shuffling the queue
(in which case, the entry table is not modified).public V put(K key, V value)
LRUCachepublic V remove(K key)
key - Key of object to remove from cache.public void setLoadFactor(double newLoadFactor)
throws java.lang.IllegalArgumentException
newLoadFactor - doublejava.lang.IllegalArgumentException - when the new load factor is not in (0.0, 1.0]public void setSpaceLimit(int limit)
LRUCachesetSpaceLimit in class LRUCache<K,V>limit - Number of units of cache spacepublic boolean shrink()
fSpaceLimit.public java.lang.String toString()
protected void updateTimestamp(LRUCache.LRUCacheEntry<K,V> entry)
This method will do nothing if timestamps have been disabled.
updateTimestamp in class LRUCache<K,V>