Configuring the ProductDisplay JSP file for JSP Caching

Because Portlet Caching uses DynaCache underneath, it is recommended to disable Portlet Caching while using JSP Caching. If both of these caching techniques are used at the same time, you are going to have two versions of the same cached content for each cacheable portlet JSP.

.

A cache policy is required for your portlet application. For more information, see cachespec.xml.

For the purpose of this demonstration, the following cache policy is used for the ProductDisplay JSP file.


<cache-entry>
    <class>servlet</class>
    <name>/jsp/html/catalog/ProductDisplay.jsp</name>
    <property name="consume-subfragments">true</property>
    <cache-id>
        <component id="renderName" type="parameter">
            <required>true</required>
        </component>
        <component id="catEntryId" type="parameter">
            <required>false</required>
        </component>
        <component id="langId" type="parameter">
            <required>false</required>
        </component>
        <timeout>180</timeout>
        <inactivity>35</inactivity>
    </cache-id>
    <dependency-id>ProductDependency
        <component id="catEntryId" type="parameter">
            <required>true</required>
        </component>
    </dependency-id>
</cache-entry>

The above defines a cache entry for the ProductDisplay JSP file with the name to be the relative path of the JSP file location from the context root of the portlet application. A property called consume-subfragments is used, where setting this property to true causes all other fragments from its child JSP file to be cached as one cached content. Typically it is not necessary to do this, but for the purpose of this demonstration, this property is set to true so that the entire portlet content can show up in one cache entry. Later, it will be demonstrated how to cache only a partial portion of the cacheable content.

A time expiration rule has also been added to this cache policy. This sample rule keeps the cache entry in the cache for a minimum of 35 seconds and a maximum of 180 seconds. If the cache entry is accessed within each 35 second inactivity period, the inactivity period is extended for another 35 seconds. However, because the timeout element is also configured, the cache entry is always invalidated after 180 seconds. If the cache entry is not accessed within the 35 second period, the entry is removed from the cache.

The last rule in this sample cache policy is for group invalidation. For example, when information on this product is modified, it is possible to invalidate all cached entries in DynaCache that have this product identifier in the dependency ID.