Beside the JSP tag library and the CacheFilter you can use OSCache through its straightforward API. E.g. you can use the GeneralCacheAdministrator to create, flush and administrate the cache. Typical use with fail overString 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 from the database) 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 overString 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 from the database) 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); } } } |