Total Pageviews

Tuesday, March 27, 2012

Liferay Cache: a key to improve performance


The main concern of this article is to present performance enhancing information using Liferay cache.
Liferay has out of box Cache MultiVMPool, which we can implement in our portlet's code for caching of results. It becomes necessary if we are using heavy web services.
MultiVMPool Cache is quite easy to implement even over the clustered environment specially if you're frank with handing objects saved in request and session, its quite similar. When I needed it for implementation, I didn't find anything relevant and that is why I need to contribute this article.


Let's know MultiVMPool Cache !!!
MultiVMPool Cache is an Out of Box Cache provided by Liferay and so such kind of complex server and bean configuration is not needed to change for its implementation :  No additional infrastructure configuration  needed. It uses the Liferay's EHChache for wrapper services. It can be implemented in clustered environment easily and used cross WARs also (Because its a JVM level Cache).


Implementation
Putting some value in Cache
There are several other overriden methods also, but most likable is to use the following:
MultiVMPoolUtil.put("Cache-name", "key" ,"value");
NOTE: New cache is automatically created if doesn't exits.


Retriving Value
String value = (String) MultiVMPoolUtil.get("Cache-name", "key");
Returns the value of "key" form "cache-name" cache.


Removing one key
MultiVMPoolUtil.remove("Cache-name", "key");
Removing one key form a cache.


Clearing Cache
MultiVMPoolUtil.clear("Cache-name");
Clear the cache named "cache-name".


Caching over Clustered environment
Uncomment the following in a clustered environment form portal-ext.properties.
    ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml

Don't forget to serialize objects so that caching can be synchronized for other network connections also.


Hope this will help you.


Ref. docs.liferay.com

10 comments:

  1. Very nice article Apoorva. Thank you!

    I believe, using MultiVMPoolUtil, we cannot control the cache_time_out. For this, we need to use the EHCache directly? As I see in another post of yours. (http://www.apoorvaprakash.in/2012/05/liferay-cache-ehcache.html)

    Regards,
    Gágà

    ReplyDelete
  2. Hi Apoorva,

    Thanks for the post. I want to cache the web service response in my Liferay Plugin portlet. But its not caching the result and its not giving any error also.
    I think I am missing some configurations like configuring "liferay-multi-vm-clustered.xml" or "liferay-single-vm.xml" in "portal-ext.properties". Please help me on that.

    ReplyDelete
    Replies
    1. Hi Vani, this code can be used for custom caching... The file you mentioned controls internal caching of Liferay, which is done by ehcache. HTH.

      Delete
  3. Hi Apoorva,

    Thanks for the information.I have implemented the cache in my custom portlet but i am not able to get cache in my another class.In method1 i used put cache and in method2 i tried to get cache.Could you please provide any example here?

    ReplyDelete
  4. Nice Article. I had implemented catche in liferay 6.1. Is there any way to implement same in liferay 6.2? because There are no MultiVMPoolUtil.put() method. Please help me I am planning to migrate 6.1 to 6.2. Thanks

    ReplyDelete
    Replies
    1. Hi Tushar,
      I hope this link can help you: http://www.apoorvaprakash.in/2012/05/liferay-cache-ehcache.html
      HTH.

      Delete