org.apache.turbine.services.cache
Class TurbineGlobalCacheService

java.lang.Object
  |
  +--org.apache.turbine.services.BaseInitable
        |
        +--org.apache.turbine.services.BaseService
              |
              +--org.apache.turbine.services.TurbineBaseService
                    |
                    +--org.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.

Version:
$Id$
Author:
Dave Bryson, Jon S. Stevens, John Thorhauer

Field Summary
private  java.util.Hashtable cache
          The cache.
private  long cacheCheckFrequency
          cacheCheckFrequency (default - 5 seconds)
 
Fields inherited from class org.apache.turbine.services.BaseService
configuration, name, properties, 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.
 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, registerNatives, toString, wait, wait, wait
 
Methods inherited from interface org.apache.turbine.services.Service
getConfiguration, getProperties, setName, setServiceBroker
 
Methods inherited from interface org.apache.turbine.services.Initable
getInit, init, setInitableBroker, shutdown
 

Field Detail

cache

private java.util.Hashtable cache
The cache.

cacheCheckFrequency

private long cacheCheckFrequency
cacheCheckFrequency (default - 5 seconds)
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
Following copied from interface: org.apache.turbine.services.Initable
Throws:
InitializationException, - if initialization of this class was not successful.

getObject

public CachedObject getObject(java.lang.String id)
                       throws ObjectExpiredException
Returns an item from the cache.
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
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


Copyright © 1999-2001 Apache Software Foundation. All Rights Reserved.