EAF 7.4 Implementation

com.lutris.util
Class LRUCache

java.lang.Object
  extended by com.lutris.util.LRUCache

public class LRUCache
extends java.lang.Object

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.

Version:
$Revision: 1.2 $
Author:
Andy John

Constructor Summary
LRUCache(int maxSize)
          Create a new, empty cache.
 
Method Summary
 void add(java.lang.Object key, java.lang.Object item)
          Add an object to the cache, with a default size of 1.
 void add(java.lang.Object key, java.lang.Object item, int size)
          Add an object to the cache.
 void clear()
          Delete all the items from the cache.
 boolean containsKey(java.lang.Object key)
          Does the cache contain the given key?
 java.lang.Object get(java.lang.Object key)
          Fetch an item from the cache.
 int getMaxSize()
          Returns the maxmimum number of items allowed in the cache.
 int getSize()
          Returns the total size of the items in the cache.
 java.lang.Object remove(java.lang.Object key)
          Remove an item from the cache.
 java.lang.String toString()
          Returns a string describing the contents of the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache(int maxSize)
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 the number of items allowed in the cache. When this limit is reached, the "oldest" items are deleted to make 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.
Method Detail

getSize

public int getSize()
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.

Returns:
The sum of the sizes of the items in the cache.

getMaxSize

public int getMaxSize()
Returns the maxmimum number of items allowed in the cache.

Returns:
The maximum number of items allowed in the cache.

add

public void add(java.lang.Object key,
                java.lang.Object item)
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().
item - The item to add to the cache.

add

public void add(java.lang.Object key,
                java.lang.Object item,
                int size)
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().
item - The item to add to the cache.
size - How large is the object? This counts against the maximim size specified when the cache was created.

get

public java.lang.Object get(java.lang.Object key)
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.
Returns:
The item, or null if not found.

remove

public java.lang.Object remove(java.lang.Object key)
Remove an item from the cache.

Parameters:
key - The key used to refer to the object when add() was called.
Returns:
The item that was removed, or null if not found.

containsKey

public boolean containsKey(java.lang.Object key)
Does the cache contain the given key?

Parameters:
key - The key used to refer to the object when add() was called.
Returns:
true if the key was found.

clear

public void clear()
Delete all the items from the cache.


toString

public java.lang.String toString()
Returns a string describing the contents of the cache.

Overrides:
toString in class java.lang.Object

EAF 7.4 Implementation