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

com.github.anonymousmister.bootfastconfig.cache.caffeine.MyCaffeineCacheManager Maven / Gradle / Ivy

package com.github.anonymousmister.bootfastconfig.cache.caffeine;

import com.github.anonymousmister.bootfastconfig.cache.properties.CaffeineBuilder;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.caffeine.CaffeineCacheManager;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;

/**
 * @author mister
 */
public class MyCaffeineCacheManager extends CaffeineCacheManager {

    private final ConcurrentMap cacheMap = new ConcurrentHashMap<>(16);


    MyCaffeineCacheManager() {

    }

    public void addCaches(Supplier> cacheMapFunctiona) {
        Map caches = cacheMapFunctiona.get();
        if (caches == null) return;
        synchronized (this.cacheMap) {
            cacheMap.putAll(caches);
        }
    }

    public void addCaches(Map caffeineCache) {
        if (caffeineCache == null) return;
        synchronized (this.cacheMap) {
            caffeineCache.forEach((k, v) -> {
                this.cacheMap.put(k, new CaffeineCache(k, v.Builder().build()));
            });
        }
    }

    public void addCaches(Collection caches) {
        if (caches == null) return;
        synchronized (this.cacheMap) {
            this.cacheMap.clear();
            Set cacheNames = new LinkedHashSet<>(caches.size());
            for (Cache cache : caches) {
                String name = cache.getName();
                this.cacheMap.put(name, cache);
                cacheNames.add(name);
            }
        }
    }

    ;


    @Override
    public Cache getCache(String name) {
        Cache cache = this.cacheMap.get(name);
        if (cache == null) {
            return super.getCache(name);
        }
        return cache;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy