EhCache3 + Spring 引导 2 没有 XML

EhCache3 + Spring Boot 2 without XML

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

 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 propramatically 创建 ehCache 配置?

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

<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();
    }
}

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