Last active
June 29, 2018 05:13
-
-
Save davetownsend/94bd1cd9165884d9b922 to your computer and use it in GitHub Desktop.
Multiple Ehcache with Spring 4 Java Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.dt.cachetest; | |
import net.sf.ehcache.config.CacheConfiguration; | |
import org.springframework.cache.annotation.CachingConfigurer; | |
import org.springframework.cache.annotation.EnableCaching; | |
import org.springframework.cache.ehcache.EhCacheCacheManager; | |
import org.springframework.cache.interceptor.KeyGenerator; | |
import org.springframework.cache.interceptor.SimpleKeyGenerator; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@EnableCaching | |
@ComponentScan("org.dt.cachetest") | |
public class EhCacheConfig implements CachingConfigurer { | |
final static String CACHE_POLICY = "LRU"; | |
@Bean(destroyMethod = "shutdown") | |
public net.sf.ehcache.CacheManager ehCacheManager() { | |
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration(); | |
config.addCache(cacheConfig("cache1", 500)); | |
config.addCache(cacheConfig("cache2", 500)); | |
return net.sf.ehcache.CacheManager.newInstance(config); | |
} | |
@Bean | |
@Override | |
public org.springframework.cache.CacheManager cacheManager() { | |
return new EhCacheCacheManager(ehCacheManager()); | |
} | |
@Bean | |
@Override | |
public KeyGenerator keyGenerator() { | |
return new SimpleKeyGenerator(); | |
} | |
private CacheConfiguration cacheConfig(String name, long maxEntries) { | |
CacheConfiguration config = new CacheConfiguration(); | |
config.setName(name); | |
config.setMaxEntriesLocalHeap(maxEntries); | |
config.setMemoryStoreEvictionPolicy(CACHE_POLICY); | |
return config; | |
} | |
} | |
Make sure to add the following gradle dependencies: | |
compile "org.springframework:spring-context-support:4.0.6.RELEASE" | |
compile 'net.sf.ehcache:ehcache:2.8.3' | |
If you use maven you'll just have figure it out. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java.lang.IllegalArgumentException: CacheManager is required
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.cache.interceptor.AbstractCacheResolver.afterPropertiesSet(AbstractCacheResolver.java:68) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 73 common frames omitted