Package org.apache.fulcrum.cache.impl
Class DefaultGlobalCacheService
- java.lang.Object
-
- org.apache.avalon.framework.logger.AbstractLogEnabled
-
- org.apache.fulcrum.cache.impl.DefaultGlobalCacheService
-
- All Implemented Interfaces:
Runnable
,org.apache.avalon.framework.activity.Disposable
,org.apache.avalon.framework.activity.Initializable
,org.apache.avalon.framework.configuration.Configurable
,org.apache.avalon.framework.logger.LogEnabled
,org.apache.avalon.framework.thread.ThreadSafe
,GlobalCacheService
public class DefaultGlobalCacheService extends org.apache.avalon.framework.logger.AbstractLogEnabled implements GlobalCacheService, Runnable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.thread.ThreadSafe
This Service functions as a Global Cache. A global cache is a good place to store items that you may need to access often but don't necessarily need (or want) to fetch from the database everytime. A good example would be a look up table of States that you store in a database and use throughout your application. Since information about States doesn't change very often, you could store this information in the Global Cache and decrease the overhead of hitting the database everytime you need State information.- Version:
- $Id$
- Author:
- Dave Bryson, Jon S. Stevens, John Thorhauer, Henning P. Schmiedehausen, Eric Pugh, Peter CourefreshableCachedObjectux
-
-
Field Summary
Fields Modifier and Type Field Description protected ConcurrentHashMap<String,CachedObject<?>>
cache
The cache.static String
CACHE_CHECK_FREQUENCY
The property for the Cache check frequencystatic long
DEFAULT_CACHE_CHECK_FREQUENCY
Cache check frequency in Millis (1000 Millis = 1 second).static int
DEFAULT_INITIAL_CACHE_SIZE
Initial size of hash table Value must be > 0.static String
INITIAL_CACHE_SIZE
The property for the InitalCacheSize-
Fields inherited from interface org.apache.fulcrum.cache.GlobalCacheService
ROLE
-
-
Constructor Summary
Constructors Constructor Description DefaultGlobalCacheService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> void
addObject(String objectId, CachedObject<T> object)
Adds an object to the cache.void
clearCache()
Iterate through the cache and remove or refresh stale objects.void
configure(org.apache.avalon.framework.configuration.Configuration conf)
Avalon component lifecycle methodvoid
dispose()
Avalon component lifecycle methodvoid
flushCache()
Flush the cache of all objects.long
getCacheCheckFrequency()
Get the Cache Check Frequency in millisecondsList<CachedObject<?>>
getCachedObjects()
Returns a copy of the non-expired CachedObjects in the cache as a list.int
getCacheSize()
Returns the current size of the cache.List<String>
getKeys()
Returns a copy of keys to objects in the cache as a list.int
getNumberOfObjects()
Returns the number of objects currently stored in the cache<T> CachedObject<T>
getObject(String objectId)
Returns an item from the cache. /** Returns an item from the cache.void
initialize()
Avalon component lifecycle methodvoid
removeObject(String objectId)
Removes an object from the cache.void
run()
Circle through the cache and remove stale objects.
-
-
-
Field Detail
-
DEFAULT_INITIAL_CACHE_SIZE
public static final int DEFAULT_INITIAL_CACHE_SIZE
Initial size of hash table Value must be > 0. Default = 20- See Also:
- Constant Field Values
-
INITIAL_CACHE_SIZE
public static final String INITIAL_CACHE_SIZE
The property for the InitalCacheSize- See Also:
- Constant Field Values
-
CACHE_CHECK_FREQUENCY
public static final String CACHE_CHECK_FREQUENCY
The property for the Cache check frequency- See Also:
- Constant Field Values
-
DEFAULT_CACHE_CHECK_FREQUENCY
public static final long DEFAULT_CACHE_CHECK_FREQUENCY
Cache check frequency in Millis (1000 Millis = 1 second). Value must be > 0. Default = 5 seconds- See Also:
- Constant Field Values
-
cache
protected transient ConcurrentHashMap<String,CachedObject<?>> cache
The cache.
-
-
Constructor Detail
-
DefaultGlobalCacheService
public DefaultGlobalCacheService()
-
-
Method Detail
-
getCacheCheckFrequency
public long getCacheCheckFrequency()
Get the Cache Check Frequency in milliseconds- Returns:
- the time between two cache check runs in milliseconds
-
getObject
public <T> CachedObject<T> getObject(String objectId) throws ObjectExpiredException
Returns an item from the cache. /** Returns an item from the cache. RefreshableCachedObject will be refreshed if it is expired and not untouched.- Specified by:
getObject
in interfaceGlobalCacheService
- Type Parameters:
T
- type of object to return- Parameters:
objectId
- The key of the stored object.- Returns:
- The object from the cache.
- Throws:
ObjectExpiredException
- when either the object is not in the cache or it has expired.
-
addObject
public <T> void addObject(String objectId, CachedObject<T> object)
Adds an object to the cache.- Specified by:
addObject
in interfaceGlobalCacheService
- Type Parameters:
T
- type of object to add- Parameters:
objectId
- The key to store the object by.object
- The object to cache.
-
removeObject
public void removeObject(String objectId)
Removes an object from the cache.- Specified by:
removeObject
in interfaceGlobalCacheService
- Parameters:
objectId
- The String id for the object.
-
getKeys
public List<String> getKeys()
Returns a copy of keys to objects in the cache as a list. Note that keys to expired objects are not returned.- Specified by:
getKeys
in interfaceGlobalCacheService
- Returns:
- A List of
String
's representing the keys to objects in the cache.
-
getCachedObjects
public List<CachedObject<?>> getCachedObjects()
Returns a copy of the non-expired CachedObjects in the cache as a list.- Specified by:
getCachedObjects
in interfaceGlobalCacheService
- Returns:
- A List of
CachedObject
objects held in the cache
-
run
public void run()
Circle through the cache and remove stale objects. Frequency is determined by the cacheCheckFrequency property.
-
clearCache
public void clearCache()
Iterate through the cache and remove or refresh stale objects.
-
getNumberOfObjects
public int getNumberOfObjects()
Returns the number of objects currently stored in the cache- Specified by:
getNumberOfObjects
in interfaceGlobalCacheService
- Returns:
- int number of object in the cache
-
getCacheSize
public int getCacheSize() throws IOException
Returns the current size of the cache.- Specified by:
getCacheSize
in interfaceGlobalCacheService
- Returns:
- int representing current cache size in number of bytes
- Throws:
IOException
- if unable to return cache size
-
flushCache
public void flushCache()
Flush the cache of all objects.- Specified by:
flushCache
in interfaceGlobalCacheService
-
configure
public void configure(org.apache.avalon.framework.configuration.Configuration conf) throws org.apache.avalon.framework.configuration.ConfigurationException
Avalon component lifecycle method- Specified by:
configure
in interfaceorg.apache.avalon.framework.configuration.Configurable
- Throws:
org.apache.avalon.framework.configuration.ConfigurationException
-
initialize
public void initialize() throws Exception
Avalon component lifecycle method- Specified by:
initialize
in interfaceorg.apache.avalon.framework.activity.Initializable
- Throws:
Exception
-
dispose
public void dispose()
Avalon component lifecycle method- Specified by:
dispose
in interfaceorg.apache.avalon.framework.activity.Disposable
-
-