4
votes

I have a simple spring 3.2 web application, which is connected to a MySQL db. My question is simple: I have method in dao, which is annotated with @Cacheable. Is there a way to log if the method goes to db, or its result is loaded from cache? For example, I'd like to see the following log:

Object with id 'x' was retrieved from database at 23:44:30 / 2015....
Object with id 'x' was retrived from cache at...

Thank you

3

3 Answers

4
votes

Spring logs out cache hits and misses under the org.springframework.cache category at TRACE level.

1
votes

you can a log message in your service layer if there's going to be a call to your persistence layer. here's a method from some code i'm working where i'm doing this. I only get a log entry when the cache isn't hit.

@Cacheable(value = CACHE_PICO)
@Transactional(readOnly = true)
@PostAuthorize(PICO_READ + OR + ALLOWED_FOR_ADMIN)
public Pico get(long id) {
    log.info("cached missed for pico {}", id);
    return _picoRepository.findOne(id);
}
-1
votes

You could use log4jdbc (docu) - it is wrapper around your normal jdbc connection that logs every executed statement.