[Opensymphony-oscache] How to Inspect Cache Entry?

jz <[email protected]> Wed, 31 Aug 2005 10:04:03 CDT
Newsgroups gmane.comp.java.open-symphony.os-cache
Message-ID <8225580.1125500938231.JavaMail.os-j2ee@opensymphony01.contegix.com>
I am able to work around this problem with two cron passes.

There are two small issues, other than attempting to retrieve the cached item twice.

1) The use of alternate cron for last modification check is only accurate to the minute.  It might be just a little bit over aggressive.

2) There is no provision to specify year in cron expression, the alternate cron is essentially setting content caches to expire at the turn of the year, at which time the system must endure massive expiration of all cached items .

Here is the code that I used on JSP (using application scope only).

=====================================================
String cacheKey = <my key>;

//
// first check if the document has been modified
//
Date docModDate = <external expiration time>;
    // last time the document is updated by content manager
    // this is a piece of metadata from Documentum
Calendar cal = new GregorianCalendar();
cal.setTime(docModDate);
int minute = cal.get(Calendar.MINUTE);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) + 1;
StringBuffer altCron = new StringBuffer();
altCron.append(minute).append(' ').append(hour).append(' ');
altCron.append(dayOfMonth).append(' ').append(month).append(' ');
altCron.append('*');
    // the cron expression cuts off at the exact second, which is
    // the cut-off time slightly ahead of the actual moment, so
    // there is no need to further adjust the values of minute and
    // hour
ServletCacheAdministrator admin = ServletCacheAdministrator.getInstance(pageContext.getServletContext());
Cache cache = admin.getAppScopeCache(pageContext.getServletContext());
String cachedText = null;
if (cache != null) {
    String actualKey = admin.generateEntryKey(cacheKey, request, PageContext.APPLICATION_SCOPE);
    try {
          cachedText = (String)cache.getFromCache(actualKey, CacheEntry.INDEFINITE_EXPIRY, altCron.toString());
    } catch (NeedsRefreshException e) {
          cache.cancelUpdate(actualKey);
          cache.flushEntry(actualKey);
    }
}

//
// now honor additional expiration policies specified per content item
// the default is to expire cache after mid-night
//
String docCachePolicy = <external expiration policy overriding the default>;
String cron = (docCachePolicy == null) ? "0 0 * * 1-5" : docCachePolicy;
    // the policy uses cron expression
    // empty string causes the OSCache to treat cron as null
    //
    // the default setting is to force expiry after mid-night during workdays
    // (including holidays), whose cron expression is
    //          0 0 * * 1-5
%>

<cache:cache key="<%= cacheKey %>" scope="application" cron="<%= cron %>">
  ...
</cache:cache>
=====================================================

JZ

---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=6275&messageID=14225#14225