Liferay Cache
Leveraging Liferay Cache to Improve performance :
Liferay provides out of box Cache MultiVMPool, which can be used by portlets to cache results of heavy network calls i.e. web services.
MultiVMPool Cache is very easy to use and yet powerful enough to work in clustered environment. Unfortunately there is no official documentation for MultiVMPool, which describes how it works and how it can be leverage by portlets.
What is MultiVMPool Cache ?
-
- Out of Box Cache from Liferay : No additional configuration infrastructure needed.
- Wrapper around EHCache : As powerful as EHCache
- Clustered Cache
- JVM Level Cache: Object cached by one WAR will be available to other WARs on same JVM.
How to Use :-
-
- MutliVMPoolUtil : Utility class provides methods to access MultiVMPool
http://docs.liferay.com/portal/5.1/javadocs/portal-kernel/com/liferay/portal/kernel/cache/MultiVMPoolUtil.html - Common Usage :
Using MultiVMPoolUtil : new cache will be dynamically created if not exits.
Following methods shows basic usage :
name= Cache Name
key=Key of Cache Entry
obj=Object to be Cached.
Put Obeject in Cache
put(java.lang.String name, java.lang.String key, java.io.Serializable obj)
Get Object from Cache
get(java.lang.String name, java.lang.String key)
Clear Cache
clear(java.lang.String name)Remove Object from Cache
remove(java.lang.String name, java.lang.String key) Please note here that Liferay MutliVMPoolUtil provides overloaded method to put Normal object (i.e non serialized) also. But in Clustered environment make sure to use Serialized object which can be copied to other caches over wire.
Clustered Cache needs to enable from portal.properties
ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml
- MutliVMPoolUtil : Utility class provides methods to access MultiVMPool
Pros:
-
- Easy to Use
- No configuration required from portlet
- Clustered Cache
Cons:
-
- Use Default settings for Cache created. No API to set different timeToLive,timeToIdle
settings for the cache. - To change default Cache settings only done using EXT environment.
- Use Default settings for Cache created. No API to set different timeToLive,timeToIdle
Thanks for the post. When I get a chance I will try this out on some of our portlets that query Domino and LDAP resources.
Dave
March 19, 2010 at 11:28 am
Hi Rutvij,
Thanks for posting a wonderful article. It was very helpful.
However, I am facing class cast exception when I am accessing cache in a portlet(in one war) which was created by a portlet (in another war). Do you know what config I am missing?
Thanks!!
Mak
March 8, 2011 at 2:48 pm
Great Thanks
Paras
September 11, 2012 at 4:45 am
Hi Rutvij,
Thanks for the post. I want to cache the web service response in my Liferay Plugin portlet.
I followed the steps mentioned in this post .But its not caching the result and its not even giving any errors.
I have “liferay-multi-vm-clustered.xml” in “\liferay-plugins-sdk-6.1.1\portlets\Custom-portlet\docroot\WEB-INF\src\ehcache” folder . I think my portlet is not able to read “liferay-multi-vm-clustered.xml” file .
Please let me know, if I have to do any other configurations in custom portlet.
-Thanks.
vani
April 5, 2013 at 6:23 am