An AbstractCacheAdministrator defines an abstract cache administrator, implementing all
the basic operations related to the configuration of a cache, including assigning
any configured event handlers to cache objects.
Extend this class to implement a custom cache administrator.
| Field Summary |
|---|
| public static final String | CACHE_MEMORY_KEY | A boolean cache configuration property that indicates whether the cache
should cache objects in memory. Set this property to false
to disable in-memory caching. |
| public static final String | CACHE_CAPACITY_KEY | An integer cache configuration property that specifies the maximum number
of objects to hold in the cache. Setting this to a negative value will
disable the capacity functionality - there will be no limit to the number
of objects that are held in cache. |
| public static final String | CACHE_ALGORITHM_KEY | A String cache configuration property that specifies the classname of
an alternate caching algorithm. This class must extend
com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache
By default caches will use com.opensymphony.oscache.base.algorithm.LRUCache as
the default algorithm if the cache capacity is set to a postive value, or
com.opensymphony.oscache.base.algorithm.UnlimitedCache if the
capacity is negative (ie, disabled). |
| public static final String | CACHE_DISK_UNLIMITED_KEY | A boolean cache configuration property that indicates whether the persistent
cache should be unlimited in size, or should be restricted to the same size
as the in-memory cache. Set this property to true to allow the
persistent cache to grow without bound. |
| public static final String | CACHE_BLOCKING_KEY | The configuration key that specifies whether we should block waiting for new
content to be generated, or just serve the old content instead. The default
behaviour is to serve the old content since that provides the best performance
(at the cost of serving slightly stale data). |
| public static final String | PERSISTENCE_CLASS_KEY | A String cache configuration property that specifies the classname that will
be used to provide cache persistence. This class must extend PersistenceListener . |
| public static final String | CACHE_PERSISTENCE_OVERFLOW_KEY | A String cache configuration property that specifies if the cache persistence
will only be used in overflow mode, that is, when the memory cache capacity has been reached. |
| public static final String | CACHE_ENTRY_EVENT_LISTENERS_KEY | A String cache configuration property that holds a comma-delimited list of
classnames. These classes specify the event handlers that are to be applied
to the cache. |
| protected Config | config | |
| protected EventListenerList | listenerList | Holds a list of all the registered event listeners. Event listeners are specified
using the #CACHE_ENTRY_EVENT_LISTENERS_KEY configuration key. |
| protected String | algorithmClass | The algorithm class being used, as specified by the #CACHE_ALGORITHM_KEY
configuration property. |
| protected int | cacheCapacity | The cache capacity (number of entries), as specified by the #CACHE_CAPACITY_KEY
configuration property. |
| Method from com.opensymphony.oscache.base.AbstractCacheAdministrator Summary: |
|---|
|
configureStandardListeners, finalizeListeners, getCacheEventListeners, getProperty, isBlocking, isMemoryCaching, isOverflowPersistence, isUnlimitedDiskCache, setAlgorithmClass, setCacheCapacity, setOverflowPersistence, setPersistenceListener |
| Method from com.opensymphony.oscache.base.AbstractCacheAdministrator Detail: |
protected Cache configureStandardListeners(Cache cache) {
if (config.getProperty(PERSISTENCE_CLASS_KEY) != null) {
cache = setPersistenceListener(cache);
}
if (config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY) != null) {
// Grab all the specified listeners and add them to the cache's
// listener list. Note that listeners that implement more than
// one of the event interfaces will be added multiple times.
CacheEventListener[] listeners = getCacheEventListeners();
for (int i = 0; i < listeners.length; i++) {
// Pass through the configuration to those listeners that require it
if (listeners[i] instanceof LifecycleAware) {
try {
((LifecycleAware) listeners[i]).initialize(cache, config);
} catch (InitializationException e) {
log.error("Could not initialize listener '" + listeners[i].getClass().getName() + "'. Listener ignored.", e);
continue;
}
}
if (listeners[i] instanceof CacheEntryEventListener) {
cache.addCacheEventListener(listeners[i]);
} else if (listeners[i] instanceof CacheMapAccessEventListener) {
cache.addCacheEventListener(listeners[i]);
}
}
}
return cache;
}
|
protected void finalizeListeners(Cache cache) {
// It's possible for cache to be null if getCache() was never called (CACHE-63)
if (cache == null) {
return;
}
Object[] listeners = cache.listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i + 1] instanceof LifecycleAware) {
try {
((LifecycleAware) listeners[i + 1]).finialize();
} catch (FinalizationException e) {
log.error("Listener could not be finalized", e);
}
}
}
}
Finalizes all the listeners that are associated with the given cache object.
Any FinalizationExceptions that are thrown by the listeners will
be caught and logged. |
protected CacheEventListener[] getCacheEventListeners() {
List classes = StringUtil.split(config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY), ',");
CacheEventListener[] listeners = new CacheEventListener[classes.size()];
for (int i = 0; i < classes.size(); i++) {
String className = (String) classes.get(i);
try {
Class clazz = Class.forName(className);
if (!CacheEventListener.class.isAssignableFrom(clazz)) {
log.error("Specified listener class '" + className + "' does not implement CacheEventListener. Ignoring this listener.");
} else {
listeners[i] = (CacheEventListener) clazz.newInstance();
}
} catch (ClassNotFoundException e) {
log.error("CacheEventListener class '" + className + "' not found. Ignoring this listener.", e);
} catch (InstantiationException e) {
log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this listener.", e);
} catch (IllegalAccessException e) {
log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not public. Ignoring this listener.", e);
}
}
return listeners;
}
Retrieves an array containing instances all of the CacheEventListener
classes that are specified in the OSCache configuration file. |
public String getProperty(String key) {
return config.getProperty(key);
}
Retrieves the value of one of the configuration properties. |
public boolean isBlocking() {
return blocking;
}
Indicates whether the cache will block waiting for new content to
be built, or serve stale content instead of waiting. Regardless of this
setting, the cache will always block if new content is being
created, ie, there's no stale content in the cache that can be served. |
public boolean isMemoryCaching() {
return memoryCaching;
}
Whether entries are cached in memory or not.
Default is true.
Set by the cache.memory property. |
public boolean isOverflowPersistence() {
return this.overflowPersistence;
}
Check if we use overflowPersistence |
public boolean isUnlimitedDiskCache() {
return unlimitedDiskCache;
}
Indicates whether the unlimited disk cache is enabled or not. |
public void setAlgorithmClass(String newAlgorithmClass) {
algorithmClass = newAlgorithmClass;
}
Sets the algorithm to use for the cache. |
protected void setCacheCapacity(int newCacheCapacity) {
cacheCapacity = newCacheCapacity;
}
|
public void setOverflowPersistence(boolean overflowPersistence) {
this.overflowPersistence = overflowPersistence;
}
Sets the overflowPersistence flag |
protected Cache setPersistenceListener(Cache cache) {
String persistenceClassname = config.getProperty(PERSISTENCE_CLASS_KEY);
try {
Class clazz = Class.forName(persistenceClassname);
PersistenceListener persistenceListener = (PersistenceListener) clazz.newInstance();
cache.setPersistenceListener(persistenceListener.configure(config));
} catch (ClassNotFoundException e) {
log.error("PersistenceListener class '" + persistenceClassname + "' not found. Check your configuration.", e);
} catch (Exception e) {
log.error("Error instantiating class '" + persistenceClassname + "'", e);
}
return cache;
}
|