How to temporarily disable caching for Spring cache

Viewed 9019

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?

2 Answers

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()
    );
}
Related