The SpEL Resolve Method Results Cache

Results from the SpEL resolve method change infrequently. Data Hub therefore supports caching of resolve method results, offering potentially significant performance improvements.

Data Hub has support for SpEL expressions in the extension.xml file. This support allows you to define rules for transforming one item type to another. The Data Hub implementation of SpEL includes the resolve method, which resolves references to other items and their attributes. Data Hub evaluates a resolve expression multiple times with the same result under certain conditions, and in certain processing phases.

Resolve method caching is based on the Spring caching abstraction, which allows method-level annotations to trigger the checking, population, and clearing of the cache. EhCache is used as the foundation for the cache itself. The cache adds items when a lookup happens for the first time, and is cleared at the end of each composition or publication action.

All cache operations are specified by annotations, and operate on a cache named spelResolveCache. Default properties for this cache are provided in the following table. Any properties that are not specified here are inherited from the ehcache.xml config file in the classpath. The location of this default file can be changed using the property datahub.caching.configuration.resource.name in your local.properties file.

default cache properties
Property name default value Description
datahub.caching.spelResolveCache.maxEntriesLocalHeap 100000 The maximum number of entries the cache can keep in memory. The cache evicts elements once the limit is reached. See the eviction policy described later.
datahub.caching.spelResolveCache.timeToIdleSeconds 600 The maximum number of seconds an element can exist in the cache without being accessed. The element expires at this limit and is no longer returned from the cache.
datahub.caching.spelResolveCache.timeToLiveSeconds 600 The maximum number of seconds an element can exist in the cache regardless of use. The element expires at this limit and is no longer returned from the cache.
datahub.caching.spelResolveCache.evictionPolicy LFU The policy to use in cases when items are evicted from the cache. Available policies are: Least Recently Used (LRU), Least Frequently Used (LFU), First In First Out (FIFO) configuration

Extending the Resolve Method Cache Configuration

It is possible to override the provided resolve method caching functionality. The override is useful in cases where the default configuration properties provide insufficient control over the caching functionality for your needs.

Context

You extend the resolve caching functionality by overriding the EhCacheFactoryBean with ID spelResolveCache. When overriding the existing EhCacheFactoryBean, be sure to reuse the cacheName spelResolveCache. The Data Hub code references this cacheName in all caching annotations.

You can set all available properties of the org.springframework.cache.ehcache.EhCacheFactoryBean when overriding. One particular interesting property is cacheManager, which references a bean of type EhCacheCacheManager. This bean can point to a redefined EhCacheManagerFactoryBean, which points to a newly generated ehcache.xml config file. Instructions for configuring this file can be found in the EhCache Documentation

Procedure

  1. Override the EhCacheFactoryBean, providing the properties cacheManager and cacheName.
    < alias name ="overrideSpelResolveCache" alias ="spelResolveCache" />
    < bean id ="overrideSpelResolveCache" class ="org.springframework.cache.ehcache.EhCacheFactoryBean" >
    	< property name ="cacheManager" ref ="newCacheManager" />
    	< property name ="cacheName" value ="spelResolveCache" />
    </ bean >
  2. Define the new EhCacheCacheManager.
    < bean id ="newCacheManager" class ="org.springframework.cache.ehcache.EhCacheCacheManager" p :cacheManager-ref ="newEhcache" />
  3. Define the referenced EhCacheManagerFactoryBean.
    < bean id ="newEhcache" class ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p :configLocation ="classpath:different/config/ehcache.xml" p :shared ="true" />
  4. Provide the XML config file.
    < ehcache xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :noNamespaceSchemaLocation ="ehcache.xsd" updateCheck ="true" monitoring ="autodetect" dynamicConfig ="true" name ="spelResolveCache" >
    	< diskStore path ="java.io.tmpdir" />
    	< defaultCache maxEntriesLocalHeap ="10000" eternal ="false" timeToIdleSeconds ="1200" timeToLiveSeconds ="1200" maxEntriesLocalDisk ="10000000" diskExpiryThreadIntervalSeconds ="120" memoryStoreEvictionPolicy ="LRU" />
    </ ehcache >

Extending Caching to Other Custom SpEL Methods

You can implement caching for your own custom SpEL methods that use a MethodResolver and MethodExecutor. This implementation is done in the same way as extending the resolve method.

Procedure

  1. Follow the steps described in Extending the Resolve Method Cache Configuration, and apply these steps to your custom methods.
  2. Add a schematic aspect similar to the following to your spring context. This aspect is required to fill and evict the cache as necessary.
    <!-- define caching behavior -->
    < cache :advice id ="cachingBehavior" cache-manager ="cacheManager" >
    	< cache :caching cache ="ownMethodCacheName" >
    		< cache :cacheable method ="myMethodName" key-generator ="org.springframework.cache.interceptor.SimpleKeyGenerator" />
    	</ cache :caching >
    </ cache :advice >
    
    <!-- define eviction behavior -->
    < cache :advice id ="evictionBehavior" cache-manager ="cacheManager" >
    	< cache :caching cache ="ownMethodCacheName" >
    		< cache :cache-evict all-entries ="true" />
    	</ cache :caching >
    </ cache :advice >
    
    <!-- apply the behavior to all the implementations of CustomerDataService interface -->
    < aop :config >
    	< aop :advisor advice-ref ="cachingBehavior" pointcut ="execution(* com.your.extension.namespace.MethodProvidingClass.myMethodName(..))" />
    	< aop :advisor advice-ref ="evictionBehavior" pointcut ="execution(* com.hybris.datahub.service.WhateverIsRequired.anyMethod(..)" />
    </ aop :config >