All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blossomproject.core.cache.BlossomCache Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.blossomproject.core.cache;

import com.github.benmanes.caffeine.cache.Cache;
import java.util.concurrent.Callable;
import org.springframework.cache.caffeine.CaffeineCache;

public class BlossomCache extends CaffeineCache {
  private final CacheConfig configuration;
  private boolean enabled;

  public BlossomCache(String name, CacheConfig configuration, Cache cache) {
    super(name, cache);
    this.configuration = configuration;
    this.enabled = true;
  }

  public BlossomCache(String name, CacheConfig configuration, Cache cache, boolean allowNullValues) {
    super(name, cache, allowNullValues);
    this.configuration = configuration;
    this.enabled = true;
  }

  @Override
  public ValueWrapper get(Object key) {
    if (!enabled) {
      return null;
    }
    return super.get(key);
  }

  @Override
  public  T get(Object key, Class type) {
    if (!enabled) {
      return null;
    }
    return super.get(key, type);
  }

  @Override
  public  T get(Object key, Callable valueLoader) {
    if (!enabled) {
      try {
        return (T) toStoreValue(valueLoader.call());
      }
      catch (Exception ex) {
        throw new ValueRetrievalException(key, valueLoader, ex);
      }
    }
    return super.get(key, valueLoader);
  }

  @Override
  public void put(Object key, Object value) {
    if (!enabled) {
      return;
    }
    super.put(key, value);
  }

  @Override
  public ValueWrapper putIfAbsent(Object key, Object value) {
    if (!enabled) {
      return null;
    }
    return super.putIfAbsent(key, value);
  }


  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    updateCacheState();
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void enable() {
    this.enabled = true;
    updateCacheState();
  }

  public void disable() {
    this.enabled = false;
    updateCacheState();
  }

  private void updateCacheState() {
    if (!enabled) {
      this.clear();
    }
  }

  public CacheConfig getConfiguration() {
    return configuration;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy