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
Modifier and TypeClassDescriptionstatic class
This type is used internally by the LRUCache to represent entries stored in the cache.class
-
Field Summary
Modifier and TypeFieldDescriptionprotected int
Amount of cache space used so farprotected static final int
Default 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 int
Maximum space allowed in cacheprotected int
Counter for handing out sequential timestamps -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionclone()
Returns a new cache containing the same contents.double
void
flush()
Flushes all entries from the cache.void
Flushes the given entry from the cache.Answers the value in the cache at the given key.int
Returns the amount of space that is current used in the cache.int
Returns the timestamps of the most recently used element in the cache.Returns the lest recently used element in the cache, can returnnull
int
Returns the timestamps of the least recently used element in the cache.int
Returns 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 boolean
makeSpace
(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 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.Sets the value in the cache at the given 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 storeprotected int
Returns the space taken by the given value.toString()
Returns a String that represents the value of this object.protected String
Returns a String that represents the contents of this object.toStringFillingRation
(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.
-
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
Returns a new cache containing the same contents. -
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
Answers the value in the cache at the given key. If the value is not in the cache, returns null- Parameters:
key
- Hash table key of object to retrieve- Returns:
- Retrieved object, or null if object does not exist
-
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
Returns a new LRUCache instance -
peek
Answers the value in the cache at the given key. If the value is not in the cache, returns null This function does not modify timestamps. -
privateAdd
Adds an entry for the given key/value/space. -
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
Sets the value in the cache at the given key. Returns the value.- Parameters:
key
- Key of object to add.value
- Value of object to add.- Returns:
- added value.
-
removeKey
Removes and returns the value in the cache for the given key. If the key is not in the cache, returns null.- Parameters:
key
- Key of object to remove from cache.- Returns:
- Value removed from cache.
-
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
Returns a String that represents the value of this object. This method is for debugging purposes only. -
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
-