Spring 4+ with Ehcache 3 – how to

Once a year, the main project I am working at gets upgraded to use newer versions of its dependencies. Nothing special, just keeping up with the open source industry. Briefly, the project is a Java web application, build upon the Spring Framework. This post will describe how we “managed” to increase the version of Ehcache from 2 to 3, while on Spring Framework 4.3.5 (no Spring Boot). I quoted managed, because what was supposed to be an easy normal task proved to be pretty difficult one.

The caching in the application is implemented using Spring caching abstraction with the specific annotations (@Cachable, @CachePut, @CacheEvict etc.). The aim was to keep the current implementation and just to upgrade the versions and prepare for future improvements (JSR-107 etc.).

For a better understanding, I will first present the current configuration (Ehcache 2) and then the new one (Ehcache 3).

pom.xml

<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.4.7</version> <type>pom</type> </dependency> 

Application Context

<cache:annotation-driven cache-manager="cacheManager" mode="proxy" proxy-target-class="false" /> <bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager"> <property name="cacheManagers"> <list> <ref bean="ehCacheManager"/> </list> </property> <property name="fallbackToNoOpCache" value="true"/> </bean> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager"> <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" p:shared="true" /> </property> </bean> 

Cached service method

@Cacheable(value=”rolesCache”, key="#id") public Role find(Long id) { … } 

ehcache.xml

<?xml version='1.0' encoding='UTF-8'?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir"/> <defaultCache eternal="true" maxElementsInMemory="10000" overflowToDisk="false" /> <cache name="rolesCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" /> </ehcache> 

While trying to switch to Ehcache 3, we took a look at the Spring documentation, thought it was pretty straightforward, but unfortunately, it wasn’t that simple. Below it is the configuration that worked for us.

pom.xml

<dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <scope>runtime</scope> <version>3.2.0</version> </dependency> 

One good thing about this updgrade, is the fact that from now on, the application is not compile-time dependent on Ehcache – the scope is not runtime.

Application Context

<cache:annotation-driven cache-manager="ehCacheManager" /> <bean id="ehCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager"> <property name="cacheManager"> <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean" p:cacheManagerUri="classpath:ehcache.xml" /> </property> </bean> 

Although the configuration is simple enough it took a while to make it work.

Cached service method

@Cacheable(value=”rolesCache”, key="#id") public Role find(Long id) { … } 

Just as intended, the implementation inside the code remained the same.

ehcache.xml

<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.ehcache.org/v3' xmlns:jsr107='http://www.ehcache.org/v3/jsr107' xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd"> <cache-template name="tiny-cache"> <expiry> <none/> </expiry> <resources> <heap unit="entries">10</heap> </resources> <cache alias="rolesCache" uses-template="small-cache" /> </config> 

The configuration is completely different from the previous version.

Hopefully, this solution will help others as well, until Spring will brush up the docs and make it a little bit more clearer how this can be configured.


Filed under: Uncategorized

Despre ZTB.ro

ZTB.ro este un agregator românesc de bloguri care colectează și afișează articole din diverse domenii, oferind vizibilitate bloggerilor și o platformă centralizată pentru cititori. Articolele sunt preluate prin feed-uri RSS/Atom și direcționează traficul către blogurile originale.

Articole recente