Class LRUCache<K,V>
java.lang.Object
org.aspectj.org.eclipse.jdt.internal.core.util.LRUCache<K,V>
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
OverflowingLRUCache
The
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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis type is used internally by the LRUCache to represent entries stored in the cache.class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intAmount of cache space used so farprotected static final intDefault amount of space in the cacheprotected LRUCache.LRUCacheEntry<K, V> Start of queue (most recently used entry)protected LRUCache.LRUCacheEntry<K, V> End of queue (least recently used entry)protected Hashtable<K, LRUCache.LRUCacheEntry<K, V>> Hash table for fast random access to cache entriesprotected intMaximum space allowed in cacheprotected intCounter for handing out sequential timestamps -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()Returns a new cache containing the same contents.doublevoidflush()Flushes all entries from the cache.voidFlushes the given entry from the cache.Answers the value in the cache at the given key.intReturns the amount of space that is current used in the cache.intReturns the timestamps of the most recently used element in the cache.Returns the lest recently used element in the cache, can returnnullintReturns the timestamps of the least recently used element in the cache.intReturns the maximum amount of space available in the cache.keys()Returns an Enumeration of the keys currently in the cache.Returns an enumeration that iterates over all the keys and values currently in the cache.protected booleanmakeSpace(int space) Ensures there is the specified amount of free space in the receiver, by removing old entries if necessary.newInstance(int size) Returns a new LRUCache instanceAnswers the value in the cache at the given key.protected voidprivateAdd(K key, V value, int space) Adds an entry for the given key/value/space.protected voidprivateAddEntry(LRUCache.LRUCacheEntry<K, V> entry, boolean shuffle) Adds the given entry from the receiver.protected voidprivateRemoveEntry(LRUCache.LRUCacheEntry<K, V> entry, boolean shuffle) Removes the entry from the entry queue.Sets the value in the cache at the given key.Removes and returns the value in the cache for the given key.voidsetSpaceLimit(int limit) Sets the maximum amount of space that the cache can storeprotected intReturns the space taken by the given value.toString()Returns a String that represents the value of this object.protected StringReturns a String that represents the contents of this object.toStringFillingRation(String cacheName) protected voidupdateTimestamp(LRUCache.LRUCacheEntry<K, V> entry) Updates the timestamp for the given entry, ensuring that the queue is kept in correct order.
-
Field Details
-
currentSpace
protected int currentSpaceAmount of cache space used so far -
spaceLimit
protected int spaceLimitMaximum space allowed in cache -
timestampCounter
protected int timestampCounterCounter for handing out sequential timestamps -
entryTable
Hash table for fast random access to cache entries -
entryQueue
Start of queue (most recently used entry) -
entryQueueTail
End of queue (least recently used entry) -
DEFAULT_SPACELIMIT
protected static final int DEFAULT_SPACELIMITDefault amount of space in the cache- See Also:
-
-
Constructor Details
-
LRUCache
public LRUCache()Creates a new cache. Size of cache is defined byDEFAULT_SPACELIMIT. -
LRUCache
public LRUCache(int size) Creates a new cache.- Parameters:
size- Size of Cache
-
-
Method Details
-
clone
-
fillingRatio
public double fillingRatio() -
flush
public void flush()Flushes all entries from the cache. -
flush
Flushes the given entry from the cache. Does nothing if entry does not exist in cache.- Parameters:
key- Key of object to flush
-
getKey
-
get
-
getCurrentSpace
public int getCurrentSpace()Returns the amount of space that is current used in the cache. -
getNewestTimestampCounter
public int getNewestTimestampCounter()Returns the timestamps of the most recently used element in the cache. -
getOldestTimestampCounter
public int getOldestTimestampCounter()Returns the timestamps of the least recently used element in the cache. -
getOldestElement
Returns the lest recently used element in the cache, can returnnull -
getSpaceLimit
public int getSpaceLimit()Returns the maximum amount of space available in the cache. -
keys
Returns an Enumeration of the keys currently in the cache. -
keysAndValues
Returns an enumeration that iterates over all the keys and values currently in the cache. -
makeSpace
protected boolean makeSpace(int space) Ensures there is the specified amount of free space in the receiver, by removing old entries if necessary. Returns true if the requested space was made available, false otherwise.- Parameters:
space- Amount of space to free up
-
newInstance
-
peek
-
privateAdd
-
privateAddEntry
Adds the given entry from the receiver.- Parameters:
shuffle- Indicates whether we are just shuffling the queue (in which case, the entry table is not modified).
-
privateRemoveEntry
Removes the entry from the entry queue.- Parameters:
shuffle- indicates whether we are just shuffling the queue (in which case, the entry table is not modified).
-
put
-
removeKey
-
setSpaceLimit
public void setSpaceLimit(int limit) Sets the maximum amount of space that the cache can store- Parameters:
limit- Number of units of cache space
-
spaceFor
Returns the space taken by the given value. -
toString
-
toStringContents
Returns a String that represents the contents of this object. This method is for debugging purposes only. -
toStringFillingRation
-
updateTimestamp
Updates the timestamp for the given entry, ensuring that the queue is kept in correct order. The entry must exist
-