public interface ICachingService
{
public static String NAME="Aspire.AppObjects.CachingService";
//place the object with a key
public void cache(Object objectToCache
,String key
,int howLongInTicks);
//returns null if the key is not there
public Object getFromCache(String key);
//For the given key invalidate the object
public void invalidate(String key);
}
public class RequestBasedMasterPagePart
{
protected IMasterPage create(String requestName, Map params)
throws RequestExecutionException
{
//Get it from the cache
Object obj = CacheUtils.getObjectFromCache(requestName, params);
if (obj != null)
{
//Object is in the cache
return (IMasterPage)obj;
}
//some how get the master page
//Place the object in cache
CacheUtils.putObjectInCache(requestName, params,masterPage);
return masterPage;
}//eof-function
}//eof-class
request.GetMasterPage.classname=com.ai.masterpage.RequestBasedMasterPagePart
request.GetMasterPage.masterPageTemplateRN=GetMasterPageTemplate
request.GetMasterPage.cacheKey=/{ownerUserId}/masterpage
The CacheUtils will just figure out the key from the request name, which in this case is "GetMasterPage". In other words, the CacheUtils can cache an object based on its request name and parameters in turn deriving the cache key from those. Using this pattern we can enable any request centric object for caching based on parameters. For example we can cache this object per user or any other parameter if that is needed.
request.UpdateMasterPage.classname=com.ai.db.DBPreTranslateArgsMultiRequestExecutor
request.UpdateMasterPage.db=reportsdb
request.UpdateMasterPage.query_type=update
request.UpdateMasterPage.request.1=UMP.checkItemID
request.UpdateMasterPage.request.2=UpdateMasterPage1
request.UpdateMasterPage.request.3=InvalidateMasterPageCache
request.invalidateMasterPageCache.classname=com.ai.cache.InvalidateCachePart
request.invalidateMasterPageCache.cacheKey=/{profile_user}/masterpage
public class DefaultCachingService implements ICachingService
{
//
Hashtable ht = new Hashtable();
//**************************************************************************
//place the object with a key
//**************************************************************************
public void cache(Object objectToCache
,String key
,int howLongInTicks)
{
CacheableEntity ce = new CacheableEntity(objectToCache,key,howLongInTicks);
ht.put(key,ce);
}
//**************************************************************************
//returns null if the key is not there
//**************************************************************************
public Object getFromCache(String key)
{
CacheableEntity ce = (CacheableEntity)ht.get(key);
if (ce == null)
{
AppObjects.log("Info: Requested cached object for key:" + key
+ " is not in the cache.");
return null;
}
return ce.m_cachedObject;
}
//**************************************************************************
//For the given key invalidate the object
//**************************************************************************
public void invalidate(String key)
{
CacheableEntity ce = (CacheableEntity)ht.get(key);
if (ce == null)
{
AppObjects.log("Warn: Requested cached object for key:" + key
+ " is not in the cache for invalidation");
return;
}
//Ce is there. Remove it from the cache
AppObjects.log("Info: Cached object with key:" + key
+ " is being removed from the cache");
ht.remove(key);
}
}//eof-class
//**************************************************************************
//* Local CacheableEntity class
//**************************************************************************
class CacheableEntity
{
public Object m_cachedObject;
public String m_key;
public int m_ticks;
public CacheableEntity(Object inObject, String inKey, int inTicks)
{
m_cachedObject = inObject;
m_key = inKey;
m_ticks = inTicks;
}
}
1. General Introduction to other Server side Patterns in this series