package com.pf.core.cache;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.util.StringUtils;
import javax.annotation.Nonnull;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** Flexible Caffeine cache manager
* Idea: {@see "https://dzone.com/articles/multiple-cache-configurations-with-caffeine-and-sp"}
*
* to be used in cases when you need different settings per cache name
*
*/
@Slf4j
@ConfigurationProperties(prefix = "cache")
public class FlexibleCaffeineCacheManager extends CaffeineCacheManager implements InitializingBean {
/**
* default spring.cache.cache-names settings
* will be overridden with {@link FlexibleCaffeineCacheManager#specs}
*/
@Value("${spring.cache.cache-names:}")
private String springCacheNames;
/**
* default spring.cache.caffeine.spec settings
* will be overridden with {@link FlexibleCaffeineCacheManager#specs}
*/
@Value("${spring.cache.caffeine.spec:}")
private String springCacheSpecs;
/**
* This field could be populated from application.(yaml|properties) file
* All features are listed into {@link CaffeineSpec}
*/
@Getter @Setter
private Map specs = new ConcurrentHashMap<>();
@Getter
private Map> builders = new ConcurrentHashMap<>();
private CacheLoader