Tuesday, October 25, 2016

Spring boot ehcache timeToLiveSeconds not being honored

I have a Spring Boot backend application (version 1.3.1.RELEASE) where caching is enabled on certain database calls.  I ran into a problem where the caching eviction time was not being honored.

Steps to enable using ehcache 2.x as cache provider:
  1. @EnableCaching was added to the configuration
  2. ehcache.xml was created on the root of the classpath with configuration for the cache
  3. <cache name="dataToCache" maxEntriesLocalHeap="7" timeToLiveSeconds="600">
    </cache>
  4. Added following annotation to the database Database service method
    @Cacheable(cacheNames = "dataToCache", key = "#root.methodName + '-' + #dataToCache")
  5. Manually tested if caching was working by setting spring.jpa.show-sql to true in application.properties.  And called the api that would
    call the service.  Only the first time an actual call to the database was made.  This was good.  I assumed all was well.
Problem:
I was expecting after 10 mins (the timeToLiveSeconds attribute was set to 600 seconds) the dataToCache cache would be evicted and new data that was added within that ten minutes would now show up.  Nope.

My problem was that I assumed caching was working since the first time an actual call to the database was made (and logged).  I incorrectly assumed caching was working with ehcache 2.x because I had added ehcache.xml to my classpath.  Simple caching was being used because "If no caching library is present in the application, simple caching is used by default".

Logs show this:
2016-08-17 09:46:15.250  INFO 4512 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration' of type [class org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration$$EnhancerBySpringCGLIB$$79234830] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

Solution:
I had inadvertently forgotten to add the ehcache jar file to the classpath!!!    After the ehcache jar was added to classpath, my timeToLiveSeconds was being honored, and now the logs verify the following:
2016-08-17 10:13:10.876  INFO 14056 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration' of type [class org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration$$EnhancerBySpringCGLIB$$5d32863] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-08-17 10:13:11.883  INFO 14056 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'ehCacheCacheManager' of type [class net.sf.ehcache.CacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-08-17 10:13:11.905  INFO 14056 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'cacheManager' of type [class org.springframework.cache.ehcache.EhCacheCacheManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

No comments:

Post a Comment

I appreciate your time in leaving a comment!