Application Caching Using Ehcache
Ehcache is an open source caching system that handles application caching.
Advantages
Using Ehcache gives Elastic Path the following benefits:
- JMX Support
- Unified run-time configuration for application and OpenJPA L2 caches
- Improved cache visibility and control
Default Configuration
Default Configuration Files
Elastic Path provides the following sample Ehcache configuration files in the extensions/database/ext-data
module:
environments/default/files/conf/cache/ehcache-5-seconds.xml
for use in testing, authoring and demo environmentsenvironments/default/files/conf/cache/ehcache-60-minutes.xml
for use in live environmentsenvironments/local/files/conf/cache/ehcache-local.xml
for use in development
For development use, you need to copy configuration files to your ${user.home}/ep/conf
directory as described in Updating Configuration Files.
Application Caches
The application level caches sit above the data access layer and are used by Elastic Path to cache longer lived application data. Their default configuration is defined either through Spring or the Elastic Path settings framework. See Application Data Caching a for a list of caches.
OpenJPA Caches
Ehcache integrates with OpenJPA to provide caches for performance enhancement.
Default Cache
The default Ehcache OpenJPA cache configuration is defined in ehcache.xml
in the commerce-engine/core/openjpa-osgi-wrapper
module.
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="1"
timeToLiveSeconds="1"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="true"
/>
L2 Data Cache
A L2 data cache is created for each domain entity with a cache name of the fully qualified class name (e.g. com.elasticpath.domain.catalog.impl.ProductImpl
). You can configure the Ehcache DB entities by creating a new cache element in ehcache.xml
and specifying its properties. If no explicit configuration is provided for a domain entity, then the defaultCache
attribute is used.
Query Cache
The query cache saves OpenJPA query resultes. It is disabled by default to prevent stale query results during transaction processing, but enabled in the Search server to improve indexing performance.
The JPA query cache is configured by the following element:
<cache name="openjpa-querycache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
statistics="true"
/>
Runtime Configuration
Ehcache configuration can be overridden at run time by providing an external Ehcache configuration file. The location of the configuration file is specified using the ep.external.ehcache.xml.path
property in the ep.properties
file.
The file path in ep.external.ehcache.xml.path
must be absolute. There are three supported formats as shown in the following examples:
${user.home}/ep/conf/cache/ehcache.xml
(Linux and Windows)/ep/conf/cache/ehcache.xml
(Linux) orc:/ep/conf/cache/ehcache.xml
(Windows)file:///ep/conf/cache/ehcache.xml
(Linux) orfile:///c:/ep/conf/cache/ehcache.xml
(Windows)
Additional Slash
The additional slash in file:///
is required by Spring.
An application restart is required for configuration changes to take effect.
Configuration Example
Here is a sample configuration override file:
note
For information on Ehcache’s configurable properties, see Ehcache’s Cache Configuration guide.
<?xml version='1.0' encoding='UTF-8'?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"
name="ep-default-cache"
updateCheck="false">
<!--
Read the Ehcache documentation for help on configuring this file.
The <defaultCache> configuration is used as the default for programmatically created caches.
<cache> entries will not inherit configuration from <defaultCache>.
-->
<defaultCache
timeToIdleSeconds="1"
timeToLiveSeconds="1"
maxEntriesLocalHeap="10000"
eternal="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="true"
/>
<cache name="openjpa-querycache"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
maxEntriesLocalHeap="10000"
eternal="false"
statistics="true"
/>
<!--
Override application cache configuration.
-->
<cache name="baseAmountCache"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
maxEntriesLocalHeap="10000"
statistics="true"
/>
</ehcache>
An example of overriding all caches can be found in the ehcache-5-seconds.xml
file in the extensions/database/ext-data/src/main/resources/environments/default/files/conf/cache/
directory.
Recommended Practices
Every commerce implementation is unique. No one configuration fits all. You will need to discover the configurations that work for your implementation. Keep in mind:
Configure lower cache timeouts in development, QA and authoring environments where users want to see changes quickly and performance isn’t a primary consideration
Configure much higher timeouts for production and performance test live environments
Stress your application while using JMX console to monitor the number of cache requests
This will help you determine if the DB entities needs to be cached and for how long.
Be cautious about increasing L2 cache timeouts
Complex domain object graphs, such a products or categories, can get out of sync when L2 objects are refreshed independently of each other.
Keep conducting performance tests until the optimal results are achieved
Ehcache documentation contains comprehensive information about sizing caches