【问题标题】:Ehcache3 defaultCache-configuration equivalentEhcache3 defaultCache-configuration 等效
【发布时间】:2020-04-03 09:29:47
【问题描述】:

在早期版本的 Ehcache 中,您可以定义默认缓存配置,该配置将应用于以编程方式创建的缓存。自 Ehcache3 以来,他们对配置 xsd 进行了很多更改,似乎不再有与 defaultCache 等效的东西了——或者至少,我在任何地方都找不到它。

例如旧配置:

<ehcache
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd"
    updateCheck="false">

    <defaultCache 
        maxElementsInMemory="10000" 
        eternal="false"
        timeToLiveSeconds="120"
        overflowToDisk="false" />
</ehcache>

我尝试了以下配置,但我不知道这是否有效,或者我是否可以删除并忽略它。我想我可以忽略它,因为当我启动应用程序时,我没有收到任何警告说 ehcache 找不到特定缓存的配置。

新配置:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

    <cache alias="default">
        <expiry>
            <ttl>120</ttl>
        </expiry>
        <heap>10000</heap>
    </cache>
</config>

我之所以使用alias="default" 是因为在本文档中:ehcache.xml 他们提到 defaultCache 的内部名称是“default”。引用:

Default Cache configuration. These settings will be applied to caches
created programmatically using CacheManager.add(String cacheName).
This element is optional, and using CacheManager.add(String cacheName) when
its not present will throw CacheException

The defaultCache has an implicit name "default" which is a reserved cache name.

所以问题是,Ehcache3 中是否有 defaultCache 的等价物,或者我可以直接删除它吗?

【问题讨论】:

    标签: java ehcache-3


    【解决方案1】:

    我使用&lt;cache alias="default"&gt; 尝试了您的配置,但它不适用于 Ehcache 3.x。 Ehcache 仍然使用随机默认配置为丢失的缓存创建新的缓存。

    我发现他们在 Ehcache 3.x 中删除了 defaultCache 的功能。但是,Ehcache 3.x JSR-107 provider 的一个特性及其配置允许与defaultCache 相同的功能。您可以查看here 了解更多信息。

    下面是我的 Spring Boot 配置,它允许使用 Ehcache 3.x 创建缺少缓存的默认配置:

    pom.xml

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    

    application.properties

    spring.jpa.properties.hibernate.cache.use_second_level_cache = true
    spring.jpa.properties.hibernate.cache.use_query_cache        = true
    spring.jpa.properties.hibernate.cache.region.factory_class   = org.hibernate.cache.jcache.JCacheRegionFactory
    spring.jpa.properties.hibernate.javax.cache.provider         = org.ehcache.jsr107.EhcacheCachingProvider
    spring.jpa.properties.hibernate.javax.cache.uri              = classpath:ehcache.xml
    spring.jpa.properties.javax.persistence.sharedCache.mode     = ENABLE_SELECTIVE
    spring.jpa.properties.hibernate.generate_statistics          = true
    

    src/main/resources/ehcache.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'>
        
        <service>
            <jsr107:defaults default-template="myDefault">
            </jsr107:defaults>
        </service>
    
        <cache-template name="myDefault">
            <key-type>java.lang.Object</key-type>
            <value-type>java.lang.Object</value-type>
            <expiry>
                <ttl unit="seconds">300</ttl>
            </expiry>
            <resources>
                <heap unit="entries">1000</heap>
                <offheap unit="MB">10</offheap>
            </resources>
        </cache-template>
    </config>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2013-06-17
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多