Show TOC

ICacheValidatorLocate this document in the navigation structure

Use

The ICacheValidator interface 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:

  • isCacheValid : Determines 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.

                      public boolean isCacheValid(IPortalComponentRequest request,
                   String key);
                   

    The key parameter is the string that was returned from the getValidationKey() method during the last request.

  • getValidationKey: Returns a key to be passed to the isCacheValid() method on the next request.

                      public String getValidationKey(IPortalComponentRequest request);
                   
Example

The following code displays an applet, whose size is read from the component profile. The ICacheValidator interface is used to invalidate the cache when these values change:

            public class DisplayAppletComponent
   implements ICacheValidator, ICachablePortalComponent {
 
   public void 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.
    **/
   public boolean 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;
   }
}