Caching is a technique that allows you to transparently store data in temporary storage, and serve all future requests directly from the temporary storage. With the use of caching, we can make the system enormously fast by reducing processing on the application server, the database server, and so on. In Chapter 1, Architectural Best Practices, we talked about the caching support in Liferay Portal. In the previous chapter, we learned various performance-related configuration best practices. Now, in this chapter we will focus on the best caching practices related to the Liferay Portal solution.
By the end of this chapter we will have learned
In Chapter 2, Load Balancing and Clustering Best Practices, we learned about Ehcache replication best practices. We learned about multiple ways to configure Ehcache replication. In this section, we will learn how to provide custom Ehcache configuration to tune the cache according to the needs of the Portal solution using the following steps:
portal-impl.jar
file in the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFlib
directory of liferay-node-01
. Copy the JAR file into some temporary directory./ehcache/liferay-single-vm.xml
, /ehcache/liferay-multi-vm-clustered.xml
, and /ehcache/hibernate-clustered.xml
into the temporary directory:jar xf portal-impl.jarehcache/liferay-single-vm.xmlehcache/liferay-multi-vm-clustered.xmlehcache/hibernate-clustered.xml
ehcache
directory and extract three XML files. Now rename the ehcache
directory to custom-ehcache
.custom-ehcache
directory to the node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27webappsROOTWEB-INFclasses
directory of liferay-node-01
.portal-ext.properties
file on liferay-node-01
:net.sf.ehcache.configurationResourceName=/custom-ehcache/hibernate-clustered.xml ehcache.single.vm.config.location=/custom-ehcache/liferay-single-vm.xml ehcache.multi.vm.config.location=/custom-ehcache/liferay-multi-vm-clustered.xml
liferay-node-02
.We copied the existing Ehcache configuration files into our custom folder, and then informed Liferay Portal to use them by providing properties in the portal-ext.properties
file. Before we discuss the purpose of these Ehcache configuration files, it is important to recap some of the basics of the Ehcache framework.
The Ehcache framework uses a component called the cache manager, which controls the life cycle of cached objects. The cache manager contains multiple cache buckets and each cache bucket stores a list of objects. To identify specific objects within a cache bucket, the cache manager stores the objects in the form of a key-value pair.
The following diagram provides a better understanding of object storage within Ehcache. As shown in the diagram, the cache manager has multiple cache buckets. Each bucket is identified by a unique name. Within each cache bucket, we have multiple key-value pairs.
Ehcache provides a way to configure each cache bucket. This can be done by creating an XML configuration file and initializing the cache manager using the same XML configuration file. In this section, we extracted three different Ehcache XML configuration files. Internally, Liferay Portal defines three different cache managers using these configuration files. Let's learn the purpose of these cache managers.
Liferay uses the Hibernate framework for its persistence layer. The Hibernate framework supports two levels of caching:
In the default implementation, Liferay Portal uses the Ehcache framework for a second-level cache. Liferay includes a default Ehcache configuration file for a Hibernate-specific cache manager. It allows for providing a custom configuration file by adding the net.sf.ehcache.configurationResourceName
property in portal-ext.properties
. In the previous section, we extracted the hibernate-clustered.xml
file from Liferay Portal and then configured Liferay to use this external file. In a clustered setup, this cache manager must be configured for cache replication.
This cache manager is specifically used for caching those resources that do not require cache replication in a clustered environment. Such resources are static in nature, and it is OK to maintain a separate cache on each clustered node. One of the examples of such a resource is velocity templates associated with web content. Liferay Portal includes a default configuration file for this cache manager. In the previous section, we configured an external configuration file by adding the ehcache.single.vm.config.location
property in the portal-ext.properties
file.
This cache manager is used to cache those resources that are required to be replicated in a clustered setup. These resources include finder query responses, entities, service responses, and so on. These resources need to be cluster-aware, otherwise it will create functional issues. For example, consider a record of an entity cached on two Liferay Portal nodes. Now on one of the nodes, the same record is updated by specific functionality. The entity cache on the same node will be refreshed. If the cache for the same record on another Liferay node is not refreshed, users connected to that node will refer to an old record from the cache. Hence, the cache of such resources needs to be cluster-aware so that changes in the cache are reflected on all the nodes of a cluster. As this cache manager is cluster-aware, it is called a multi-VM pool. The Liferay bundle includes the default configuration file for this cache manager. Liferay Portal allows for providing a custom configuration file through the ehcache.multi.vm.config.location
configuration property. In the previous section, we have a specified, external Ehcache configuration file by adding this property in portal-ext.properties
.
18.216.190.167