Configuring Ehcache
Configuring 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
Application Caches
The application level caches, which sit above the data access layer, are used by Elastic Path to cache longer lived application data. See Application Data Caching a for a list of caches. Their default configuration is defined either through Spring or the Elastic Path settings framework.
OpenJPA Caches
Ehcache integrates with OpenJPA to provide caches for performance enhancement.
Default Cache
<defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="1" timeToLiveSeconds="1" overflowToDisk="false" maxElementsOnDisk="10000000" diskPersistent="false" 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 element 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.
<cache name="openjpa-querycache" maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" 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.
- Windows: file:///C:/path1/path2/path3/test-ehcache-configuration.xml
- Linux/Mac: file:///path1/path2/path3/production-ehcache-configuration.xml
The additional slash in file:/// is necessary for Spring.
An application restart is required for configuration changes to take effect.
Configuration Sample
Here is a sample configuration file:
For information on Ehcache's configurable properties, see Ehcache's Cache Configuration guide.
<?xml version='1.0' encoding='UTF-8'?>f <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 all caches, including OpenJPA L2 caches. --> <!-- Modified default configuration. --> <defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="150" timeToLiveSeconds="300" overflowToDisk="false" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true" /> <!-- Override ProductImpl JPA L2 cache configuration. --> <cache name="com.elasticpath.domain.catalog.impl.ProductImpl" maxElementsInMemory="2000" timeToIdleSeconds="250" timeToLiveSeconds="500" </cache> <!-- Override product application cache configuration. --> <cache name="productLookupCache" maxElementsInMemory="20000" timeToIdleSeconds="3600" timeToLiveSeconds="7200" </cache> </ehcache>
Recommended Practices
- Don't modify the default configuration which is suitable for development and QA. Instead, configure caching for live and performance test environments with an external configuration file.
- 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.