Bypassing the @Cacheable annotation in Production for debugging

spring cacheable annotation
spring boot cache auto refresh
spring cacheable key
spring boot cache example mkyong
spring cache list of objects
cacheevict
spring cache ttl
spring load cache on startup

I'm working on a new Spring based project and I was wondering, if i were to use the @Cacheable annotation found in Spring 3.1 or in the ehcache-spring-annotation project, how would you go about implementing a mechanism for bypassing the cache mechanism "on demand", e.g. for production debugging.

Let's say my application serves only REST requests, on occasion, i would like, for a single call (that i manually make) to get data directly from the "DB" (or what ever backend there is) and avoid and any caching on the way (any caching). While the call is running, I don't really mind if it re-populates the cache with what it just got from the db...

I would like to hear your thoughts on how you would go about implementing this.

Thanks in advance for any guidance,

Ken.


Perhaps you can extend the cache manager and provide a boolean expression in addition to the existing checks.

That boolean you can set in a ThreadLocal in your controller, so that whenever the service is invoked, the boolean is in place.

A Guide To Caching in Spring, To enable caching, Spring makes good use of annotations, much like the overhead of looking up entries in a large cache by removing stale  In addition to Lee Chee Kiam: Here is my solution for small projects with only marginal usage of bypassing (not annotated) method calls. The DAO is simply injected into itself as a proxy and calls it's own methods using that proxy instead of a simple method call. So @Cacheable is considered without doing complicated insturmentation.


You can create a method that flushes the cache and run it only with your debugger when you want a value from the DB.

@Cacheable(modelId = "i18NTextCacheModel")
public List<I18NText> loadAll() {

    logger.debug("loadAll called");
    ...
}

@CacheFlush(modelId = "i18NTextFlushModel")
public Long save(I18NText entity) {

    logger.debug("save called");
    ...
}

Spring + Hibernate + EhCache Caching, Learn about cache configuration, cache operations, cache statistics, and which will detect cache annotations like @Cacheable, @CachePut, and To better see what's happening, we can turn on debug-level logging for Hibernate: removing only specific or removing all items from the cache region. You can also use the @Caching annotation to combine multiple cache related annotations on a single method. Avoid combining @Cacheable and @CachePut on the same method, as the behavior can be quite


Arrived at a similar result as suggested by @Bozho If the request parameter refresh-cache=true is passed, the cache gets bypassed

I extended DefaultRedisCacheWriter and used it as the CacheWriter

    RedisCacheManager cacheManager = RedisCacheManager
            .builder(new RefreshableRedisCacheWriter(redisConnectionFactory()))
            .cacheDefaults(cacheConfiguration)
            .build();

Overwrite the get() method in this writer

@Override
public byte[] get(String name, byte[] key) {

    Assert.notNull(name, "Name must not be null!");
    Assert.notNull(key, "Key must not be null!");

    HttpServletRequest request = ApiRequestContext.getRequestContext();
    if (Boolean.valueOf(request.getParameter("refresh-cache"))) {
        logger.info("bypassing cache for key: {}", key);
        return null;
    }

    return execute(name, connection -> connection.get(key));
}

ApiRequestContext is a custom request object here stored in the thread local. Note that when this method returns null, the value is fetched from the db and put back into the cache, effectively refreshing the cache.

Spring cache annotations: some tips & tricks, In this post I'll assume you know how the caching annotations work and However, it is also easy to lose track of the actual cache key that is being used. debug issues that pop up when combining caching with transactions. 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.


Using the Build Cache, This guide covers the different use cases for Gradle's build cache, from local-only There are for example annotation processors that do not clean up stale files in the as they can depend on a variety of inputs outside of the test and production code. This can be fixed by simply disabling Crashlytics in “debug” builds. He quickly applied @Cacheable just to discover that the code performed worse then it used to. He got rid of the annotation and implemented caching himself manually, using good old java.util


Using Ehcache 3 in Spring Boot, All you need to do is activate caching support via Annotation in production, you should choose one of the above cache providers. 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,


How to Size Caches, Introduction; Cache Configuration Sizing Attributes; Pooling Resources Versus The maximum number of bytes a cache can use in off-heap memory, or, when set at the This annotation is not inherited, and must be added to any subclasses that Removing the common factor of 'eviction' which is the same in all caches  @Cacheable annotations has 3 attributes. Value : is the cache name and it is mandatory, in example it is “messagecache” Key: based on this data will be cached and it is optional; Condition: based on the condition data will be cached. In example if the id < 10 then only data will be cached otherwise won’t. it is optional @CacheEvict