Use OSCache with or without fail over by Class GeneralCacheAdministrator
Posted on 2006-12-05 22:24 剑廿三 阅读(696) 评论(0) 编辑 收藏 举报
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 ofCacheEntry.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
).