com.opensymphony.oscache.general
Class GeneralCacheAdministrator

java.lang.Object
  extended by com.opensymphony.oscache.base.AbstractCacheAdministrator
      extended by com.opensymphony.oscache.general.GeneralCacheAdministrator
All Implemented Interfaces:
Serializable

public class GeneralCacheAdministrator
extends AbstractCacheAdministrator

A GeneralCacheAdministrator creates, flushes and administers the cache. EXAMPLES :


 // ---------------------------------------------------------------
 // Typical use with fail over
 // ---------------------------------------------------------------
 String myKey = "myKey";
 String myValue;
 int myRefreshPeriod = 1000;
 try {
     // Get from the cache
     myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
 } catch (NeedsRefreshException nre) {
     try {
         // Get the value (probably by calling an EJB)
         myValue = "This is the content retrieved.";
         // Store in the cache
         admin.putInCache(myKey, myValue);
     } catch (Exception ex) {
         // We have the current content if we want fail-over.
         myValue = (String) nre.getCacheContent();
         // It is essential that cancelUpdate is called if the
         // cached content is not rebuilt
         admin.cancelUpdate(myKey);
     }
 }



 // ---------------------------------------------------------------
 // Typical use without fail over
 // ---------------------------------------------------------------
 String myKey = "myKey";
 String myValue;
 int myRefreshPeriod = 1000;
 try {
     // Get from the cache
     myValue = (String) admin.getFromCache(myKey, myRefreshPeriod);
 } catch (NeedsRefreshException nre) {
     try {
         // Get the value (probably by calling an EJB)
         myValue = "This is the content retrieved.";
         // Store in the cache
         admin.putInCache(myKey, myValue);
         updated = true;
     } finally {
         if (!updated) {
             // It is essential that cancelUpdate is called if the
             // cached content could not be rebuilt
             admin.cancelUpdate(myKey);
         }
     }
 }
 // ---------------------------------------------------------------
 // ---------------------------------------------------------------
 

Version:
$Revision: 254 $
Author:
Francois Beauregard, Alain Bergevin
See Also:
Serialized Form

Field Summary
private  Cache applicationCache
          Application cache
private static org.apache.commons.logging.Log log
           
 
Fields inherited from class com.opensymphony.oscache.base.AbstractCacheAdministrator
algorithmClass, CACHE_ALGORITHM_KEY, CACHE_BLOCKING_KEY, CACHE_CAPACITY_KEY, CACHE_DISK_UNLIMITED_KEY, CACHE_ENTRY_EVENT_LISTENERS_KEY, CACHE_MEMORY_KEY, CACHE_PERSISTENCE_OVERFLOW_KEY, cacheCapacity, config, listenerList, PERSISTENCE_CLASS_KEY
 
Constructor Summary
GeneralCacheAdministrator()
          Create the cache administrator.
GeneralCacheAdministrator(Properties p)
          Create the cache administrator with the specified properties
 
Method Summary
 void cancelUpdate(String key)
          Cancels a pending cache update.
private  void createCache()
          Creates a cache in this admin
 void destroy()
          Shuts down the cache administrator.
 void flushAll()
          Flush the entire cache immediately.
 void flushAll(Date date)
          Flush the entire cache at the given date.
 void flushEntry(String key)
          Flushes a single cache entry.
 void flushGroup(String group)
          Flushes all items that belong to the specified group.
 void flushPattern(String pattern)
          Deprecated. For performance and flexibility reasons it is preferable to store cache entries in groups and use the flushGroup(String) method instead of relying on pattern flushing.
 Cache getCache()
          Grabs a cache
 Object getFromCache(String key)
          Get an object from the cache
 Object getFromCache(String key, int refreshPeriod)
          Get an object from the cache
 Object getFromCache(String key, int refreshPeriod, String cronExpression)
          Get an object from the cache
 void putInCache(String key, Object content)
          Put an object in a cache
 void putInCache(String key, Object content, EntryRefreshPolicy policy)
          Put an object in a cache
 void putInCache(String key, Object content, String[] groups)
          Puts an object in a cache
 void putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy)
          Puts an object in a cache
 void removeEntry(String key)
          Remove an object from the cache
 void setCacheCapacity(int capacity)
          Sets the cache capacity (number of items).
 
Methods inherited from class com.opensymphony.oscache.base.AbstractCacheAdministrator
configureStandardListeners, finalizeListeners, getCacheEventListeners, getProperty, isBlocking, isMemoryCaching, isOverflowPersistence, isUnlimitedDiskCache, setAlgorithmClass, setOverflowPersistence, setPersistenceListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final transient org.apache.commons.logging.Log log

applicationCache

private Cache applicationCache
Application cache

Constructor Detail

