SAP Commerce(Hybris) Ehcache configuration

Nuray Fahri
2 min readSep 19, 2020

SAP Commerce(Hybris) Cache is a part of the SAP Commerce persistence layer. It improves the performance of a single-server node by reducing the number of database queries. It transparently stores search results, item attributes, and item instances in memory.

SAP Commerce(Hybris) uses EhCache OOTB to support region cache functionality. This enables you to tune a running system and to make sure that certain objects are cached for a longer time, while other objects are removed more quickly due to a limited cache size.

What is EhCache? — Ehcache is an open source, standards-based cache that boosts performance, offloads your database, and simplifies scalability. It’s the most widely-used Java-based cache because it’s robust, proven, full-featured, and integrates with other popular libraries and frameworks.

How to configure cache in your specific extension?

1.Create cache configuration file in YourExtension: hybris/bin/custom/yourExtension/resources/cache/yourextension-ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false" monitoring="autodetect"
dynamicConfig="true">
<!--
see ehcache-core-*.jar/ehcache-failsafe.xml for description of elements
-->
<diskStore path="java.io.tmpdir/yourextension_cache"/>
<cache name="productCache"
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToLiveSeconds="60"
diskPersistent="false"
maxElementsOnDisk="2000"
memoryStoreEvictionPolicy="LRU"/></ehcache>

2. Create cache configuration file in YourExtension : hybris/bin/custom/myCoreExtension/resources/cache/yourExtension-spring-cache.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:utils="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

<cache:annotation-driven cache-manager="compositeYourCacheManager" key-generator="commerceCacheKeyGenerator"/>
<alias name="defaultYourCacheManagerList" alias="yourManagerList"/>
<utils:list id="defaultYourExtensionCacheManagerList">
<ref bean="defaultYourCacheManager"/>
</utils:list>
<alias name="defaultCompositeYourCacheManager" alias="compositeYourCacheManager"/>
<bean id="defaultCompositeWSCacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<ref bean="yourCacheManagerList"/>
</property>
</bean>
<alias name="defaultYourCacheManager" alias="yourCacheManager"/>
<bean id="defaultYourCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="yourEhcache"/>
</bean>
<alias name="defaultYourEhcache" alias="yourEhcache"/>
<bean id="defaultYourEhcache" class="de.hybris.platform.webservicescommons.cache.TenantAwareEhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/cache/yourextension-ehcache.xml"/>
</bean></beans>

3. Change your Java methods with @Cacheable annotation. So, that response from Java Method is cached.

@Cacheable(value = "productCache", key = "T(de.hybris.platform.commercewebservicescommons.cache.CommerceCacheKeyGenerator).generateKey(true,true,#productCode)")public ProductWsDTO getProductByCode(@PathVariable final String productCode)

More you can learn here: https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1808/en-US/576988a1ca9e42e3a86877ab2c5bcc49.html

Make sure you give this post a clap and follow my blog if you find it helpful.

--

--