【问题标题】:EhCache3 + Spring Boot 2 without XML没有 XML 的 EhCache3 + Spring Boot 2
【发布时间】:2020-02-10 08:44:14
【问题描述】:

我一直在寻找如何使用 Spring Boot 2 实现 EhCache3 的解决方案。问题是在版本 3 中,他们将包更改为 org.ehcache,我发现的无 xml 配置示例适用于您声明 net.xml 的 2 版本。 sf.ehcache.config。我想使用第三个版本。

 package com.jcg.example.ehcache_no_xml.config;

import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;

@Configuration
public class EhCacheConfig {

    @Bean
    public EhCacheManagerFactoryBean cacheManager() {
        return new EhCacheManagerFactoryBean();
    }

    @Bean
    public EhCacheCacheManager testEhCacheManager() {
        // testEhCache Configuration - create configuration of cache that previous required XML
        CacheConfiguration testEhCacheConfig = new CacheConfiguration()
            .eternal(false)                     // if true, timeouts are ignored
            .timeToIdleSeconds(3)               // time since last accessed before item is marked for removal
            .timeToLiveSeconds(5)               // time since inserted before item is marked for removal
            .maxEntriesLocalHeap(10)            // total items that can be stored in cache
            .memoryStoreEvictionPolicy("LRU")   // eviction policy for when items exceed cache. LRU = Least Recently Used
            .name("testCache");

        Cache testCache = new Cache(testEhCacheConfig);

        cacheManager().getObject().addCache(testCache);
        return new EhCacheCacheManager(cacheManager().getObject());
    }
}

有什么解决方案可以用 spring 来创建 ehCache 配置吗?

【问题讨论】:

    标签: spring spring-boot ehcache-3


    【解决方案1】:

    好的,我会发布我的答案,因为我之前已经解决了这个问题。 我正在使用 Spring Boot 和下一个依赖项:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
      <groupId>javax.cache</groupId>
      <artifactId>cache-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </dependency>
    

    我的缓存配置:

    @Configuration
    @EnableCaching
    public class CacheConfig {
    
        @Bean
        public JCacheCacheManager jCacheCacheManager() {
            return new JCacheCacheManager(cacheManager());
        }
    
        @Bean(value = "cacheManager")
        public CacheManager cacheManager() {
            CachingProvider provider = Caching.getCachingProvider();
            CacheManager cacheManager = provider.getCacheManager();
            List<String> cacheNames = Arrays.asList("myCacheName");
            cacheNames.forEach(cn -> cacheManager.createCache(cn, config()));
            return cacheManager;
        }
    
        private MutableConfiguration<SimpleKey, SiteIdSet> config() {
            return new MutableConfiguration<SimpleKey, SiteIdSet>()
                    .setTypes(SimpleKey.class, ClassWhichShouldBeCached.class)
                    .setStoreByValue(false);
        }
    
    }
    

    在开始时有一个方法可以检索一些值然后缓存它们:

    @Slf4j
    @Service
    @DependsOn("cacheManager")
    public class MyServiceImpl implements MyService {
    
        ommited ...
    
        @Override
        @Cacheable(cacheNames = "myCacheName")
        public Set<Integer> getSmth() {
            return callDb();
        }
    
        @Override
        @CachePut(cacheNames = "myCacheName")
        public Set<Integer> getSmth() {
            return callDb();
        }
    }
    

    我不会描述这些方法的作用,但它们与定义的缓存一起工作,并在需要时填充和更新它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 2019-03-13
      • 2020-01-14
      • 2019-04-29
      相关资源
      最近更新 更多