Liferay Caching - Service Level Caching using WebCachePool

                Caching is important part of any web application. We implement caching for our application on different levels. Generally we cache static resource on web server level and DB level caching on persistence level. Most of web-framework/portal provide some level of caching. Liferay strongly integrated caching in its core part and it also provide mechanism that user can optimize caching according to their requirement.

    Liferay provide many level caching like CacheFilter, MultiVMPool/SingleVMPool, Hibernate caching.

  • Frontend Caching: Liferay cache HTML page in CacheFilter. It check if request is cachable or not. If yes, it cache the HTML data. Liferay can also cache portlet fragment.
  • Service Level Caching: Liferay provide ability to cache data on service level. SingleVMPool cache use for cache data on a single server node. MultiVMPool is used for cache data across multiple nodes.
  • Persistence Caching: Liferay cache data in persistence level like it Hibernate caching, SB finder cache etc.

    You can find more detail on Jorge Ferrer's blog.

                In Service level caching, Liferay provides WebCachePool which internally uses SingleVMPool. It hides the implementation details of getting the data and caching it at same time. WebCachePoolImpl provides get(String key, WebCacheItem wci) method and it check if provided key has already cached data or not. So developer only need to focus on their logic; getting data from cache, adding caching and refresh cache data handle by WebCachePool.

                To use get(String key, WebCacheItem wci) method, developer need to pass two parameter. First param is key. It is unique key for cache. For e.g. Liferay cache RSS feeds in RSS-Portlet. In RSS Portlet, feed URL is key. Second Parameter is object of implementation class of WebCacheItem interface. It fetch actual data from DB or webservice when it required. Like in RSS portlet, convert(key) method of RSSWebCacheItem call when portlet need first time feeds for a key(URL). It is also called when cache is expired.

 

 

 

                WebCachePool is easy to use. Only one class need to write. Developer don't need to know how to cache data against unique key. WebCachePool manage it. We can define refresh time of each WebCacheItem. So if there are two WebCacheItem in your portlet, they may have different refresh time. Liferay use WebCachePool in many portlet like Translator Portlet, Amazon Ranking Portlet, RSS portlet, Currency Converter Portlet etc. In Translator Portlet, Liferay use Microsoft Translator services and it cache translated output against the input key for 90 days (translation doesn't change usually).

 

Using WebCachePool in Custom Portlet:

                First you need to create a class with implement WebCacheItem interface. You need to override convert(String key) and getRefreshTime() method.

 

 

Now use this in you controller or service like this:

 

If you like to remove cached data of any key, you can do like:

 

If you want to clear all cache in WebCachePool, you can do like :

 

Blogs
Nicely put and well explained.
Thanks for sharing this valuable info.
Thanks for the explanation.
I have one small confusion on when to use SingleVMPoolUtil and when to use WebCachePoolUtil?

Thanks for sharing. Very well explained. I also need cache information in the header. How to put that information in the header.