Overriding Cache

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:

  1. 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.

    Figure 1: Caches configured in Liferay DXP can be examined using JMX tools such as Zulu Mission Control  \(Portal Process → MBean server  → MBean Browser\)

    Figure 1: Caches configured in Liferay DXP can be examined using JMX tools such as Zulu Mission Control (Portal Process → MBean server → MBean Browser)

    Some cache configurations can also be viewed statically in their deployment artifacts or source code.

    • liferay-*-vm.xml files in the Liferay [version] Foundation - Liferay [version] Portal Cache - Impl.lpkg file.

    • module-*-vm.xml files in modules or Liferay LPKG files.

  2. 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.

  3. 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>
    
  4. In the <ehcache/> element, add a <cache/> element and set its name attribute to the name of the cache you’re overriding.

  5. Specify all existing <cache/> element attributes you want to preserve. Hint: view the attributes in an MBean browser, as mentioned earlier.

  6. Add or modify attributes to meet your needs. The <cache/> element attributes are described in the ehcache.xsd and Ehcache documentation.

  7. Deploy the project.

Congratulations! Your cache modification is in effect.

Caching Data

« Introduction to Cache ConfigurationCaching Data »
Was this article helpful?
0 out of 0 found this helpful