How to temporarily disable caching for Spring cache
Asked Answered
M

2

11

I have a spring bean annotated with @Cacheable annotations defined like so

@Service
public class MyCacheableBeanImpl implements MyCacheableBean {
    @Override
    @Cacheable(value = "cachedData")
    public List<Data> getData() { ... }
}

I need this class to be capable of disabling caching and working only with data from original source. This should happen based on some event from the outside. Here's my approach to this:

@Service
public class MyCacheableBeanImpl implements MyCacheableBean, ApplicationListener<CacheSwitchEvent> {
    //Field with public getter to use it in Cacheable condition expression
    private boolean cacheEnabled = true;

    @Override
    @Cacheable(value = "cachedData", condition = "#root.target.cacheEnabled") //exression to check whether we want to use cache or not
    public List<Data> getData() { ... }

    @Override
    public void onApplicationEvent(CacheSwitchEvent event) {
        // Updating field from application event. Very schematically just to give you the idea
        this.cacheEnabled = event.isCacheEnabled();
    }

    public boolean isCacheEnabled() {
        return cacheEnabled;
    }

}

My concern is that the level of "magic" in this approach is very high. I'm not even sure how I can test that this would work (based on spring documentation this should work but how to be sure). Am I doing it right? If I'm wrong then how to make it right?

Mccarthy answered 10/3, 2016 at 9:23 Comment(2)
see here : spring-boot-how-to-disable-cachable-during-developmentRateable
@StefanIsele-prefabware.com this will only activate/deactivate caching on context start up. The behaviour I want to achieve is that caching may be disabled at any time based on some external event.Mccarthy
S
10

What I was looking for was NoOpCacheManager:

To make it work I switched from xml bean creation to a factory

I did something as follows:

    @Bean
public CacheManager cacheManager() {
    final CacheManager cacheManager;        
    if (this.methodCacheManager != null) {
        final EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
        ehCacheCacheManager.setCacheManager(this.methodCacheManager);
        cacheManager = ehCacheCacheManager;
    } else {
        cacheManager = new NoOpCacheManager();
    }

    return cacheManager;
}
Skilful answered 11/1, 2017 at 8:45 Comment(2)
Nice. This is not exactly what a asked for because again, you're creating a cache manager based only on startup conditions and I need to switch cache on and off at runtime. But this gave me an idea, that I can create my own Cache Manager delegate that will chose between NoOp and ordinary CacheManager based on some volatile state in it. That way Spring annotations will continue to work and I get control on how cache proxy will behave.Mccarthy
oh, thank you for using NoOpCacheManager class. i'm very happy for see this class :))). please highlight this class in your answer for better finding.Monaural
S
3

Inspired by SimY4 last comment, here is my working solution overloading SimpleCacheManager in order to provide runtime switch. Just use switchableSimpleCacheManager.setEnabeld(false/true) to switch off/on.

package ch.hcuge.dpi.lab.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.support.NoOpCache;
import org.springframework.cache.support.SimpleCacheManager;

/**
* Extends {@link SimpleCacheManager} to allow to disable caching at runtime
*/
public class SwitchableSimpleCacheManager extends SimpleCacheManager {

  private boolean enabled = true;

  public boolean isEnabled() {
      return enabled;
  }

/**
 * If the enabled value changes, all caches are cleared
 *
 * @param enabled true or false
 */
public void setEnabled(boolean enabled) {
    if (enabled != this.enabled) {
        clearCaches();
    }
    this.enabled = enabled;
}

@Override
public Cache getCache(String name) {
    if (enabled) {
        return super.getCache(name);
    } else {
        return new NoOpCache(name);
    }
}

protected void clearCaches() {
    this.loadCaches().forEach(cache -> cache.clear());
}

}

Configuration ( using Caffeine ):

@Bean
public SwitchableSimpleCacheManager cacheManager() {
    SwitchableSimpleCacheManager cacheManager = new SwitchableSimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(
            buildCache(RESULT_CACHE, 24, 5000)
    ));
    return cacheManager;
}

private CaffeineCache buildCache(String name, int hoursToExpire, long maxSize) {
    return new CaffeineCache(
            name,
            Caffeine.newBuilder()
                    .expireAfterWrite(hoursToExpire, TimeUnit.HOURS)
                    .maximumSize(maxSize)
                    .build()
    );
}
Superstratum answered 12/1, 2021 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.