首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有xml的情况下使用spring boot2和ehcache 3?

如何在没有xml的情况下使用spring boot2和ehcache 3?
EN

Stack Overflow用户
提问于 2019-09-12 22:45:02
回答 2查看 6.8K关注 0票数 6

目前,我有以下配置:

代码语言:javascript
复制
@Configuration
@EnableCaching
public class EhcacheConfig {
    @Bean
    public CacheManager cacheManager() throws URISyntaxException {
        return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(
                getClass().getResource("/ehcache.xml").toURI(),
                getClass().getClassLoader()
        ));
    }
}

它指的是以下XML:

代码语言:javascript
复制
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="
            http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

    <cache alias="pow_cache">
        <key-type>org.springframework.cache.interceptor.SimpleKey</key-type>
        <value-type>java.lang.Double</value-type>
        <expiry>
            <ttl unit="seconds">15</ttl>
        </expiry>

        <listeners>
            <listener>
                <class>my.pack.CacheEventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
            </listener>
        </listeners>

        <resources>
            <heap unit="entries">2</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache>

</config>

和服务如下所示:

代码语言:javascript
复制
@Cacheable(value = "pow_cache", unless = "#pow==3||#result>100", condition = "#val<5")
public Double pow(int val, int pow) throws InterruptedException {
    System.out.println(String.format("REAL invocation myService.pow(%s, %s)", val, pow));
    Thread.sleep(3000);
    return Math.pow(val, pow);
}

它工作正常,但我想摆脱xml配置。

我已经阅读并尝试应用following answer(最后一段代码),但它只适用于Ehcache2,但我将使用Eehcache3

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-16 22:34:27

作为EhCache seems to be JSR-107 compliant,您需要以这种方式使用它来进行编程配置:

代码语言:javascript
复制
@Bean
public CacheManager cacheManager() throws URISyntaxException {
    CachingProvider provider = Caching.getCachingProvider();  
    CacheManager cacheManager = provider.getCacheManager();   

    CacheConfigurationBuilder<SimpleKey, Double> configuration = 
    CacheConfigurationBuilder.newCacheConfigurationBuilder(org.springframework.cache.interceptor.SimpleKey.class,
        java.lang.Double.class, 
        ResourcePoolsBuilder.heap(2).offheap(10, MemoryUnit.MB))
        .withExpiry(Expirations.timeToLiveExpiration(new Duration(15, TimeUnit.SECONDS)));

    Cache cache = cacheManager.createCache("pow_cache", configuration);
    cache.getRuntimeConfiguration().registerCacheEventListener(listener, EventOrdering.UNORDERED,
        EventFiring.ASYNCHRONOUS, EnumSet.of(EventType.CREATED, EventType.EXPIRED)); 
    return cacheManager;
}

我自己还没有测试过,但这对你来说应该是可行的。

请查看EhCache存储库中的this programatic sample with more configuration optionshow to register listeners programatically上的文档部分。

票数 8
EN

Stack Overflow用户

发布于 2020-09-29 23:46:12

以下是我的解决方案

代码语言:javascript
复制
@Bean
CacheManager getCacheManager() {

    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    CacheConfigurationBuilder<String, String> configurationBuilder =
            CacheConfigurationBuilder.newCacheConfigurationBuilder(
                    String.class, String.class,
                    ResourcePoolsBuilder.heap(1000)
                                        .offheap(25, MemoryUnit.MB))
                                     .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofHours(3)));

    CacheEventListenerConfigurationBuilder asynchronousListener = CacheEventListenerConfigurationBuilder
            .newEventListenerConfiguration(new CacheEventLogger()
                    , EventType.CREATED, EventType.EXPIRED).unordered().asynchronous();

    //create caches we need
    cacheManager.createCache("productCatalogConfig",
                             Eh107Configuration.fromEhcacheCacheConfiguration(configurationBuilder.withService(asynchronousListener)));

    return cacheManager;
}

等同于:

代码语言:javascript
复制
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.ehcache.org/v3"
    xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">

<cache alias="productCatalogConfig">
    <key-type>java.lang.String</key-type>
    <value-type>java.lang.String</value-type>
    <expiry>
        <ttl unit="seconds">1600</ttl>
    </expiry>

    <listeners>
        <listener>
            <class>com.klarna.config.CacheEventLogger</class>
            <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
            <event-ordering-mode>UNORDERED</event-ordering-mode>
            <events-to-fire-on>CREATED</events-to-fire-on>
            <events-to-fire-on>EXPIRED</events-to-fire-on>
        </listener>
    </listeners>

    <resources>
        <heap unit="entries">1000</heap>
        <offheap unit="MB">25</offheap>
    </resources>
</cache>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57909228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档