com.compoundtheory.objectcache
Class CacheManager

java.lang.Object
  extended by com.compoundtheory.objectcache.CacheManager
All Implemented Interfaces:
ICacheManager, java.util.Observer

public class CacheManager
extends java.lang.Object
implements ICacheManager, java.util.Observer

This is a cache manager for a set of classes

Author:
Mark Mandel

Constructor Summary
CacheManager(CacheConfig config, edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor threadPool)
          Constructor, requires the config object
 
Method Summary
 void add(java.lang.String clazz, java.lang.String key, java.lang.ref.SoftReference softRef)
          Adds an object to to the cache
 void discard(java.lang.String clazz, java.lang.String key)
          Discard the object from the cache
 java.lang.Object get(java.lang.String clazz, java.lang.String key)
          Retrieve a CFC from the cache
 java.util.Set getCachedClasses()
          gets the set of classes that are stored in this cache
 int getCalculatedSize(java.lang.String clazz)
          Returns the calculated size (slow), by looping through a copy of the collection and checking if the soft references are cleared or not
 int getEstimatedSize(java.lang.String clazz)
          Returns the estimate size of the cache (fast), does not scan the cache for empty soft references
 boolean has(java.lang.String clazz, java.lang.String key)
          Whether the CFC has been cached
 java.lang.Object popReapedCFC(java.lang.ref.SoftReference softRef)
          remove a soft reference cleared object from the map so it can be fired for a discard queue
 void reap(java.lang.String clazz, java.lang.ref.SoftReference softRef)
          Reap off a cleared soft reference
 void update(java.util.Observable o, java.lang.Object arg)
          Push the cfc onto the reap map
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CacheManager

public CacheManager(CacheConfig config,
                    edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor threadPool)
Constructor, requires the config object

Parameters:
config - The config object that defines the caching settings for each class
Method Detail

has

public boolean has(java.lang.String clazz,
                   java.lang.String key)
Whether the CFC has been cached

Specified by:
has in interface ICacheManager
Parameters:
clazz - The class of CFC
key - The key the CFC is stored under
Returns:
If it exists in a cache

get

public java.lang.Object get(java.lang.String clazz,
                            java.lang.String key)
                     throws ObjectNotFoundException
Retrieve a CFC from the cache

Specified by:
get in interface ICacheManager
Parameters:
clazz - The class of the CFC
key - The key it was stored under
Returns:
The CFC itself
Throws:
ObjectNotFoundException - if the object doesn't exist in the cache

add

public void add(java.lang.String clazz,
                java.lang.String key,
                java.lang.ref.SoftReference softRef)
Adds an object to to the cache

Specified by:
add in interface ICacheManager
Parameters:
clazz - The class of the object
key - The key of the object
softRef - The soft reference containing the CFC

discard

public void discard(java.lang.String clazz,
                    java.lang.String key)
Discard the object from the cache

Specified by:
discard in interface ICacheManager
Parameters:
clazz - the class of the object
key - the key of the object to discard

reap

public void reap(java.lang.String clazz,
                 java.lang.ref.SoftReference softRef)
Reap off a cleared soft reference

Specified by:
reap in interface ICacheManager
Parameters:
clazz - the class to reap to
softRef - the soft reference to reap

getCachedClasses

public java.util.Set getCachedClasses()
gets the set of classes that are stored in this cache

Specified by:
getCachedClasses in interface ICacheManager
Returns:
Set of classnames that are stored in this cache.

getCalculatedSize

public int getCalculatedSize(java.lang.String clazz)
Returns the calculated size (slow), by looping through a copy of the collection and checking if the soft references are cleared or not

Parameters:
clazz - the class to get the size for
Returns:
the calculated cache size

getEstimatedSize

public int getEstimatedSize(java.lang.String clazz)
Returns the estimate size of the cache (fast), does not scan the cache for empty soft references

Parameters:
clazz - the class to get the size for
Returns:
the estimated cache size

update

public void update(java.util.Observable o,
                   java.lang.Object arg)
Push the cfc onto the reap map

Specified by:
update in interface java.util.Observer

popReapedCFC

public java.lang.Object popReapedCFC(java.lang.ref.SoftReference softRef)
remove a soft reference cleared object from the map so it can be fired for a discard queue

Specified by:
popReapedCFC in interface ICacheManager
Parameters:
softRef - the softreference to be cleared