火星文 技术研习社

Noname Cat, Keep Thinking
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
http://www.opensymphony.com/oscache/api/com/opensymphony/oscache/general/GeneralCacheAdministrator.html

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

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).