TTL Expiry configuration in ehcache.xml is not working for Ehcache3

4,861 views
Skip to first unread message

Chaitanya Gondela

unread,
Jul 11, 2018, 8:49:37 PM7/11/18
to ehcache-users
  1. What version of Ehcache you are currently using;
  2. Paste the configuration for the Cache/CacheManager you have an issue with;
  3. Add any name and version of other library or framework you use Ehcache with (e.g. Hibernate);
  4. Providing JDK and OS versions maybe useful as well.
I'm using Ehcache 3.5.2 with Spring boot to save a list of users in cache. My intention is to flush out specific cache based upon expiry which we set. 

<cache alias="usersCache">
<key-type>java.lang.String</key-type>
<value-type>com.example.demo.modal.Users</value-type>
<expiry>
<ttl unit="seconds">60</ttl>
    </expiry>
<jsr107:mbeans enable-statistics="true"/>
</cache>
</config>

cache is not expiring after 60 seconds. I'm, using Spring Data JPA version 2.0.3.

Henri Tremblay

unread,
Jul 12, 2018, 12:18:39 AM7/12/18
to ehcach...@googlegroups.com
This is a bit surprising.

Can you give us the java code as well? If it Spring Cache or Hibernate Cache?

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ehcache-users/27b590b4-3678-477b-8222-4f0b482c1a83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Chaitanya Gondela

unread,
Jul 13, 2018, 5:39:47 PM7/13/18
to ehcache-users
This is the java code to retrieve from if exists.

package com.example.demo.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.example.demo.modal.Users;
import com.example.demo.repository.UsersRepository;

@Component
public class UsersCache {
@Autowired
private UsersRepository usersRepository;
@Cacheable(value = "usersCache", key = "#name")
public Users getUser(String name) {
System.out.println("Retreving from database for name" + name);
return usersRepository.findByName(name);

Chaitanya Gondela

unread,
Jul 13, 2018, 5:40:44 PM7/13/18
to ehcache-users
This is the java code to retrieve from cache if exists.

Henri Tremblay

unread,
Jul 14, 2018, 1:07:48 AM7/14/18
to ehcach...@googlegroups.com
You are not using Ehcache. You just think you are.

The current ehcache.xml used isn't complete so should fail.

To have Ehcache to work in Spring Boot, you need 2 things:
  • Specify an ehcache.xml in the application.properties: 
    spring.cache.jcache.config=classpath:ehcache.xml
  • Add a dependency to JSR107
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
<scope>runtime</scope>
</dependency>
Without these two things, Spring Boot won't understand that you want to use Ehcache. So it will use the default cache.

Once this is done, you will see Ehcache fail because you haven't specified a resource pool on your cache. You need something like this:

<cache alias="usersCache">
  <key-type>java.lang.String</key-type>
  <value-type>com.example.demo.modal.Users</value-type>
  <expiry>
  <ttl unit="seconds">60</ttl>
  </expiry>
  <heap unit="entries">2000</heap>
  <jsr107:mbeans enable-statistics="true"/>
  </cache>

--
You received this message because you are subscribed to the Google Groups "ehcache-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ehcache-user...@googlegroups.com.
To post to this group, send email to ehcach...@googlegroups.com.

Chaitanya Gondela

unread,
Jul 14, 2018, 2:39:22 AM7/14/18
to ehcache-users
I already added the config key-value pair in application.properties

Apart from that, I added the dependency you specified and resource pool in ehcache.xml

I tried again but still facing the same issue. Cache is NOT expiring after specified TTL (I tried with 6 seconds).

Below are the dependencies added in pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.1.0</version>
  <scope>runtime</scope>
</dependency>
</dependencies>

Chaitanya Gondela

unread,
Jul 17, 2018, 5:09:52 PM7/17/18
to ehcache-users
Can someone help on this? 

Henri, still I'm facing the issue.

Henri Tremblay

unread,
Jul 18, 2018, 12:08:40 AM7/18/18
to ehcach...@googlegroups.com
My current guess is caching as well.

Try to log in debug to see what is happening.

One thing is sure, Ehcache works perfectly in this case. You can debug in the get() call to see which cache is used and why it doesn't expire.

Message has been deleted

Chaitanya Gondela

unread,
Jul 18, 2018, 1:11:13 PM7/18/18
to ehcache-users
Thanks for your reply.

Yes, it seems NOT using Ehcache. When I was debugging I'm not find anything but when I enable the Ehcache logs using below property, I'm not seeing anything like cache created or record inserted in the cache.

I think we should see something like that right.

Added below property in application.properties to enable ehcache logs.
logging.level.org.ehcache=info

As I'm new to the Ehcache not able to find more details. Maybe if you want I can share the code here if you want to see the actual issue.

Chaitanya Gondela

unread,
Jul 18, 2018, 1:46:30 PM7/18/18
to ehcache-users
https://github.com/gchaitu795/Ehcache3

Above is the GitHub where I committed the sample code which I was trying.

Henri Tremblay

unread,
Jul 19, 2018, 12:26:13 AM7/19/18
to ehcach...@googlegroups.com
You should have started by that.

So... the problem is exactly what I said in first place. You are not using Ehcache. Your current Spring CacheManager is ConcurrentMapCacheManager. (easy to check by doing
@Autowired
private CacheManager cacheManager;
somewhere and looking at the implementation in debug.

Then, I said you need JSR107 in your classpath. It was commented in the pom.xml. If I uncomment it, it starts using Ehcache through JSR107 as expected.

Then it fails because of the wrong path to ehcache.xml. It should be 
spring.cache.jcache.config=classpath:ehcache.xml
And now it works and expires as expected.


Chaitanya Gondela

unread,
Jul 19, 2018, 1:26:40 PM7/19/18
to ehcache-users
Thank you. It's working now.
Reply all
Reply to author
Forward
0 new messages