-
-
Save SivaVr/cd62c728982ef411532ebb33892b8187 to your computer and use it in GitHub Desktop.
Multiple Ehcache with Spring 4 Java Config
This file contains hidden or 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