This Service functions as a Template component.
It is written for use in Turbine but it can be used in any container compatible with Avalon's ECM container.
First, here is the role configuration.
<role name="org.apache.fulcrum.bsf.BSFService" shorthand="cache" default-class="org.apache.fulcrum.bsf.TurbineBSFService"/>
And here is the configuration:
<cache cacheInitialSize="20" cacheCheckFrequency="5"/>
GlobalCacheService gs = null; try { /* * Look for the item in the cache. * If it doesn't exist or the item is stale, * the cache will throw an exception. */ gs = (GlobalCacheService)avalonComponentService.lookup(GlobalCacheService.ROLE) CachedObject obj = gs.getObject("cached_object"); data.setMessage( data.getScreen() + " Got " + obj.getContents().toString() + " from global cache!" ); } catch(ObjectExpiredException gone) { /* * Add the item to the cache. */ gs.addObject("cached_object", new CachedObject("in_the_cache",5000)); data.setMessage( data.getScreen() + " Refreshed/or added new item to" + " the cache! Expires in 5 seconds" ); }
You can also place an expiration time on your objects so the Service will automatically remove them when they expire. If you don't specify an expiration time, the Service uses 5 seconds. To see an example, look at the test case CacheTest