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

com.github.bootfastconfig.cache.caffeine.CaffeineCacheConfig Maven / Gradle / Ivy

Go to download

Parent pom providing dependency and plugin management for applications built with Maven

The newest version!
package com.github.bootfastconfig.cache.caffeine;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.bootfastconfig.cache.CacheBuilder;
import com.github.bootfastconfig.cache.CacheConfig;
import org.springframework.cache.caffeine.CaffeineCache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class CaffeineCacheConfig implements CacheConfig> {


    private final List> caffeineCacheList;

    private Caffeine cacheBuilder;


    public CaffeineCacheConfig() {
        this.caffeineCacheList = new ArrayList<>();
    }


    @Override
    public void importCacheBuilder(CacheConfig> cacheConfig) {
        Collection> cacheBuilder = cacheConfig.getCacheBuilder();
        addCaches(cacheBuilder);

    }

    @Override
    public Collection> getCacheBuilder() {
        return caffeineCacheList;
    }

    @Override
    public void addCache(CacheBuilder cache) {
        caffeineCacheList.add(cache);
    }

    @Override
    public void addCaches(Collection> caches) {
        caffeineCacheList.addAll(caches);
    }


    public void addDefaultCache(Caffeine cacheBuilder) {
        if (this.cacheBuilder == null) {
            this.cacheBuilder = cacheBuilder;
        }

    }

    public Caffeine getDefaultCache() {
        if (this.cacheBuilder == null) {
            Caffeine objectObjectCaffeine = Caffeine.newBuilder();
            objectObjectCaffeine.initialCapacity(100);
            objectObjectCaffeine.maximumSize(300L);
            objectObjectCaffeine.expireAfterAccess(120, TimeUnit.SECONDS);
            this.cacheBuilder = objectObjectCaffeine;
        }
        return cacheBuilder;
    }

    @Override
    public void exportConsumer(Consumer> cacheManager) {
        caffeineCacheList.forEach(cacheManager);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy