Spring cache logging on @Cacheable hit

spring cache refresh
spring load cache on startup
spring cacheable expire
spring cacheable key
spring cache evict multiple keys
spring cacheable not working
spring boot-starter-cache example
spring cache statistics

Currently I am working with a Spring Cache and the @Cacheable/@CacheEvict annotations.

I would like to get some sort of a console log statement like "INFO: i got those values from the cache, NOT from the host. awesome"

Is there a clean and easy way to do this? We are using slf4j apparently btw, if that is of any interest.

Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cache logger in trace level. So, if you append logs under the org.springframework.cache logger to an appropriate appender, you would have some useful information on, say, the console. If you're using Logback, you could use something like the following in your logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.cache" level="trace">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>

With this configuration, you should see something like following on your console:

Cache entry for key 'Page request [number: 0, size 20, sort: null]' found in cache 'persons'

Spring log if cache is called for @Cacheable, Spring logs out cache hits and misses under the org.springframework.cache category at TRACE level. Spring Cache @Cacheable - not working while calling from another method of the same bean. Spring cache is not working when calling cached method from another method of the same bean. Here is an example to explain my problem in clear way.

And for Spring Boot 2 you can add in your application.properties:

logging.level.org.springframework.cache=TRACE

Spring boot caching tutorial with example, Learn to easily manage application cache from Spring boot caching support. With caching, we store such frequently accessed data in memory to avoid hitting to sleep for 5 Secs.. to simulate backend call. log, where as in subsequent calls​,  getEmployee() will be called and data will be placed in cache. Here id is the Key, if id changes method will be executed and data will be placed in cache with the key. When ever the requests comes for the @Cacheable annotated method, spring will check the key in corresponding cache if the cache has key in it.

You can enable trace level logging.

Eg., in application.properties put 'trace=true'.

Spring logging documentation

Spring Cache Annotation Tips and Tricks, Want some clarity about what Spring caching annotations really do? Here is some For very expensive methods, you want to optimize the cache hits as much as possible. private static final Logger logger = LoggerFactory. For anyone using the Grails Spring Cache plugin, a workaround is described in the documentation. I had this issue on a grails app, but unfortunately the accepted answer seems to be unusable for Grails. The solution is ugly, IMHO, but it works. The example code demonstrates it well:

@Cacheable overhead in Spring, Spring 3.1 introduced great caching abstraction layer. But I want to measure the overhead of caching layer (during cache hit to be specific). First time a method annotated with @Cacheable gets called, it gets executed and it’s return value is stored in Cache using a key [method parameter for instance, ]. Next time, if the method gets called using same key [same parameter for instance], the result is returned directly from Cache, without executing the method.

Cache hit logs?, ehcache-spring-annotations. When I enable debug for both ehcache-spring-​annotations and ehcache itself I was expecting to see cache hit logs in my output,​  Annotation indicating that the result of invoking a method (or all methods in a class) can be cached. Each time an advised method is invoked, caching behavior will be applied, checking whether the method has been already invoked for the given arguments. A sensible default simply uses the method parameters to compute the key, but a SpEL expression can be provided via the key() attribute,

Spring cache annotations: some tips & tricks, Cacheable annotations and synchronization. For very expensive methods, you want to optimize the cache hits as much as possible. When being  The Spring framework provides a simple way to cache the value returned by a method. Basically, you annotate a method with @Cacheable and next time you call it with the same arguments, you get the cached value avoiding re-executing the method’s code. In the example we’re going to use, the Spring cache infrastructure will be backed by Ehcache. These are the basic Maven dependencies we’ll need:

Comments
  • that helped me find the similar config in our project setup. thanks :)