@cacheable method is always executed and ingoing timeToLiveSeconds
cache-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-driven create-missing-caches="true" cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="shared" value="true" />
</bean>
<bean id="cacheKeyGenerator" class="com.test.StringArgumentCacheKeyGenerator" />
</beans>
pom.xml
<properties>
<jdk.version>1.8</jdk.version>
<spring.framework.version>5.1.6.RELEASE</spring.framework.version>
<spring.security.version>3.2.9.RELEASE</spring.security.version>
<spring.integration.version>5.1.4.RELEASE</spring.integration.version>
</properties>
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.1.3</version>
</dependency>
ehcache.xml
<cache name="myList"
overflowToDisk="false" statistics="true"
eternal="false"
maxElementsInMemory="10000"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LFU" />
Java class: The getDepartmentsList() is called everytime I visit the page that displays the department. I added system out "I am getting the dept" to debug. The timeToLiveSeconds set to 5 min. So I was hoping if I refresh the page within 5 min, I will not see print statement "I am getting the dept".
public class MyClass
{
@Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )
public final Map<String, String> getDepartmentsList(String userid) {
System.out.println("I am getting the dept");
Map<String, String> deptMap = myService.getAuthorizedDept(userid);
return deptMap;
}
}
I am new to spring ehcache and debugging this is not easy. The only way I was able to troubleshoot this by adding system.out. I will appreciate any help/suggestions on how to fix this. Thank you
Update: based on the comments I updated the java class and I am seeing the following:
public class MyClass
{
public final Map<String, String> getDepartmentsList(String userid) {
System.out.println("I am getting the dept" );
Map<String, String> deptMap = myService.getAuthorizedDept(userid);
return deptMap;
}
}
@Service
public class Myservice
{
@Cacheable(cacheName = "myList", keyGeneratorName = "cacheKeyGenerator" )
public Map<String, String> getDepartmentsList(String userid){
}
}
- With updated code as suggested, the method does not call that often but do get calls before the 5 min up. I was just refreshing the browser.
- I am getting the dept 2020/09/30 19:53:43
- I am getting the dept 2020/09/30 19:55:47
based on above, it refreshed in 2 min
- I am getting the dept 2020/09/30 20:10:56
- I am getting the dept 2020/09/30 20:12:57
based on above, it refreshed in 2 min but time is set to 5 min ( timeToLiveSeconds="300")
- Also, If I the close the browser session, it calls the method regardless of if time has expired or not. Very confuse as if the cache is browser based or it is caching on the server. Sorry if my questions are very basic.
Thank you all for your comments so far.