如何在没有xml的情况下使用Spring Boot 2和ehcache 3? [英] How to use spring boot 2 and ehcache 3 without xml?

查看:167
本文介绍了如何在没有xml的情况下使用Spring Boot 2和ehcache 3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有以下配置:

@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:

<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>

服务看起来像这样:

@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配置。

It works properly but I want to get free of xml configuration.

我已阅读并尝试应用以下答案(最后一篇代码),但它仅适用于Ehcache 2,但我将使用Eehcache 3

I've read and tried to apply following answer(last piece of code) But it works only for Ehcache 2 but I am going to use Eehcache 3

如何实现?

推荐答案

作为EhCache 似乎符合JSR-107 ,您需要以这种方式使用它来进行程序配置:

As EhCache seems to be JSR-107 compliant, you'll need to use it this way to have a programatic configuration:

@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;
}

我自己还没有测试过,但这应该对您有用。

Haven't tested it myself, but this should work for you.

查看具有更多配置选项的此程序化示例,来自EhCache存储库和如何以编程方式注册侦听器

Check out this programatic sample with more configuration options from the EhCache repo and the docs part on how to register listeners programatically too.

这篇关于如何在没有xml的情况下使用Spring Boot 2和ehcache 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