Skip to content
Snippets Groups Projects
Commit a41b83a2 authored by Stephane Nicoll's avatar Stephane Nicoll
Browse files

Upgrade to Ehcache 3

I saw on twitter the reference of an article (in french):
http://javaetmoi.com/2016/08/migrer-vers-spring-boot/

That article concludes with something along the lines of  "Besides the
EhCache and Maven configuration, Petclinic does not hold a single line
of XML anymore".

Looking at the code, we can remove more XML if you want. This PR migrates
the cache infrastructure to EhCache 3 and JCache (JSR-107). This also
reduces the number of dependencies.
parent 86458070
No related branches found
No related tags found
No related merge requests found
......@@ -59,10 +59,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
......@@ -74,18 +70,6 @@
<artifactId>jstl</artifactId>
</dependency>
<!-- used for EhCacheCacheManager -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Date and Time -->
<dependency>
<groupId>joda-time</groupId>
......@@ -123,14 +107,12 @@
<!-- EhCache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Dandelion -->
......
package org.springframework.samples.petclinic.config;
import java.util.concurrent.TimeUnit;
import javax.cache.CacheManager;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.ehcache.jsr107.Eh107Configuration;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
......@@ -11,4 +24,21 @@ import org.springframework.context.annotation.Profile;
@EnableCaching
@Profile("production")
public class CacheConfig {
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
return new JCacheManagerCustomizer() {
@Override
public void customize(CacheManager cacheManager) {
CacheConfiguration<Object, Object> config = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(100, EntryUnit.ENTRIES))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(60, TimeUnit.SECONDS)))
.build();
cacheManager.createCache("vets", Eh107Configuration.fromEhcacheCacheConfiguration(config));
}
};
}
}
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir"/>
<!-- objects are evicted from the cache every 60 seconds -->
<cache name="vets"
timeToLiveSeconds="60"
maxElementsInMemory="100"
eternal="false"
overflowToDisk="false"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment