spring/ spring-boot/ caching/ ehcache/ mbeans

I read through the documentation for ehcache 3 and its a bit confusing in the context of spring cache. My configuration is as follows:

 <?xml version="1.0" encoding="UTF-8"?> <ehcache:config updateCheck="true" monitoring="autodetect" dynamicConfig="true" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ehcache='http://www.ehcache.org/v3' xmlns:jsr107='http://www.ehcache.org/v3/jsr107' xsi:schemaLocation=" http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd"> <ehcache:annotation-driven /> <ehcache:service> <service> <jsr107:defaults enable-management="true" enable-statistics="true"/> </service> </ehcache:service> <ehcache:cache alias="xyz" statistics="true"> <ehcache:key-type>java.lang.Long</ehcache:key-type> <ehcache:value-type>abcsomething</ehcache:value-type> <ehcache:expiry> <ehcache:ttl unit="seconds">10</ehcache:ttl> </ehcache:expiry> <ehcache:resources> <ehcache:heap unit="entries">10000</ehcache:heap> <ehcache:offheap unit="MB">1</ehcache:offheap> </ehcache:resources> <jsr107:mbeans enable-statistics="true"/> </ehcache:cache> ... </ehcache:config> 

  cache: jcache: config: classpath*:ehcache.xml 

My yaml:

  cache: jcache: config: classpath*:ehcache.xml 

A bit lost as to what I should be looking for - I thought org.ehcache can be profiled and would show up. I dont see anything with that pattern in jvisualvm. Or not sure how to read the information. Used to be straightforward in ehcache 2.x Any help would be appreciated. I would like to get the size and count of the cache. Number of elements currently in the cache etc.

在此处输入图片说明

To be sure how to answer would need the spring configuration and some Java code. However, your current ehcache.xml seems a weird medley of Ehcache 2 and 3. And the yaml is wrong.

So, in order, you need a pom.xml including JSR-107

    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
    </dependency>

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

The, the application.yml should have a spring prefix.

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

For the cache to be enabled, you need something like @EnableCaching or <cache:annotation-driven/> which should be in the Spring configuration, not in the ehcache.xml .

Finally, I've cleaned up everything that shouldn't be in the ehcache.xml . I'm also using a default namespace to make it more readable. Also, since Spring caches are untyped, you need to remove the key-type and value-type from the cache configuration.

<?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/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd
  http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd">

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

  <cache alias="xyz">
    <expiry>
      <ttl unit="seconds">10</ttl>
    </expiry>
    <resources>
      <heap unit="entries">10000</heap>
      <offheap unit="MB">1</offheap>
    </resources>
    <jsr107:mbeans enable-statistics="true"/>
  </cache>

</config>

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Spring Boot, caching with EhCache How to enable ehcache statistics on spring xml file Spring Boot does not initialize Ehcache shutdown ehcache on a spring boot application Ehcache not working with Spring boot application Using ehcache 3 with Spring Annotations (not using Spring Boot) Hibernate statistics in spring boot not working? EhCache3 + Spring Boot 2 without XML timetoliveseconds ehcache spring boot config is not working Make Ehcache TTL timeout configurable with Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM