javatools.util
Class Cache

java.lang.Object
  |
  +--javatools.util.Cache

public class Cache
extends java.lang.Object

General purpose cache class. Acts basically like a weak reference dictionary except that it also returns null when asked to find an item whose use-by date has expired. It works with two main data structures: A Dictionary which provides fast access into the cache, and a Queue which keeps track of the least recently used items in the cache so that old items can be removed and limit the cache to a bounded size.


Nested Class Summary
(package private)  class Cache.CacheItem
          This is the class we store in our internal data structures.
 
Constructor Summary
Cache()
           
Cache(int size)
          Constructor which takes an initial guesstimate of how many items we expect to store in it
 
Method Summary
 void expire(java.lang.Object key)
          Expire the item in the cache with the given key.
 java.lang.Object get(java.lang.Object key)
          Get an object from the cache.
 void put(java.lang.Object key, java.lang.Object item)
          Put a key value pair into the cache.
 void setCacheMaxAgeSeconds(int s)
          Change the maximum age of items in the cache.
 void setCacheMaxObj(int n)
          Set the maximum number of objects to cache.
 void setNoCacheMaxAge()
          Turn off cache expiry based on age
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache()

Cache

public Cache(int size)
Constructor which takes an initial guesstimate of how many items we expect to store in it

Parameters:
size - Description of Parameter
Method Detail

setCacheMaxObj

public void setCacheMaxObj(int n)
Set the maximum number of objects to cache.

Parameters:
n - The new cacheMaxObj value

setCacheMaxAgeSeconds

public void setCacheMaxAgeSeconds(int s)
Change the maximum age of items in the cache. Can be changed at any time.

Parameters:
s - The new cacheMaxAgeSeconds value

setNoCacheMaxAge

public void setNoCacheMaxAge()
Turn off cache expiry based on age


get

public java.lang.Object get(java.lang.Object key)
Get an object from the cache. Returns null if the object is not in the cache.

Parameters:
key - Description of Parameter
Returns:
Description of the Returned Value

put

public void put(java.lang.Object key,
                java.lang.Object item)
Put a key value pair into the cache.

Parameters:
key - Description of Parameter
item - Description of Parameter

expire

public void expire(java.lang.Object key)
Expire the item in the cache with the given key.

Parameters:
key - Description of Parameter