org.apache.turbine.services.cache
Class TurbineGlobalCacheService

java.lang.Object
  extended byorg.apache.turbine.services.BaseInitable
      extended byorg.apache.turbine.services.BaseService
          extended byorg.apache.turbine.services.TurbineBaseService
              extended byorg.apache.turbine.services.cache.TurbineGlobalCacheService
All Implemented Interfaces:
GlobalCacheService, Initable, java.lang.Runnable, Service

public class TurbineGlobalCacheService
extends TurbineBaseService
implements GlobalCacheService, java.lang.Runnable

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. The following properties are needed to configure this service:

 services.GlobalCacheService.classname=org.apache.turbine.services.cache.TurbineGlobalCacheService
 services.GlobalCacheService.cache.initial.size=20
 services.GlobalCacheService.cache.check.frequency=5000
 
classname
the classname of this service
cache.initial.size
Initial size of hash table use to store cached objects. If this property is not present, the default value is 20
cache.check.frequency
Cache check frequency in Millis (1000 Millis = 1 second). If this property is not present, the default value is 5000

Version:
$Id: TurbineGlobalCacheService.java 534527 2007-05-02 16:10:59Z tv $
Author:
Dave Bryson, Jon S. Stevens, John Thorhauer, Henning P. Schmiedehausen

Field Summary
static java.lang.String CACHE_CHECK_FREQUENCY
          The property for the Cache check frequency
static 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 java.lang.String INITIAL_CACHE_SIZE
          The property for the InitalCacheSize
 
Fields inherited from class org.apache.turbine.services.BaseService
configuration, name, serviceBroker
 
Fields inherited from class org.apache.turbine.services.BaseInitable
initableBroker, isInitialized
 
Fields inherited from interface org.apache.turbine.services.cache.GlobalCacheService
SERVICE_NAME
 
Constructor Summary
TurbineGlobalCacheService()
          Constructor.
 
Method Summary
 void addObject(java.lang.String id, CachedObject o)
          Adds an object to the cache.
 void clearCache()
          Iterate through the cache and remove or refresh stale objects.
 void flushCache()
          Flush the cache of all objects.
 int getCacheSize()
          Returns the current size of the cache.
 int getNumberOfObjects()
          Returns the number of objects currently stored in the cache
 CachedObject getObject(java.lang.String id)
          Returns an item from the cache.
 void init()
          Called the first time the Service is used.
 void removeObject(java.lang.String id)
          Removes an object from the cache.
 void run()
          Circle through the cache and remove stale objects.
 
Methods inherited from class org.apache.turbine.services.TurbineBaseService
init, init, init, shutdown
 
Methods inherited from class org.apache.turbine.services.BaseService
getConfiguration, getName, getProperties, getServiceBroker, setName, setServiceBroker
 
Methods inherited from class org.apache.turbine.services.BaseInitable
getInit, getInitableBroker, setInit, setInitableBroker
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.turbine.services.Service
getConfiguration, getName, getProperties, setName, setServiceBroker
 
Methods inherited from interface org.apache.turbine.services.Initable
getInit, init, setInitableBroker, shutdown
 

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 java.lang.String INITIAL_CACHE_SIZE
The property for the InitalCacheSize

See Also:
Constant Field Values

CACHE_CHECK_FREQUENCY

public static final java.lang.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
Constructor Detail

TurbineGlobalCacheService

public TurbineGlobalCacheService()
Constructor.

Method Detail

init

public void init()
          throws InitializationException
Called the first time the Service is used.

Specified by:
init in interface Initable
Overrides:
init in class TurbineBaseService
Throws:
InitializationException

getObject

public CachedObject getObject(java.lang.String id)
                       throws ObjectExpiredException
Returns an item from the cache. RefreshableCachedObject will be refreshed if it is expired and not untouched.

Specified by:
getObject in interface GlobalCacheService
Parameters:
id - 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 void addObject(java.lang.String id,
                      CachedObject o)
Adds an object to the cache.

Specified by:
addObject in interface GlobalCacheService
Parameters:
id - The key to store the object by.
o - The object to cache.

removeObject

public void removeObject(java.lang.String id)
Removes an object from the cache.

Specified by:
removeObject in interface GlobalCacheService
Parameters:
id - The String id for the object.

run

public void run()
Circle through the cache and remove stale objects. Frequency is determined by the cacheCheckFrequency property.

Specified by:
run in interface java.lang.Runnable

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 interface GlobalCacheService
Returns:
int number of object in the cache

getCacheSize

public int getCacheSize()
                 throws java.io.IOException
Returns the current size of the cache.

Specified by:
getCacheSize in interface GlobalCacheService
Returns:
int representing current cache size in number of bytes
Throws:
java.io.IOException

flushCache

public void flushCache()
Flush the cache of all objects.

Specified by:
flushCache in interface GlobalCacheService


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.