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

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

package com.github.bootfastconfig.cache.caffeine;


import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.bootfastconfig.cache.*;
import com.github.bootfastconfig.cache.properties.CaffeineCacheProperties;
import com.github.bootfastconfig.springtool.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;

import java.util.Collection;
import java.util.List;

/**
 * @author mister
 */
@ConditionalOnClass(value = {Caffeine.class, CaffeineCacheManager.class})
public class CaffeineConfig {


    @Autowired(required = false)
    private List cacheConfigs;

    @Autowired
    private CaffeineCacheProperties caffeineCacheProperties;

    @Autowired(required = false)
    private AnnotationConfigCacheBuilder annotationConfigCacheBuilder;


    @Bean(CacheBeanNameConstant.CAFFEINE_CACHE_MANAGER)
    @ConditionalOnMissingBean(name = {CacheBeanNameConstant.CAFFEINE_CACHE_MANAGER}, value = CaffeineCacheManager.class)
    public CaffeineCacheManager caffeineCacheManager() {
        CaffeineCacheConfig cacheConfigCollect = initCaffeineCacheConfig();
        MyCaffeineCacheManager cacheManager = new MyCaffeineCacheManager();
        cacheManager.setCaffeine(cacheConfigCollect.getDefaultCache());
        cacheConfigCollect.exportConsumer(cacheManager);
        return cacheManager;
    }


    CaffeineCacheConfig initCaffeineCacheConfig() {
        CaffeineCacheConfig cacheConfigCollect = new CaffeineCacheConfig();
        if (annotationConfigCacheBuilder != null) {
            cacheConfigCollect.addCaches(getCaffeineAnnotationConfigCacheBuilder().get());
        }
        if (cacheConfigs != null) {
            for (CaffeineCacheConfigGather cacheConfig : cacheConfigs) {
                cacheConfigCollect.addDefaultCache(cacheConfig.getDefaultCaffeineConfig());
                cacheConfig.addCaffeineCacheConfig(cacheConfigCollect);
            }
        }
        Collection caffeineCacheGrouping = caffeineCacheProperties.getCaffeineCacheGrouping();
        if (caffeineCacheGrouping != null) {
            caffeineCacheGrouping.forEach(c -> {
                cacheConfigCollect.addCache(c);
            });
        }
        return cacheConfigCollect;
    }


    private AnnotationConfigCacheBuilder> getCaffeineAnnotationConfigCacheBuilder() {

        return this.annotationConfigCacheBuilder.setConversionFactory(o -> {
            CacheSimpleConfig cacheSimpleConfig = (CacheSimpleConfig) o;
            if (cacheSimpleConfig.cacheName() == null || StringUtil.EMPTY.equals(cacheSimpleConfig.cacheName())) {
                return null;
            }
            CaffeineBuilder.CaffeineBuilderBuilder caffeineBuilderBuilder = CaffeineBuilder
                    .builder()
                    .initialCapacity(100 > cacheSimpleConfig.maximumSize() ?
                            new Long(cacheSimpleConfig.maximumSize()).intValue() : 100)
                    .maximumSize(cacheSimpleConfig.maximumSize())
                    .expireAfterWrite(new Duration(cacheSimpleConfig.duration(), cacheSimpleConfig.unit()));
            return caffeineBuilderBuilder.name(cacheSimpleConfig.cacheName()).build();
        });
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy