Cache portlet output
Portlet output can be cached at the local application server or using a remote proxy server.
Local cache settings
If servlet caching is enabled on the application server, 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 application server.
The portlet indicates how long, in seconds, its output should be cached in the portlet deployment descriptor.
- Standard portlet cache settings
<expiration-cache>300</expiration-cache>- IBM portlet 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 standard portlet descriptor does not include an element that corresponds to <shared/>.
- Modifying the local cache at runtime
- For standard portlets, 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.
Figure 1. getLastModified() examplepublic 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, standard 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. Standard 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. Figure 2. Remote cache scope settings in the portlet descriptor extension
<?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 standard 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.
If the portlet window does not modify cache settings at runtime, then the portlet definition can include this setting in the ibm-portlet-portal-ext.xmi file.
- Modifying the remote cache at runtime
- 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 );<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 false.
Related information
- IBM WebSphere Developer Domain - Portal Zone: WebSphere Portal Zone
- Customize the portal