Caching portlet output


Portlet output can be cached at the local appserver or using a remote proxy server.

 

Local cache settings

If servlet caching is enabled on the appserver, the portlet cache holds the complete output of the portlet by portlet state. As a result, the portal server calls the portlet's service or render methods when the user changes the portlet state.

To enable local caching, check the Enable Servlet Caching option in the administrative console for the appserver. See the WebSphere Application Server Information Center for detailed instructions.

The portlet indicates how long, in seconds, its output should be cached in the portlet deployment descriptor.

JSR 168 cache settings


   <expiration-cache>300</expiration-cache>

IBM cache settings


  <cache>
    <expires>300</expires>
    <shared>no</shared>
  </cache>

A value of -1 indicates that the portlet cache never expires. A value of 0 indicates that the portlet is never cached, which is also the behavior if the portlet descriptor does not provide cache settings. The IBM portlet deployment descriptor also includes the <shared/> element, which specifies whether caching is performed only for shared content or per user. Typically, a shared cache is content that is available for unauthenticated users or for a group of users. The descriptor for JSR 168 portlets does not include an element that corresponds to <shared/>.

 

Modifying the local cache at runtime

For JSR 168, the portlet window can modify the expiration time at runtime by setting the EXPIRATION_CACHE property in the RenderResponse, as follows:


RenderResponse.setProperty(
    PortletResponse.EXPIRATION_CACHE,
    (new Integer(3000)).toString() );

The getLastModified() method of the IBM portlet API enables the portlet developer to inform the container when the current cache entry for the portlet should be invalidated, and therefore the portlet's content should be refreshed. The following example shows how a portlet that caches its output can change its content immediately to provide additional output. In the portlet's getLastModified() method, the time stamp is set to an attribute in the portlet's session.

getLastModified() example

    public long getLastModified PortletRequest(request) {

        PortletSession session = request.getPortletSession(false);

        if(session == null) {
            return System.currentTimeMillis();
        }
        if (session != null) {
            Long lastModified = (Long) session.getAttribute(LAST_MODIFIED);
            if (lastModified != null) {
                return lastModified.longValue();
            }
        }
        return -1;
    }

 

Remote cache settings

In an environment where a remote proxy server is used for caching, JSR 168 compliant portlets can indicate cache settings, which are used by the portal server to determine how the page is cached on a remote proxy server. See Caching shared pages by multiple users for more information on how remote caching is determined for the page. IBM portlets cannot provide individual cache preferences for the remote cache.

JSR 168 compliant portlets specify how long, in seconds, portlet output should be cached using the <expiration-cache/> element of the portlet deployment descriptor. However, additional settings must also be provided using a deployment descriptor extension (ibm-portlet-portal-ext.xmi). The portlet indicates whether the remote cache is SHARED or NON_SHARED using the <remote-cache-scope/> element in the extension file. The following example shows the format of the extension file with the cache preferences indicated in bold.


<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://www.ibm.com/xml/ns/portlet/portlet-app_1_0_ext.xsd" 
             version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.ibm.com/xml/ns/portlet/portlet-app_1_0_ext.xsd 
                                 http://www.ibm.com/xml/ns/portlet/portlet-app_1_0_ext.xsd" >
  <anonymous-session>true</anonymous-session>
  <!-- The href must match the portlet name in the portlet.xml file-->
  <portlet href="PORTLET_NAME_FROM_PORTLET_XML">
    <remote-cache-scope>SHARED</remote-cache-scope>
    <remote-cache-dynamic>true</remote-cache-dynamic>
  </portlet>
</portlet-app>

The <anonymous-session/> tag allows a JSR 168 compliant portlet to get a persistent HttpSession on an unauthenticated page. If this tag is missing, the session object is lost after each request. If this tag is present and set to true, the portlet will get an HttpSession that is persistent over multiple requests.

 

Modifying the remote cache at runtime

The portlet window can modify the expiration time and scope at runtime.


  String paramExpiry = "3000";
  String paramScope = "SHARED";
  renderResponse.setProperty( RemoteCacheInfo.KEY_SCOPE, paramScope );
  renderResponse.setProperty( RenderResponse.EXPIRATION_CACHE, paramExpiry );

If the portlet window does not modify cache settings at runtime, then the portlet definition should include this setting in the ibm-portlet-portal-ext.xmi file.


  <remote-cache-dynamic>false</remote-cache-dynamic>

This setting optimizes performance, informing the portlet container that it does not need to wait for the portlet window to publish remote cache information. The default behavior if this setting is not specified is true, indicating that the portlet window does publish remote cache information.

 

See also

Home |

 

WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.

 

IBM is a trademark of the IBM Corporation in the United States, other countries, or both.