Refreshing the portlet cache


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 doView() methods when the user changes the portlet state. The getLastModified() method 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. Use WindowListeners to set a new timestamp and then return the timestamp in getLastModified. The following examples show parts of a bookmark portlet that caches its output, but needs to change its content immediately if the window state changes to provide additional output.

First, the application server must be configured to enable session caching. Check the Enable Servlet Caching option in the administrative console for the portal server. See the WebSphere Application Server Information Center for detailed instructions.

Next, the supported portlet states are declared and caching is enabled in portlet.xml.

getLastModified() example: Portlet deployment descriptor



  <cache>
    <expires>-1</expires>
    <shared>NO</shared>
  </cache>
  <allows>
    <maximized/>
    <minimized/>
  </allows>


In the portlet's getLastModified() method, the timestamp is set to an attribute in the portlet's session.

getLastModified() example: WindowListener



    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;
    }


See also