public class LRUCache<K,V>
extends java.lang.Object
implements java.lang.Cloneable
LRUCache is a hashtable that stores a finite number of elements.
When an attempt is made to add values to a full cache, the least recently used values
in the cache are discarded to make room for the new values as necessary.
The data structure is based on the LRU virtual memory paging scheme.
Objects can take up a variable amount of cache space by implementing
the ILRUCacheable interface.
This implementation is NOT thread-safe. Synchronization wrappers would have to be added to ensure atomic insertions and deletions from the cache.
ILRUCacheable| Modifier and Type | Class and Description |
|---|---|
static class |
LRUCache.LRUCacheEntry<K,V>
This type is used internally by the LRUCache to represent entries
stored in the cache.
|
class |
LRUCache.Stats |
| Modifier and Type | Field and Description |
|---|---|
protected int |
currentSpace
Amount of cache space used so far
|
protected static int |
DEFAULT_SPACELIMIT
Default amount of space in the cache
|
protected LRUCache.LRUCacheEntry<K,V> |
entryQueue
Start of queue (most recently used entry)
|
protected LRUCache.LRUCacheEntry<K,V> |
entryQueueTail
End of queue (least recently used entry)
|
protected java.util.Hashtable<K,LRUCache.LRUCacheEntry<K,V>> |
entryTable
Hash table for fast random access to cache entries
|
protected int |
spaceLimit
Maximum space allowed in cache
|
protected int |
timestampCounter
Counter for handing out sequential timestamps
|
| Constructor and Description |
|---|
LRUCache()
Creates a new cache.
|
LRUCache(int size)
Creates a new cache.
|
| Modifier and Type | Method and Description |
|---|---|
LRUCache<K,V> |
clone()
Returns a new cache containing the same contents.
|
double |
fillingRatio() |
void |
flush()
Flushes all entries from the cache.
|
void |
flush(K key)
Flushes the given entry from the cache.
|
V |
get(K key)
Answers the value in the cache at the given key.
|
int |
getCurrentSpace()
Returns the amount of space that is current used in the cache.
|
K |
getKey(K key) |
int |
getNewestTimestampCounter()
Returns the timestamps of the most recently used element in the cache.
|
K |
getOldestElement()
Returns the lest recently used element in the cache, can return
null |
int |
getOldestTimestampCounter()
Returns the timestamps of the least recently used element in the cache.
|
int |
getSpaceLimit()
Returns the maximum amount of space available in the cache.
|
java.util.Enumeration<K> |
keys()
Returns an Enumeration of the keys currently in the cache.
|
ICacheEnumeration<K,V> |
keysAndValues()
Returns an enumeration that iterates over all the keys and values
currently in the cache.
|
protected boolean |
makeSpace(int space)
Ensures there is the specified amount of free space in the receiver,
by removing old entries if necessary.
|
protected LRUCache<K,V> |
newInstance(int size)
Returns a new LRUCache instance
|
V |
peek(K key)
Answers the value in the cache at the given key.
|
protected void |
privateAdd(K key,
V value,
int space)
Adds an entry for the given key/value/space.
|
protected void |
privateAddEntry(LRUCache.LRUCacheEntry<K,V> entry,
boolean shuffle)
Adds the given entry from the receiver.
|
protected void |
privateRemoveEntry(LRUCache.LRUCacheEntry<K,V> entry,
boolean shuffle)
Removes the entry from the entry queue.
|
V |
put(K key,
V value)
Sets the value in the cache at the given key.
|
V |
removeKey(K key)
Removes and returns the value in the cache for the given key.
|
void |
setSpaceLimit(int limit)
Sets the maximum amount of space that the cache can store
|
protected int |
spaceFor(V value)
Returns the space taken by the given value.
|
java.lang.String |
toString()
Returns a String that represents the value of this object.
|
protected java.lang.String |
toStringContents()
Returns a String that represents the contents of this object.
|
java.lang.String |
toStringFillingRation(java.lang.String cacheName) |
protected void |
updateTimestamp(LRUCache.LRUCacheEntry<K,V> entry)
Updates the timestamp for the given entry, ensuring that the queue is
kept in correct order.
|
protected int currentSpace
protected int spaceLimit
protected int timestampCounter
protected java.util.Hashtable<K,LRUCache.LRUCacheEntry<K,V>> entryTable
protected LRUCache.LRUCacheEntry<K,V> entryQueue
protected LRUCache.LRUCacheEntry<K,V> entryQueueTail
protected static final int DEFAULT_SPACELIMIT
public LRUCache()
DEFAULT_SPACELIMIT.public LRUCache(int size)
size - Size of Cachepublic LRUCache<K,V> clone()
clone in class java.lang.Objectpublic double fillingRatio()
public void flush()
public void flush(K key)
key - Key of object to flushpublic V get(K key)
key - Hash table key of object to retrievepublic int getCurrentSpace()
public int getNewestTimestampCounter()
public int getOldestTimestampCounter()
public K getOldestElement()
nullpublic int getSpaceLimit()
public java.util.Enumeration<K> keys()
public ICacheEnumeration<K,V> keysAndValues()
protected boolean makeSpace(int space)
space - Amount of space to free uppublic V peek(K key)
protected void privateAdd(K key, V value, int space)
protected void privateAddEntry(LRUCache.LRUCacheEntry<K,V> entry, boolean shuffle)
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)
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)
key - Key of object to add.value - Value of object to add.public V removeKey(K key)
key - Key of object to remove from cache.public void setSpaceLimit(int limit)
limit - Number of units of cache spaceprotected int spaceFor(V value)
public java.lang.String toString()
toString in class java.lang.Objectprotected java.lang.String toStringContents()
public java.lang.String toStringFillingRation(java.lang.String cacheName)
protected void updateTimestamp(LRUCache.LRUCacheEntry<K,V> entry)