Liferay DXP pre-configures cache for service entities, service entity finder results, and cache for several other classes. You can tune existing cache to meet your needs. For example, it may help to write cache overflow elements to disk, increase the maximum number of cached elements, or make other adjustments. Using a module and only one XML file, you can override cache configurations dynamically.
Here is how to override a cache configuration:
-
Identify the name of the cache you want to override. Existing cache configurations and statistics (hit/miss counts and percentages) can be examined at runtime through JMX. Using a tool that supports JMX analysis, you can examine Liferay DXP’s cache configurations in the MBean of
net.sf.ehcache
. Please note that the caches listed in the MBean are more than what Liferay DXP’s cache configuration files specify because some caches are created purely through Java code.Some cache configurations can also be viewed statically in their deployment artifacts or source code.
-
liferay-*-vm.xml
files in theLiferay [version] Foundation - Liferay [version] Portal Cache - Impl.lpkg
file. -
module-*-vm.xml
files in modules or Liferay LPKG files.
-
-
If you don’t own the existing project that specifies the cache or you want to use a different project to configure the cache, create a module project. Otherwise, edit the cache in the existing project. These instructions demonstrate adding the cache configuration to a new module project.
-
In the
src/main/resources/META-INF
folder, add an XML file for the type of cache (multi-VM or single-VM) you’re overriding.module-multi-vm.xml
file:<ehcache dynamicConfig="true" monitoring="off" name="module-multi-vm" updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd" > <!-- cache elements go here --> </ehcache>
module-single-vm.xml
file:<ehcache dynamicConfig="true" monitoring="off" name="module-single-vm" updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd" > <!-- cache elements go here --> </ehcache>
-
In the
<ehcache/>
element, add a<cache/>
element and set itsname
attribute to the name of the cache you’re overriding. -
Specify all existing
<cache/>
element attributes you want to preserve. Hint: view the attributes in an MBean browser, as mentioned earlier. -
Add or modify attributes to meet your needs. The
<cache/>
element attributes are described in the ehcache.xsd and Ehcache documentation.
Congratulations! Your cache modification is in effect.