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.
NICE
ReplyDeleteReally Useful info
DeleteThis comment has been removed by the author.
ReplyDeleteOne more question : we are using the above code and am experiencing the cache. But the object in cache lives only for 2-3 mins. Please see my cache configuration in liferay-multi-vm.xml:
ReplyDeletecache name="TestCache"
eternal="false"
timeToIdleSeconds="3600"
maxElementsInMemory="10000"
overflowToDisk="true"
maxElementsOnDisk="10000"
timeToLiveSeconds="3600"
/.
Please let me know If I missed any thing in above configuration.
Thanks in advance,
Gopi.