| java.lang.Object com.lutris.util.LRUCache
LRUCache | public class LRUCache (Code) | | This class implements a fixed size Least-Recently-Used cache.
The cache has a maximum size specified when it is created. When
an item is added to the cache, if the cache is already at the
maximum size the least recently used item is deleted, then the
new item is added.
The only two methods that count as "using" the object (for the
least-recently-used algorithm) are add() and
get() .
The items cached are refered to by keys, just like a Hashtable.
author: Andy John version: $Revision: 1.3 $ |
Constructor Summary | |
public | LRUCache(int maxSize) Create a new, empty cache. |
Method Summary | |
public synchronized void | add(Object key, Object item) Add an object to the cache, with a default size of 1. | public synchronized void | add(Object key, Object item, int size) Add an object to the cache. | public synchronized void | clear() Delete all the items from the cache. | public synchronized boolean | containsKey(Object key) | public synchronized Object | get(Object key) Fetch an item from the cache. | public synchronized int | getMaxSize() Returns the maxmimum number of items allowed in the cache. | public synchronized int | getSize() Returns the total size of the items in the cache. | public synchronized Object | remove(Object key) Remove an item from the cache. | public synchronized String | toString() Returns a string describing the contents of the cache. |
LRUCache | public LRUCache(int maxSize)(Code) | | Create a new, empty cache.
Parameters: maxSize - The maxmimum size of the items allowed to be in the cache.Since the size of an item defaults to 1, this is normally thenumber of items allowed in the cache.When this limit is reached, the "oldest" items are deleted tomake room for the new item, so the total size of the cache(the sum of the sizes of all the items it contains)does not exceed the specified limit. |
add | public synchronized void add(Object key, Object item)(Code) | | Add an object to the cache, with a default size of 1.
If the cache is full, the oldest items will be deleted to make room.
The item becomes the "newest" item.
Parameters: key - The key used to refer to the object when calling get() . Parameters: item - The item to add to the cache. |
add | public synchronized void add(Object key, Object item, int size)(Code) | | Add an object to the cache.
If the cache is full, the oldest items will be deleted to make room.
The item becomes the "newest" item.
Parameters: key - The key used to refer to the object when calling get() . Parameters: item - The item to add to the cache. Parameters: size - How large is the object? This counts against the maximim sizespecified when the cache was created. |
clear | public synchronized void clear()(Code) | | Delete all the items from the cache.
|
containsKey | public synchronized boolean containsKey(Object key)(Code) | | Does the cache contain the given key?
Parameters: key - The key used to refer to the object when add() was called.true if the key was found. |
get | public synchronized Object get(Object key)(Code) | | Fetch an item from the cache. Returns null if not found.
The item becomes the "newest" item.
Parameters: key - The key used to refer to the object when add() was called.The item, or null if not found. |
getMaxSize | public synchronized int getMaxSize()(Code) | | Returns the maxmimum number of items allowed in the cache.
The maximum number of items allowed in the cache. |
getSize | public synchronized int getSize()(Code) | | Returns the total size of the items in the cache.
If all the items were added with the default size of 1,
this is the number of items in the cache.
The sum of the sizes of the items in the cache. |
remove | public synchronized Object remove(Object key)(Code) | | Remove an item from the cache.
Parameters: key - The key used to refer to the object when add() was called.The item that was removed, or null if not found. |
toString | public synchronized String toString()(Code) | | Returns a string describing the contents of the cache.
|
|
|