We learned about the basics of Ehcache and also different cache managers used in Liferay Portal. Now let's focus on the cache manager configuration to tune the caching mechanism in Liferay. Using the cache manager configuration, we can provide a cache control parameter for each and every cache bucket. Let's understand the important configuration parameters by looking at the defaultCache
and cache
elements of the Liferay multi-VM cache manager configuration file.
<defaultCache eternal="false" maxElementsInMemory="10000" 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" /> </defaultCache> <cacheeternal="false" maxElementsInMemory="10000" name="com.liferay.portlet.calendar.service.impl.CalEventLocalUtil" 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>
As shown in the preceding code snippet, we have defined one cache bucket with a name com.liferay.portlet.calendar.service.impl.CalEventLocalUtil
. This cache bucket stores the Calendar-portlet-related service responses in the cache. We have defined various cache control attributes in the cache
tag. Some of the attributes are related to cache replication. Let's understand the important cache control attributes of the cache
tag:
eternal
: This attribute indicates whether the objects placed in the specific cache can expire or not. If it is set to true
, objects in the cache will never expire. It overrides the value of the timeToIdleSeconds
attribute. We configured its value to false
as we need to make sure cached objects are removed if they are not used.maxElementsInMemory
: This attribute is important to size in-memory cache. It defines the maximum number of objects that can be stored in RAM. Once the number of objects in the cache bucket reaches this number, the cache manager removes the least recently used (LRU) object from the cache, if the overflowToDisk
attribute is set to false
.timeToIdleSeconds
: This attribute defines the time for which an object can be in the cache without utilization. For example, the value of this parameter is set to 3600 for one of the cache buckets, and there is an object in the cache bucket which has not been accessed in the last hour. In this situation, such an object will be removed from the cache bucket. This attribute is also very important from the point of view of performance.overflowToDisk
: This flag indicates whether to move cached objects to the filesystem when the number of objects in-memory exceeds the limit. Internally, the cache manager uses serialization and deserialization to read and write objects on the filesystem.It is not mandatory to define a cache bucket in the configuration file. The Ehcache framework also allows for creating new cache buckets programmatically. In that situation, a cache bucket is created with the default cache control attributes. The default cache control attributes are provided by the defaultCache
element in the configuration file. In the previous snippet, we have set the same cache control attributes in the defaultCache
element.
We learned about the importance and use of cache control attributes. Let's talk about the best practices associated with them:
overflowToDisk
attribute. If it is enabled, it will generate more IO and will ultimately affect the performance. If the system is expected to have a huge amount of cache, it is a good idea to choose a centralized cache such as Terracotta rather than enabling the overflowToDisk
attribute.eternal
attribute to false
. It is fine to enable this attribute when the number of elements in the cache bucket is low and they are accessed frequently by the Portal.maxElementsInMemory
attribute as per the application need. It has to be calculated properly based on the application need. If the value is low, cache objects are removed frequently from the cache.timeToIdleSeconds
should be properly configured for every cache bucket. If the value is too low, cache objects are frequently removed from the cache. Similarly, if the value is high, the system will occupy memory for unused cached objects.As there are so many cache buckets to tune cache control parameters, we need to first decide which cache buckets are of our interest. This can be decided based on the kind of features that we are using. For example, Portal heavily uses collaboration features. In that situation, some of the important cache buckets could be as follows:
com.liferay.portal.kernel.dao.orm.EntityCache.com.liferay.portlet.blogs.model.impl.BlogsEntryImpl com.liferay.portal.kernel.dao.orm.EntityCache.com.liferay.portlet.wiki.model.impl.WikiPageImpl com.liferay.portal.kernel.dao.orm.EntityCache.com.liferay.portlet.messageboards.model.impl.MBCategoryImpl com.liferay.portal.kernel.dao.orm.EntityCache.com.liferay.portlet.messageboards.model.impl.MBThreadImpl
It is difficult to calculate cache control parameters for all cache buckets in the beginning. Hence, it is recommended to tune them during the load-testing phase. During the load-testing phase, we should monitor cache statistics and then the cache control parameters should be tuned based on the cache statistics result.
3.147.47.166