ICacheValidator
TheICacheValidatorinterface enables you to save a string value (validation key) with the cache and then, during the next request, determine whether the cache is still valid based on the string that was stored.
The interface defines the following methods:
isCacheValidDetermines whether the cache is still valid. The key that was stored with the cache is passed to the method to help determine the validity of the cache.
publicboolean isCacheValid(IPortalComponentRequest request, String key);
The key parameter is the string that was returned from thegetValidationKey()method during the last request.
getValidationKeyReturns a key to be passed to theisCacheValid()method on the next request.
publicString getValidationKey(IPortalComponentRequest request);
Example
The following code displays an applet, whose size is read from the component profile. TheICacheValidatorinterface is used to invalidate the cache when these values change:
publicclass DisplayAppletComponent
implements ICacheValidator, ICachablePortalComponent {
publicvoid doContent(
IPortalComponentRequest request,
IPortalComponentResponse response) {
if(request!=null&&response!=null){
// Display applet of a certain size
int witdh = getAppletWidthFromProfile(request);
int height = getAppletHeightFromProfile(request);
displayApplet(width, height);
}
}
public CachingLevel getCachingLevel() {
return CachingLevel.USER;
}
/**
* The method determines if the key has changed. If it has,
* the cache is invalidated.
**/
publicboolean isCacheValid(IPortalComponentRequest request, String key) {
if(request!=null&&response!=null){
int witdh = getAppletWidthFromProfile(request);
int height = getAppletHeightFromProfile(request);
String newKey = computeKey(width, height);
return key.equals(newKey);
}
}
/**
*Saves with the cache a key based on profile properties
**/
public String getValidationKey(IPortalComponentRequest request) {
if(request!=null){
int width = getAppletWidthFromProfile(request);
int height = getAppletHeightFromProfile(request);
// Computes a key with the width and height
String key = computeKey(width, height);
return key;
}
}
private String computeKey( int width, int height) {
String key = "" + width + "|" + height;
return key;
}
}