jrunx.util
Class LRUCache

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable
              |
              +--jrunx.util.FastHashtable
                    |
                    +--jrunx.util.LRUCache
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class LRUCache
extends FastHashtable

Caching datastructure that uses a Least Recently Used (LRU) algorithm. This cache has a max size associated with it that is used to determine when objects should be purged from the cache. Once the max size is reached, the least recently used element will be purged. This class is not guaranteed to be synchronized. If synchronization is necessary, then use LRUCache.synchronizedCache(new LRUCache(size)) to obtain a synchronized LRUCache.

Author:
Spike Washburn (spike@allaire.com)
See Also:
Serialized Form

Inner Class Summary
 class LRUCache.LRUEnumerator
           
 
Inner classes inherited from class jrunx.util.FastHashtable
FastHashtable.HashEnumerator
 
Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
LRUCache(int maxSize)
          Create a new LRU cache.
LRUCache(int maxSize, int purgeSize)
          Create a new LRU cache.
 
Method Summary
 java.util.Enumeration elements()
           
 java.lang.Object get(java.lang.Object key)
          Retrieve an object from the cache using the specified key.
 int getMaxSize()
          Returns the number of elements that this cache can hold.
 int getPurgeSize()
          Returns the number of LRU elements to purge when the max size is reached.
protected  void handleLRUElementPurged(java.lang.Object key, java.lang.Object value)
          Handler hook to signal subclasses that the LRU element has been purged from the cache.
 java.util.Enumeration keys()
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Insert an object into the cache.
 java.lang.Object remove(java.lang.Object key)
          Remove an object from the cache.
 int size()
          Returns the number of elements currently in the cache.
 
Methods inherited from class jrunx.util.FastHashtable
clear, containsElement, containsKey, toString
 
Methods inherited from class java.util.Hashtable
clone, contains, containsValue, entrySet, equals, hashCode, isEmpty, keySet, putAll, rehash, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache(int maxSize)
Create a new LRU cache.
Parameters:
maxSize - the maximum number of elements this cache can hold before purging LRU elements.

LRUCache

public LRUCache(int maxSize,
                int purgeSize)
Create a new LRU cache. This constructor takes a purgeSize parameter that is used to increase the number of LRU elements to when the cache exceeds its max size. Increasing the purge size can reduce the overhead of using the cache if the cache size is maxed out frequently.
Parameters:
maxSize - the maximum number of elements this cache can hold before purging LRU elements.
purgeSize - the number of LRU elements to purge once the max size is exceeded.
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Insert an object into the cache.
Overrides:
put in class FastHashtable

get

public java.lang.Object get(java.lang.Object key)
Retrieve an object from the cache using the specified key. Use of this method will make the retrieved object the most recently used object.
Overrides:
get in class FastHashtable

remove

public java.lang.Object remove(java.lang.Object key)
Remove an object from the cache.
Overrides:
remove in class FastHashtable

size

public int size()
Returns the number of elements currently in the cache.
Overrides:
size in class FastHashtable

getMaxSize

public int getMaxSize()
Returns the number of elements that this cache can hold. Once more than this number of elements is added to the cache, the least recently used element will automatically be removed.

getPurgeSize

public int getPurgeSize()
Returns the number of LRU elements to purge when the max size is reached.

handleLRUElementPurged

protected void handleLRUElementPurged(java.lang.Object key,
                                      java.lang.Object value)
Handler hook to signal subclasses that the LRU element has been purged from the cache. This method is triggered when the cache's max size has been exceeded and the LRU element must be removed from the cache. This method is invoked after the LRU element has been removed from the cache.
Parameters:
key - the insertion key that was originally used to add value to the cache.
value - the value element bound to the key.

elements

public java.util.Enumeration elements()
Overrides:
elements in class FastHashtable

keys

public java.util.Enumeration keys()
Overrides:
keys in class FastHashtable


Copyright � 2002 Macromedia Corporation. All Rights Reserved.