net.sf.ashkay
Class ObjectCache

java.lang.Object
  extended bynet.sf.ashkay.ObjectCache

public class ObjectCache
extends java.lang.Object

ObjectCache is the default implementation of an object caching mechanism.

ObjectCaching makes use of a Factory for the creation of objects that are not cached. The user of an ObjectCache provides an implementation of ObjectFactory to the cache. If the cache does not find the object asked for, it uses the factory to create the object, cache it, and return it.

Looking up an object from cache requires a key. The key is a unique identifier. The key could be a database record id, a URI, or any other id that is guaranteed to be unique. The key is also used by the factory to create the object. The factory ought to be able to construct an object given only a key. This key is then used as the key in the cache.

Gets may also be done with a data parameter. The data provides construction information to the factory about the object being created. The cache does not use the data directly but, if the object is not cached, will pass the data to the factory. Common use of the data would be if the user of the cache has an "un-objectified" data source (e.g. XML document) from some other source that would save the factory the lookup call. So, if the user has the key and the data and wants the object form of the data, it simply asks the cache passing in the key and data.

The ObjectCache uses CachingStrategies to detirmine a strategy for caching objects that is stores. These strategies may affect caching in any number of ways. You might want to use a cache that does not prevent garbage collection of cached objects, only getting benefit of the cache as memory allows. A client may want to place timeouts for objects to force them to be re-loaded after a period of time. Any number of caching strategies may be used, but be careful not to use two caching strategies that work in opposition. That is a check left to the user at this point.

Author:
Dave Brown

Constructor Summary
ObjectCache()
           
ObjectCache(java.util.List theStrategies)
          Constructs an ObjectCache without a factory using the strategies indicated.
ObjectCache(ObjectFactory aFactory)
          Constructs an ObjectCache from a factory using the default strategy of NONE.
ObjectCache(ObjectFactory aFactory, java.util.List theStrategies)
          Constructs an ObjectCache from a factory and strategy.
 
Method Summary
 ObjectCache addStrategy(CachingStrategy aStrategy)
          Adds a caching strategy to this cache
 void clear()
          Clears the cache
 java.lang.Object evict(java.lang.Object key)
          Forcefully evicts an object/key from the cache.
 java.lang.Object get(java.lang.Object key)
          Finds an object in the cache and returns it.
 java.lang.Object get(java.lang.Object key, java.lang.Object data)
          Finds an object in the cache and returns it.
 void put(java.lang.Object key, java.lang.Object value)
           
 ObjectCache removeStrategy(CachingStrategy aStrategy)
          Removes a strategy from this cache
 int size()
          Returns the size of the cache
 boolean usesStrategy(CachingStrategy aStrategy)
          Checks if this cache uses the specified caching strategy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectCache

public ObjectCache()

ObjectCache

public ObjectCache(ObjectFactory aFactory)
Constructs an ObjectCache from a factory using the default strategy of NONE.

Parameters:
aFactory - - the factory for the cache to use

ObjectCache

public ObjectCache(java.util.List theStrategies)
Constructs an ObjectCache without a factory using the strategies indicated.

Parameters:
theStrategies -

ObjectCache

public ObjectCache(ObjectFactory aFactory,
                   java.util.List theStrategies)
Constructs an ObjectCache from a factory and strategy.

Parameters:
aFactory - - the factory for the cache to use
theStrategies - - the caching strategies for this cache
Method Detail

addStrategy

public ObjectCache addStrategy(CachingStrategy aStrategy)
Adds a caching strategy to this cache

Parameters:
aStrategy - - the strategy to add

removeStrategy

public ObjectCache removeStrategy(CachingStrategy aStrategy)
Removes a strategy from this cache

Parameters:
aStrategy - - the strategy to remove

usesStrategy

public boolean usesStrategy(CachingStrategy aStrategy)
Checks if this cache uses the specified caching strategy

Parameters:
aStrategy - - the strategy to check

get

public java.lang.Object get(java.lang.Object key)
                     throws CreationException
Finds an object in the cache and returns it. If the cache contains no object for the specified key, the cache attempts to construct it using its factory.

Parameters:
key - - the key to lookup
Throws:
CreationException - on any error during creation
See Also:
get(java.lang.Object, java.lang.Object)

evict

public java.lang.Object evict(java.lang.Object key)
Forcefully evicts an object/key from the cache. This will only evict the object if it has been created (i.e. not being created by a factory).

Parameters:
key - to the object to evict
Returns:
the object evicted or null if none

get

public java.lang.Object get(java.lang.Object key,
                            java.lang.Object data)
                     throws CreationException
Finds an object in the cache and returns it. If the cache contains no object for the specified key, the cache attempts to construct it using its factory and the passed in data. The cache also uses the caching strategies to validate the entry in the cache.

Parameters:
key - - the key to lookup
data - - the data to aid construction
Throws:
CreationException - on any error during creation

put

public void put(java.lang.Object key,
                java.lang.Object value)

clear

public void clear()
Clears the cache


size

public int size()
Returns the size of the cache

Returns:
int size of the cache


Copyright © 2004 . All Rights Reserved.