XML-less configuration of EhCache in Spring
@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
@Bean(destroyMethod="shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("myCacheName");
cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
cacheConfiguration.setMaxEntriesLocalHeap(1000);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(cacheConfiguration);
return net.sf.ehcache.CacheManager.newInstance(config);
}
@Bean
@Override
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Bean
@Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver();
}
@Bean
@Override
public CacheErrorHandler errorHandler() {
return new SimpleCacheErrorHandler();
}
}
Alternatively for testing, you can use a simple ConcurrentMapCache running without XML as below.
@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
@Bean
@Override
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<Cache> caches = new ArrayList<Cache>();
caches.add(new ConcurrentMapCache("myCacheName"));
cacheManager.setCaches(caches);
return cacheManager;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Bean
@Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver();
}
@Bean
@Override
public CacheErrorHandler errorHandler() {
return new SimpleCacheErrorHandler();
}
}
Edit: Updated to add shutdown method on underlying cache
Edit: Added configuration for error handler and cache resolver
Edit: changing constructor call on SimpleCacheResolver
which resolves CacheManager must not be null
issue (Spring v5.1+)
@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
public static final String USER_CACHE_INSTANCE = "my-spring-ehcache";
private final CacheManager cacheManager;
private final net.sf.ehcache.CacheManager ehCacheManager;
public CachingConfig() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName(USER_CACHE_INSTANCE);
cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
cacheConfiguration.setMaxEntriesLocalHeap(1000);
net.sf.ehcache.config.Configuration config
= new net.sf.ehcache.config.Configuration();
config.addCache(cacheConfiguration);
this.ehCacheManager = net.sf.ehcache.CacheManager.newInstance(config);
this.cacheManager = new EhCacheCacheManager(ehCacheManager);
}
@Bean(destroyMethod="shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
return ehCacheManager;
}
@Bean
@Override
public CacheManager cacheManager() {
return cacheManager;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Bean
@Override
public CacheResolver cacheResolver() {
return new SimpleCacheResolver(cacheManager);
}
@Bean
@Override
public CacheErrorHandler errorHandler() {
return new SimpleCacheErrorHandler();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…