0

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>a.b.c.something</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.

enter image description here

sharman
  • 515
  • 1
  • 5
  • 18

1 Answers1

1

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>
Henri
  • 5,551
  • 1
  • 22
  • 29