Using EhCache in Spring 4 without XML

后端 未结 2 1526
执念已碎
执念已碎 2020-11-29 19:23

Is there a way to initialize EhCache without xml in either Spring 4 or with Spring Boot?

I noticed Spring Boot 1.0.0.RC3 doesn\'t have any ehcache dependencies but t

2条回答
  •  我在风中等你
    2020-11-29 19:35

    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 caches = new ArrayList();
            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();
        }
    }
    

提交回复
热议问题