Caching portlet output

The Portlet Specification 2.0 as well as Portlet Specification 2.0 define how your portlet can request portlet container to cache its output.

The way it works is if you want portal container to cache output of your portlet then you can add expiration-cache element to your portlet.xml like this.

<expiration-cache>-1</expiration-cache>

Value of expiration cache means this to portal

  1. -1: Value of -1 tells portal server that the output of portlet never expires

  2. 0: Value of 0 is equivalent of not adding the expiration-cache element at all. It means that dont cache the output of the portlet.

  3. positive no.: Value of more than 0 means cache output of portlet for those many mil-seconds



If you set the expiration cache element for portlet then on the first request to portlet the portlet container will pass request to doView() method and cache its output. There after whenever portal server gets request it will return content from cache instead of passing control to doView() method of the portlet. The cache will get expired if either it is expired or you perform some action on the portlet. In that case the doView() method would get called again. Clicking on renderURL in the portlet does not result in doView() method being called again.

Please note that if you go to say either Edit or HELP mode of the portlet then the cache wont be discarded also content generated in doEdit() or doHelp() method does not get cached.

If you have cachemonitor.ear installed on your portal server then you can look at the content that is being cached like this.



I am using the SampleCachePortlet.war Portlet for testing fragment cache. This portlet writes time stamp when output was generated. As you can see the portal server is caching the output generated by portlet for every user under different entry.

You can use Cache Monitor to verify the cached content. This is a sample entry

/wps/PA_SampleCache/SampleCachePortlet:7_6RUKKSU31G8I602VFOAQ3710G3
/wps/PA_SampleCache/SampleCachePortlet:7_6RUKKSU31G8I602VFOAQ3710G3:sunil:text/html:en-AU, en;q=0.9, en-US;q=0.8:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729):UTF-8:normal::UTF-8:requestType=GET
none 7_6RUKKSU31G8I602VFOAQ3710G3:sunil
None 1

As you can see the value of
Template: <urloftheportlet>:<portletinstanceid>
CacheId:<urloftheportlet>:<portletinstanceid>:<username>:<contenttype>
Timeout (seconds): Time when cache expires. It is saying none in our case because we set expiration-cache to -1
Dependency IDs: <portletinstanceid>:<username>

If you try accessing the portlet with different users then you will see one entry for each user. Now if you dont want that instead you want portal to cache output across users. Ex. Company news portlet should display same data to all the users then add value of cache-scope equal to public like this in portlet.xml


<cache-scope>public</cache-scope>


Once you do that portal will keep only one copy of the portlet output across the users, your cache entry will look like this in the cache monitor

/wps/PA_SampleCache/SampleCachePortlet:7_6RUKKSU31G8I602VFOAQ3710G3 /wps/PA_SampleCache/SampleCachePortlet:7_6RUKKSU31G8I602VFOAQ3710G3:public:text/html:en-US, en;q=0.9:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729):UTF-8:normal::UTF-8:requestType=GET none 7_6RUKKSU31G8I602VFOAQ3710G3:public None 1


As you can see the Cache Id does not include userName this time instead it says public same thing with the dependencyId it says <portletinstanceid>:public.

1 comment:

tsfaridi said...

I am facing an issue related to caching - posted on IBM developer works forum:

http://www.ibm.com/developerworks/forums/thread.jspa?threadID=462097