GeneralCacheAdministrator

public GeneralCacheAdministrator()
Create the cache administrator.


GeneralCacheAdministrator

public GeneralCacheAdministrator(Properties p)
Create the cache administrator with the specified properties

Method Detail

getCache

public Cache getCache()
Grabs a cache

Returns:
The cache

removeEntry

public void removeEntry(String key)
Remove an object from the cache

Parameters:
key - The key entered by the user.

getFromCache

public Object getFromCache(String key)
                    throws NeedsRefreshException
Get an object from the cache

Parameters:
key - The key entered by the user.
Returns:
The object from cache
Throws:
NeedsRefreshException - when no cache entry could be found with the supplied key, or when an entry was found but is considered out of date. If the cache entry is a new entry that is currently being constructed this method will block until the new entry becomes available. Similarly, it will block if a stale entry is currently being rebuilt by another thread and cache blocking is enabled (cache.blocking=true).

getFromCache

public Object getFromCache(String key,
                           int refreshPeriod)
                    throws NeedsRefreshException
Get an object from the cache

Parameters:
key - The key entered by the user.
refreshPeriod - How long the object can stay in cache in seconds. To allow the entry to stay in the cache indefinitely, supply a value of CacheEntry.INDEFINITE_EXPIRY
Returns:
The object from cache
Throws:
NeedsRefreshException - when no cache entry could be found with the supplied key, or when an entry was found but is considered out of date. If the cache entry is a new entry that is currently being constructed this method will block until the new entry becomes available. Similarly, it will block if a stale entry is currently being rebuilt by another thread and cache blocking is enabled (cache.blocking=true).

getFromCache

public Object getFromCache(String key,
                           int refreshPeriod,
                           String cronExpression)
                    throws NeedsRefreshException
Get an object from the cache

Parameters:
key - The key entered by the user.
refreshPeriod - How long the object can stay in cache in seconds. To allow the entry to stay in the cache indefinitely, supply a value of CacheEntry.INDEFINITE_EXPIRY
cronExpression - A cron expression that the age of the cache entry will be compared to. If the entry is older than the most recent match for the cron expression, the entry will be considered stale.
Returns:
The object from cache
Throws:
NeedsRefreshException - when no cache entry could be found with the supplied key, or when an entry was found but is considered out of date. If the cache entry is a new entry that is currently being constructed this method will block until the new entry becomes available. Similarly, it will block if a stale entry is currently being rebuilt by another thread and cache blocking is enabled (cache.blocking=true).

cancelUpdate

public void cancelUpdate(String key)
Cancels a pending cache update. This should only be called by a thread that received a NeedsRefreshException and was unable to generate some new cache content.

Parameters:
key - The cache entry key to cancel the update of.

destroy

public void destroy()
Shuts down the cache administrator.


flushAll

public void flushAll()
Flush the entire cache immediately.


flushAll

public void flushAll(Date date)
Flush the entire cache at the given date.

Parameters:
date - The time to flush

flushEntry

public void flushEntry(String key)
Flushes a single cache entry.


flushGroup

public void flushGroup(String group)
Flushes all items that belong to the specified group.

Parameters:
group - The name of the group to flush

flushPattern

public void flushPattern(String pattern)
Deprecated. For performance and flexibility reasons it is preferable to store cache entries in groups and use the flushGroup(String) method instead of relying on pattern flushing.

Allows to flush all items that have a specified pattern in the key.

Parameters:
pattern - Pattern.

putInCache

public void putInCache(String key,
                       Object content,
                       EntryRefreshPolicy policy)
Put an object in a cache

Parameters:
key - The key entered by the user
content - The object to store
policy - Object that implements refresh policy logic

putInCache

public void putInCache(String key,
                       Object content)
Put an object in a cache

Parameters:
key - The key entered by the user
content - The object to store

putInCache

public void putInCache(String key,
                       Object content,
                       String[] groups)
Puts an object in a cache

Parameters:
key - The unique key for this cached object
content - The object to store
groups - The groups that this object belongs to

putInCache

public void putInCache(String key,
                       Object content,
                       String[] groups,
                       EntryRefreshPolicy policy)
Puts an object in a cache

Parameters:
key - The unique key for this cached object
content - The object to store
groups - The groups that this object belongs to
policy - The refresh policy to use

setCacheCapacity

public void setCacheCapacity(int capacity)
Sets the cache capacity (number of items). If the cache contains more than capacity items then items will be removed to bring the cache back down to the new size.

Overrides:
setCacheCapacity in class AbstractCacheAdministrator
Parameters:
capacity - The new capacity of the cache

createCache

private void createCache()
Creates a cache in this admin



Copyright © 2011 OpenSymphony. All Rights Reserved.