package com.sequenceiq.periscope.config; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurer; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.SimpleCacheErrorHandler; import org.springframework.cache.interceptor.SimpleCacheResolver; import org.springframework.cache.interceptor.SimpleKeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import net.sf.ehcache.config.CacheConfiguration; @Configuration @EnableCaching @EnableAutoConfiguration public class CachingConfig implements CachingConfigurer { private static final int MAX_ENTRIES = 1000; @Bean public net.sf.ehcache.CacheManager ehCacheManager() { net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration(); config.addCache(createUserCache()); return net.sf.ehcache.CacheManager.newInstance(config); } @Bean @Override public CacheManager cacheManager() { return new EhCacheCacheManager(ehCacheManager()); } @Override public CacheResolver cacheResolver() { return new SimpleCacheResolver(cacheManager()); } @Bean @Override public KeyGenerator keyGenerator() { return new SimpleKeyGenerator(); } @Override public CacheErrorHandler errorHandler() { return new SimpleCacheErrorHandler(); } private CacheConfiguration createUserCache() { CacheConfiguration cacheConfiguration = new CacheConfiguration(); cacheConfiguration.setName("userCache"); cacheConfiguration.setMemoryStoreEvictionPolicy("LRU"); cacheConfiguration.setMaxEntriesLocalHeap(MAX_ENTRIES); return cacheConfiguration; } }