We know that the performance is now major factor for any software application and Caching plays a very important role of achieving the HIGH performance level. Now let’s go through the caching architecture & MultiVMPool implementation in Liferay portal along with our custom code. Firstly we will go through the implementation of caching in our custom portlet for caching the results. We have to serialize the objects in order to synchronize the objects for caching.

Liferay has out of the box MultiVMPool cache (JVM Level Cache) and it’s a easy to implement in our custom portlet if we have to save data in the user session & request. Liferay MultiVMPool cache implementation internally uses the Ehcache for all kinds of wrapper services.

 

Let’s go through the below code to implements MultiVMPool cache in liferay 6.1 & 6.2 respectively.

For 6.1 use the below code to implement: -

MultiVMPoolUtil.put("Cache-name", "key" ,"value");

For 6.2 implement the below code if you get the exception to set the key & value: -

MultiVMPoolUtil.getCache("CacheName").put(key, value)

Use the below code to retrieve the cache key value: -

String kv = (String) MultiVMPoolUtil.getCache("Cache-name", "key");

To remove the cache use the below code: -

MultiVMPoolUtil.removeCache(String name);

 

Note:- We can override the default cache settings like timeToLive, timeToIdle by defining in the ext plugin etc.

 

To enable the MultiVMPool in portal then add the below properties in portal-ext.properties file:-

ehcache.multi.vm.config.location=/ehcache/liferay-multi-vm-clustered.xml

Below is the MultiVM pool configuration defined in the xml file: -

 

<ehcache

       dynamicConfig="true"

       monitoring="off"

       name="liferay-multi-vm-clustered"

       updateCheck="false"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="ehcache.xsd"

/>

       <cacheManagerPeerProviderFactory             class="com.liferay.portal.cache.ehcache.LiferayCacheManagerPeerProviderFactory"              properties="portalPropertyKey=ehcache.multi.vm.config.location.peerProviderProperties"

              propertySeparator=","

       />

Liferay provides integration with different caching frameworks like OSCache, EHCache, Terracotta etc. Ehcache is very powerful distributed caching framework and Liferay comes with the Ehcache integration by default.

In liferay the caching is implemented in all the 3 layers and the below diagram shows the implementation: -

 RiiteshBhachawat liferay caching

 

Liferay relies completely on hibernate to do the database access and also liferay code performs complex database queries directly through Entity Cache, Finder Cache.

Hibernate Cache has 2 levels – L1 Cache & L2 Cache.

L1 Cache – It cache the objects retrieved from the current database session.

L2 Cache – Caches objects across database sessions and store the objects (Entity Cache, Query Cache).

In liferay value objects are cached at mainly 3 levels – Entity Cache, Query Cache & hibernate Cache.

Entity cache contains the value object & Query Cache only contains the primary key. By default the Hibernate level2 Cache is disabled in 6.2 and Query cache is responsible to improve performance.

 

Hibernate Cache configuration: - The configuration is defined in the hibernate-clustered.xml file which is mentioned below: -

<cache

              eternal="false"

              maxElementsInMemory="10000"

              name="com.liferay.portal.model.impl.UserImpl"

              overflowToDisk="false"

              timeToIdleSeconds="600"

       >

<cacheEventListenerFactory                class="com.liferay.portal.cache.ehcache.LiferayCacheEventListenerFactory"                     properties="replicatePuts=false,replicateUpdatesViaCopy=false"               propertySeparator=","

              />

              <bootstrapCacheLoaderFactory class="com.liferay.portal.cache.ehcache.LiferayBootstrapCacheLoaderFactory" />

       </cache>

We can enable, disable the Entity & Finder cache by adding the below properties in portal-ext.properties: -

value.object.entity.cache.enabled=true

value.object.finder.cache.enabled=true

 

We can use profiling tool to monitor the Ehcache object utilization by selecting individual cache entries.

In next article i will explain the implementation of the Jconsole to monitor the Ehcache performance in liferay portal.