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

com.ideaaedi.extspringcache.provider.ExtCaffeineCacheManagerProvider Maven / Gradle / Ivy

The newest version!
package com.ideaaedi.extspringcache.provider;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import com.ideaaedi.extspringcache.annotation.ExtCacheableOop;
import com.ideaaedi.extspringcache.caffeine.ExtCaffeineCacheManager;
import com.ideaaedi.extspringcache.properties.ExtSpringCacheProperties;
import com.ideaaedi.extspringcache.support.EmptyObjectProvider;
import com.ideaaedi.extspringcache.support.ExtCacheHelper;
import com.ideaaedi.extspringcache.support.SafeContainer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * redis Caffeine provider, 用于管理所有的 ExtCaffeineCacheManager
 *
 * @author JustryDeng
 * @since 2020/11/8 12:22:26
 */
@Slf4j
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class ExtCaffeineCacheManagerProvider implements CacheManagerProvider {
    
    public static final String BEAN_NAME = "extCaffeineCacheManagerProvider";
    
    @Autowired
    private ExtSpringCacheProperties extSpringCacheProperties;
    
    @Autowired(required = false)
    private CacheProperties cacheProperties;
    
    @Autowired
    private ObjectProvider> caffeineProvider;
    
    @Autowired
    private org.springframework.beans.factory.ObjectProvider caffeineSpecProvider;
    
    @Autowired
    private ObjectProvider> cacheLoaderProvider;
    
    @Autowired
    private ApplicationContext applicationContext;
    
    private final Map caffeineCacheManagerMap = new ConcurrentHashMap<>(8);
    
    @Override
    @Nullable
    public CacheManager provide(ExtCacheableOop oop) {
        return caffeineCacheManagerMap.get(oop);
    }
    
    /**
     * 为了保留Caffeine原有的对spring-context中个组件/配置的支持,
     * 这里初始化的数据直接从spring-context中拿(, 而不写死)
     * 

* P.S. 如果初始化时,想使自定义的caffeine与spring-context中可能存在的相关caffeine配置不搭噶, * 那么只需要这里builder的时候传null或空的provider即可 */ @Override public void afterSingletonsInstantiated() { // -> 初始化caffeineCacheManagerMap SafeContainer.Data4Caffeine.EXT_CAFFEINE_CONTAINER.forEach(oop -> { ExtCaffeineCacheManager.ExtCaffeineCacheManagerBuilder builder; if (extSpringCacheProperties.getCaffeine().isResponseSpringContext()) { builder = ExtCaffeineCacheManager.ExtCaffeineCacheManagerBuilder .builder(cacheProperties, caffeineProvider, caffeineSpecProvider, cacheLoaderProvider); } else { builder = ExtCaffeineCacheManager.ExtCaffeineCacheManagerBuilder .builder(new CacheProperties(), new EmptyObjectProvider<>(), new EmptyObjectProvider<>(), new EmptyObjectProvider<>()); } ExtCaffeineCacheManager cacheManager = builder.custom( ExtCacheHelper.buildCaffeine(oop.getCaffeine(), applicationContext, builder) ).build(); caffeineCacheManagerMap.put(oop, cacheManager); }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy