org.ceryle.util
Class Cache

java.lang.Object
  extended by java.util.Dictionary<K,V>
      extended by java.util.Hashtable
          extended by org.ceryle.util.Cache
All Implemented Interfaces:
Serializable, Cloneable, Map

public class Cache
extends Hashtable

Extends Hashtable as an Object cache.

To Do

Provide a cache size limit using total storage size and delete cache objects past that threshold. Currently this is done on count only.

Version:
$Id: Cache.java,v 3.1 2007-06-15 12:09:55 altheim Exp $
Author:
Murray Altheim
See Also:
Serialized Form

Field Summary
static int DEFAULT_SIZE
          The value of the default cache size.
static int hysteresis
          The value of size-trimming hysteresis (the amount over the limit permitted prior to trimming, used to reduce the cost of trimming).
 
Constructor Summary
Cache()
          Constructor for an Cache using the default cache size (of 50) and automatically checking the limit.
Cache(int limit, boolean autocheck)
          Constructor for an Cache with a cache size limit and the boolean controlling whether the limit check is automatic or manual.
 
Method Summary
protected  void addToCache(String key, Object o)
          Adds the Object o to the cache using the String key as the key.
 Object checkCacheFor(String key)
          Checks the cache for an Object corresponding to key, returning null if it is not in the cache, or if caching is inactive.
 void checkLimit()
          Checks the current cache size against the limit and removes objects as necessary to reach the limit.
 void clear()
          Clears the document cache.
 Object get(Object key)
          Throws an exception.
 Iterator getAll()
          Returns an Iterator over the Objects in the cache.
 Object put(Object key, Object value)
          Throws an exception.
 void putAll(Map map)
          Throws an exception.
 Object remove(Object key)
          Throws an exception.
 boolean removeFromCache(Object o)
          Removes the Object o from the document cache, returning true if it was removed.
 boolean removeFromCache(String key)
          Removes the document specified by the String key from the cache, returning true if it was removed.
 Collection values()
          Throws an exception.
 
Methods inherited from class java.util.Hashtable
clone, contains, containsKey, containsValue, elements, entrySet, equals, hashCode, isEmpty, keys, keySet, rehash, size, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
The value of the default cache size. Value is 50.

See Also:
Constant Field Values

hysteresis

public static int hysteresis
The value of size-trimming hysteresis (the amount over the limit permitted prior to trimming, used to reduce the cost of trimming). Default value is 10.

Constructor Detail

Cache

public Cache()
Constructor for an Cache using the default cache size (of 50) and automatically checking the limit.


Cache

public Cache(int limit,
             boolean autocheck)
Constructor for an Cache with a cache size limit and the boolean controlling whether the limit check is automatic or manual. The minimum acceptable value for limit is 10.

Method Detail

addToCache

protected void addToCache(String key,
                          Object o)
Adds the Object o to the cache using the String key as the key. Note: If the key is already in use, this replaces the existing cache entry with the new Object. If caching is inactive or either parameter is null this method does nothing (rather than throw an exception).


checkCacheFor

public Object checkCacheFor(String key)
Checks the cache for an Object corresponding to key, returning null if it is not in the cache, or if caching is inactive.


removeFromCache

public boolean removeFromCache(String key)
Removes the document specified by the String key from the cache, returning true if it was removed.


removeFromCache

public boolean removeFromCache(Object o)
Removes the Object o from the document cache, returning true if it was removed. This is somewhat expensive as we need to iterate through the entire Map.Entry set to locate the key so it can be removed from the keyset and queue.


getAll

public Iterator getAll()
Returns an Iterator over the Objects in the cache.


clear

public void clear()
Clears the document cache.

Specified by:
clear in interface Map
Overrides:
clear in class Hashtable

checkLimit

public void checkLimit()
Checks the current cache size against the limit and removes objects as necessary to reach the limit. To provide some hysteresis, the size is permitted to exceed the limit by the value of hysteresis prior to trimming.


values

public final Collection values()
Throws an exception. Use getAll() instead.

Specified by:
values in interface Map
Overrides:
values in class Hashtable

remove

public final Object remove(Object key)
Throws an exception. Use removeFromCache(String) instead.

Specified by:
remove in interface Map
Overrides:
remove in class Hashtable

get

public final Object get(Object key)
Throws an exception. Use checkCacheFor(String) instead.

Specified by:
get in interface Map
Overrides:
get in class Hashtable

put

public final Object put(Object key,
                        Object value)
Throws an exception. Use addToCache(String,Object) instead.

Specified by:
put in interface Map
Overrides:
put in class Hashtable

putAll

public final void putAll(Map map)
Throws an exception. Use addToCache(String,Object) instead.

Specified by:
putAll in interface Map
Overrides:
putAll in class Hashtable


The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.