首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ehcache 3和Spring统计

Ehcache 3和Spring统计
EN

Stack Overflow用户
提问于 2019-03-19 08:54:31
回答 1查看 2.4K关注 0票数 4

我正在尝试将目前使用Ehcache 2的Spring项目迁移到最新的Ehcache 3.7。

除了缺少的Spring缓存统计数据之外,一切似乎都很好。

下面是前面的Ehcache 2配置:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="ehcache.xsd"
     updateCheck="false"
     monitoring="on"
     dynamicConfig="true"
     statistics="true">

<cache name="asset"
       maxEntriesLocalHeap="5"
       timeToIdleSeconds="600"
       timeToLiveSeconds="3600"
       memoryStoreEvictionPolicy="LRU"/>

</ehcache>

和新的Ehcache 3配置:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<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.6.xsd
    http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.6.xsd">

<service>
    <jsr107:defaults enable-management="true" enable-statistics="true"/>
</service>

<cache alias="asset">
    <resources>
        <heap unit="entries">5</heap>
    </resources>
    <expiry>
        <ttl unit="hours">1</ttl>
    </expiry>
    <jsr107:mbeans enable-management="true" enable-statistics="true"/>
</cache>

</config>

POM依赖项(仅与缓存管理相关的依赖项):

代码语言:javascript
复制
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.7.0</version>
    </dependency>

弹簧配置:

代码语言:javascript
复制
spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect
        hbm2ddl:
          auto: none
        cache:
          use_second_level_cache: true
          region:
            factory_class: jcache
        javax:
          cache:
            provider: org.ehcache.jsr107.EhcacheCachingProvider
            missing_cache_strategy: fail

我以前用Ehcache 2获取这种统计数据:

但是使用Ehcache 3时,SBA Insight/Details页面和Data/Cache页面上都不会显示任何统计信息。

对于Ehcache 2,它是相当即插即用的,但Ehcache 3的情况似乎并非如此。

有人有线索吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 09:50:11

解决了!愚蠢的错误..。

在spring配置中,而不是:

代码语言:javascript
复制
spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml

我不得不在Ehcache 3中使用这个:

代码语言:javascript
复制
spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

并定义一个bean:

代码语言:javascript
复制
@Bean
public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer(
        JCacheCacheManager cacheManager) {
    return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER,
            cacheManager.getCacheManager());

}

如本文所述:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-configure-hibernate-second-level-caching

现在,缓存通过执行机构和SBA中的JMX得到了很好的报告:)

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55236895

复制
相关文章

相似问题

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