Search code examples
javaspring-bootcaching

Spring - @Cacheable annotation not working on java method


When adding @Cacheable annotation on a method performing a simple Java code, on application execution, breakpoint inside the method is triggered. Does this mean that the result was not cached?

Configuration used:

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=[port]
spring.cache.redis.time-to-live=[some value]

Note that the @Cacheable is working fine in the same project when put before a database method


Solution

  • To make it work, we need to call the method annotated with @Cacheable from a different Java class. Otherwise it doesnt work.