| Method from com.opensymphony.oscache.web.ServletCacheAdministrator Detail: |
public void addScopeEventListener(ScopeEventListener listener) {
listenerList.add(ScopeEventListener.class, listener);
}
Register a listener for Cache Map events. |
public void cancelUpdate(int scope,
HttpServletRequest request,
String key) {
Cache cache = getCache(request, scope);
key = this.generateEntryKey(key, request, scope);
cache.cancelUpdate(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. |
public static void destroyInstance(ServletContext context) {
ServletCacheAdministrator admin;
Map admins = (Map) context.getAttribute(CACHE_ADMINISTRATORS_KEY);
if (admins != null)
{
Set keys = admins.keySet();
Iterator it = keys.iterator();
while (it.hasNext())
{
String adminKey = (String) it.next();
admin = (ServletCacheAdministrator) admins.get( adminKey );
if (admin != null)
{
// Finalize the application scope cache
Cache cache = (Cache) context.getAttribute(admin.getCacheKey());
if (cache != null) {
admin.finalizeListeners(cache);
context.removeAttribute(admin.getCacheKey());
context.removeAttribute(adminKey);
cache = null;
if (log.isInfoEnabled()) {
log.info("Shut down the ServletCacheAdministrator "+adminKey);
}
}
admin = null;
}
}
context.removeAttribute(CACHE_ADMINISTRATORS_KEY);
}
}
Shuts down all servlet cache administrators. This should usually only
be called when the controlling application shuts down. |
protected void finalizeListeners(Cache cache) {
super.finalizeListeners(cache);
}
Finalizes all the listeners that are associated with the given cache object |
public void flushAll() {
flushAll(new Date());
}
Flush all scopes instantly. |
public void flushAll(Date date) {
synchronized (flushTimes) {
setFlushTime(date, PageContext.APPLICATION_SCOPE);
setFlushTime(date, PageContext.SESSION_SCOPE);
setFlushTime(date, PageContext.REQUEST_SCOPE);
setFlushTime(date, PageContext.PAGE_SCOPE);
}
// Trigger a flushAll event
dispatchScopeEvent(ScopeEventType.ALL_SCOPES_FLUSHED, -1, date, null);
}
Flush all scopes at a particular time |
public String generateEntryKey(String key,
HttpServletRequest request,
int scope) {
return generateEntryKey(key, request, scope, null, null);
}
Generates a cache entry key.
If the string key is not specified, the HTTP request URI and QueryString is used.
Operating systems that have a filename limitation less than 255 or have
filenames that are case insensitive may have issues with key generation where
two distinct pages map to the same key.
POST Requests (which have no distinguishing
query string) may also generate identical keys for what is actually different pages.
In these cases, specify an explicit key attribute for the CacheTag. |
public String generateEntryKey(String key,
HttpServletRequest request,
int scope,
String language) {
return generateEntryKey(key, request, scope, language, null);
}
Generates a cache entry key.
If the string key is not specified, the HTTP request URI and QueryString is used.
Operating systems that have a filename limitation less than 255 or have
filenames that are case insensitive may have issues with key generation where
two distinct pages map to the same key.
POST Requests (which have no distinguishing
query string) may also generate identical keys for what is actually different pages.
In these cases, specify an explicit key attribute for the CacheTag. |
public String generateEntryKey(String key,
HttpServletRequest request,
int scope,
String language,
String suffix) {
/**
* Used for generating cache entry keys.
*/
StringBuffer cBuffer = new StringBuffer(AVERAGE_KEY_LENGTH);
// Append the language if available
if (language != null) {
cBuffer.append(FILE_SEPARATOR).append(language);
}
// Servers for multiple host domains need this distinction in the key
if (useHostDomainInKey) {
cBuffer.append(FILE_SEPARATOR).append(request.getServerName());
}
if (key != null) {
cBuffer.append(FILE_SEPARATOR).append(key);
} else {
String generatedKey = request.getRequestURI();
if (generatedKey.charAt(0) != FILE_SEPARATOR_CHAR) {
cBuffer.append(FILE_SEPARATOR_CHAR);
}
cBuffer.append(generatedKey);
cBuffer.append("_").append(request.getMethod()).append("_");
generatedKey = getSortedQueryString(request);
if (generatedKey != null) {
try {
java.security.MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
byte[] b = digest.digest(generatedKey.getBytes());
cBuffer.append('_");
// Base64 encoding allows for unwanted slash characters.
cBuffer.append(toBase64(b).replace('/", '_"));
} catch (Exception e) {
// Ignore query string
}
}
}
// Do we want a suffix
if ((suffix != null) && (suffix.length() > 0)) {
cBuffer.append(suffix);
}
return cBuffer.toString();
}
Generates a cache entry key.
If the string key is not specified, the HTTP request URI and QueryString is used.
Operating systems that have a filename limitation less than 255 or have
filenames that are case insensitive may have issues with key generation where
two distinct pages map to the same key.
POST Requests (which have no distinguishing
query string) may also generate identical keys for what is actually different pages.
In these cases, specify an explicit key attribute for the CacheTag. |
public Cache getAppScopeCache(ServletContext context) {
Cache cache;
Object obj = context.getAttribute(getCacheKey());
if ((obj == null) || !(obj instanceof Cache)) {
if (log.isInfoEnabled()) {
log.info("Created new application-scoped cache at key: " + getCacheKey());
}
cache = createCache(PageContext.APPLICATION_SCOPE, null);
context.setAttribute(getCacheKey(), cache);
} else {
cache = (Cache) obj;
}
return cache;
}
A convenience method to retrieve the application scope cache |
public Cache getCache(HttpServletRequest request,
int scope) {
if (scope == PageContext.APPLICATION_SCOPE) {
return getAppScopeCache(context);
}
if (scope == PageContext.SESSION_SCOPE) {
return getSessionScopeCache(request.getSession(true));
}
throw new RuntimeException("The supplied scope value of " + scope + " is invalid. Acceptable values are PageContext.APPLICATION_SCOPE and PageContext.SESSION_SCOPE");
}
Grabs the cache for the specified scope |
public String getCacheKey() {
if (cacheKey == null) {
cacheKey = getProperty(CACHE_KEY_KEY);
if (cacheKey == null) {
cacheKey = DEFAULT_CACHE_KEY;
}
}
return cacheKey;
}
Get the cache key from the properties. Set it to a default value if it
is not present in the properties |
public Date getFlushTime(int scope) {
synchronized (flushTimes) {
return (Date) flushTimes.get(new Integer(scope));
}
}
Get the flush time for a particular scope. |
public Object getFromCache(int scope,
HttpServletRequest request,
String key,
int refreshPeriod) throws NeedsRefreshException {
Cache cache = getCache(request, scope);
key = this.generateEntryKey(key, request, scope);
return cache.getFromCache(key, refreshPeriod);
}
Retrieve an item from the cache |
public static ServletCacheAdministrator getInstance(ServletContext context) {
return getInstance(context, null);
}
Obtain an instance of the CacheAdministrator |
public static synchronized ServletCacheAdministrator getInstance(ServletContext context,
Properties p) {
String adminKey = null;
if (p!= null) {
adminKey = p.getProperty(CACHE_KEY_KEY);
}
if (adminKey == null) {
adminKey = DEFAULT_CACHE_KEY;
}
adminKey += CACHE_ADMINISTRATOR_KEY_SUFFIX;
ServletCacheAdministrator admin = (ServletCacheAdministrator) context.getAttribute(adminKey);
// First time we need to create the administrator and store it in the
// servlet context
if (admin == null) {
admin = new ServletCacheAdministrator(context, p);
Map admins = (Map) context.getAttribute(CACHE_ADMINISTRATORS_KEY);
if (admins == null) {
admins = new HashMap();
}
admins.put(adminKey, admin);
context.setAttribute(CACHE_ADMINISTRATORS_KEY, admins);
context.setAttribute(adminKey, admin);
if (log.isInfoEnabled()) {
log.info("Created new instance of ServletCacheAdministrator with key "+adminKey);
}
admin.getAppScopeCache(context);
}
if (admin.context == null) {
admin.context = context;
}
return admin;
}
Obtain an instance of the CacheAdministrator |
public static ServletCacheAdministrator getInstanceFromKey(ServletContext context,
String key) {
// Note we do not bother to check if the key is null because it mustn't.
if (!key.endsWith(CACHE_ADMINISTRATOR_KEY_SUFFIX)) {
key = key + CACHE_ADMINISTRATOR_KEY_SUFFIX;
}
return (ServletCacheAdministrator) context.getAttribute(key);
}
Obtain an instance of the CacheAdministrator for the specified key |
public Cache getSessionScopeCache(HttpSession session) {
Cache cache;
Object obj = session.getAttribute(getCacheKey());
if ((obj == null) || !(obj instanceof Cache)) {
if (log.isInfoEnabled()) {
log.info("Created new session-scoped cache in session " + session.getId() + " at key: " + getCacheKey());
}
cache = createCache(PageContext.SESSION_SCOPE, session.getId());
session.setAttribute(getCacheKey(), cache);
} else {
cache = (Cache) obj;
}
return cache;
}
A convenience method to retrieve the session scope cache |
protected String getSortedQueryString(HttpServletRequest request) {
Map paramMap = request.getParameterMap();
if (paramMap.isEmpty()) {
return null;
}
Set paramSet = new TreeMap(paramMap).entrySet();
StringBuffer buf = new StringBuffer();
boolean first = true;
for (Iterator it = paramSet.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String[] values = (String[]) entry.getValue();
for (int i = 0; i < values.length; i++) {
String key = (String) entry.getKey();
if ((key.length() != 10) || !"jsessionid".equals(key)) {
if (first) {
first = false;
} else {
buf.append('&");
}
buf.append(key).append('=").append(values[i]);
}
}
}
// We get a 0 length buffer if the only parameter was a jsessionid
if (buf.length() == 0) {
return null;
} else {
return buf.toString();
}
}
Creates a string that contains all of the request parameters and their
values in a single string. This is very similar to
HttpServletRequest.getQueryString() except the parameters are
sorted by name, and if there is a jsessionid parameter it is
filtered out.
If the request has no parameters, this method returns null. |
public boolean isScopeFlushed(CacheEntry cacheEntry,
int scope) {
Date flushDateTime = getFlushTime(scope);
if (flushDateTime != null) {
long lastUpdate = cacheEntry.getLastUpdate();
return (flushDateTime.getTime() >= lastUpdate);
} else {
return false;
}
}
Checks if the given scope was flushed more recently than the CacheEntry provided.
Used to determine whether to refresh the particular CacheEntry. |
public void logError(String message) {
log.error("[oscache]: " + message);
}
Log error messages to commons logging. |
public void putInCache(int scope,
HttpServletRequest request,
String key,
Object content) {
putInCache(scope, request, key, content, null);
}
Put an object in the cache. This should only be called by a thread
that received a NeedsRefreshException . Using session scope
the thread has to insure that the session wasn't invalidated in
the meantime. CacheTag and CacheFilter guarantee that the same
cache is used in cancelUpdate and getFromCache. |
public void putInCache(int scope,
HttpServletRequest request,
String key,
Object content,
EntryRefreshPolicy policy) {
Cache cache = getCache(request, scope);
key = this.generateEntryKey(key, request, scope);
cache.putInCache(key, content, policy);
}
Put an object in the cache. This should only be called by a thread
that received a NeedsRefreshException . Using session scope
the thread has to insure that the session wasn't invalidated in
the meantime. CacheTag and CacheFilter guarantee that the same
cache is used in cancelUpdate and getFromCache. |
public void removeScopeEventListener(ScopeEventListener listener) {
listenerList.remove(ScopeEventListener.class, listener);
}
Unregister a listener for Cache Map events. |
public void setCacheCapacity(int scope,
HttpServletRequest request,
int capacity) {
setCacheCapacity(capacity);
getCache(request, scope).setCapacity(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. |
public void setFlushTime(int scope) {
setFlushTime(new Date(), scope);
}
Set the flush time for a specific scope to the current time. |
public void setFlushTime(Date date,
int scope) {
if (log.isInfoEnabled()) {
log.info("Flushing scope " + scope + " at " + date);
}
synchronized (flushTimes) {
if (date != null) {
// Trigger a SCOPE_FLUSHED event
dispatchScopeEvent(ScopeEventType.SCOPE_FLUSHED, scope, date, null);
flushTimes.put(new Integer(scope), date);
} else {
logError("setFlushTime called with a null date.");
throw new IllegalArgumentException("setFlushTime called with a null date.");
}
}
}
Set the flush time for a specific scope to a specific time |