org.cdlib.xtf.cache
Class GeneratingCache<K,V>

Object
  extended by Cache<K,V>
      extended by GeneratingCache<K,V>
Direct Known Subclasses:
Authenticator.IpListCache, ConfigCache, ImageCache, StylesheetCache

public abstract class GeneratingCache<K,V>
extends Cache<K,V>

A cache that generates an entry if one isn't found. The generate() method must be supplied by the derived class.


Nested Class Summary
 
Nested classes/interfaces inherited from class Cache
Cache.ListEntry, Cache.NullIterator
 
Field Summary
private  Cache.ListEntry curEntry
          The entry being generated
 
Fields inherited from class Cache
ageList, keyMap
 
Constructor Summary
GeneratingCache(int maxEntries, int maxTime)
          Constructor - sets up the parameters of the cache.
 
Method Summary
 void addDependency(Dependency d)
          Can be called by the generate() method to add a dependency to the key being generated.
 V find(K key)
          Check the cache for an entry matching the given key.
protected abstract  V generate(K key)
          Called when find() fails to locate an entry for the given key.
 
Methods inherited from class Cache
cleanup, clear, dependenciesValid, getDependencies, has, lastSet, logAction, remove, size
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

curEntry

private Cache.ListEntry curEntry
The entry being generated

Constructor Detail

GeneratingCache

public GeneratingCache(int maxEntries,
                       int maxTime)
Constructor - sets up the parameters of the cache.

Parameters:
maxEntries - Maximum # of entries. Beyond this, older ones will be removed. Zero means no limit.
maxTime - Time (in seconds) an entry can stay in the cache without being used. Entries older than this will be removed. Zero means no limit.
Method Detail

find

public V find(K key)
       throws Exception
Check the cache for an entry matching the given key. If not found, one is generated.

Parameters:
key - The key to look up
Returns:
Value corresponding to that key. Never null.
Throws:
Exception

addDependency

public void addDependency(Dependency d)
Can be called by the generate() method to add a dependency to the key being generated.

Parameters:
d - The dependency to add

generate

protected abstract V generate(K key)
                       throws Exception
Called when find() fails to locate an entry for the given key. This method must be supplied by the derived class, and must produce a value for the key, or throw an exception if it can't.

Parameters:
key - The key to generate a value for.
Returns:
The value for that key
Throws:
Exception - If a value cannot be generated for any reason.