Friday, February 26, 2016

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 :

Wednesday, December 2, 2015

Install EE plugin in development machine

Liferay marketplace is great tool, you can sell your app, download free apps or purchase EE apps. There are also many great free EE apps. You can download CE apps using your browser. But when you search EE apps in browser, you see EE Only button and there is a message that it Available to EE subscribers. Find out how to get a subscription.


Many developers don't know how to download free EE plugin so I thought I should write a blog about it.
Prerequisites: EE portal, valid EE License, Admin credentials of you portal, Liferay.com credentials.
For free EE plugins, you need to login with admin credentials in your portal. Click on Control Panel. Now go to Purchased menu in App section. It will ask you to login on Liferay Market place, use you Liferay.com credentials. After login it may ask for update your marketplace app. So update it first.

Now you will see two or three tabs, click on EE tab. It will show all free EE plugins. You can install plugin from there.
Enjoy !!!

Thursday, October 17, 2013

Disable velocimacro caching in liferay

Liferay developer face VM macro caching problem in development stage. Velocity use velocimacro.library.autoreload property to handle it. You can disable velocimacro caching in Liferay by adding following properties:

Liferay 6.2 : 

You need to update "velocity.engine.resource.modification.check.interval" property in portal-ext.properties

       #
       # Set this to 0 to load template source every time. Set this to a number
       # less than 0 to load templates once and cache it indefinitely. Set this to
       # a number greater than 0 to load templates and cache it for the period of
       # time specified by that number in milliseconds seconds.
       #

       velocity.engine.resource.modification.check.interval=0



Liferay 6.1 :


You need to update "velocity.engine.resource.manager.cache.enabled=false" property in portal-ext.properties

      velocity.engine.resource.manager.cache.enabled=false


Note: This feature is intended for development, not for production.