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

com.ideaaedi.extspringcache.caffeine.ExtCaffeineCacheManager Maven / Gradle / Ivy

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

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.support.ExtCacheHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ext-caffeine 缓存管理器
 * 

* * @author JustryDeng * @since 2020/11/8 14:46:28 */ public class ExtCaffeineCacheManager extends CaffeineCacheManager { private boolean dynamic = true; private final Map customCacheMap = new ConcurrentHashMap<>(16); /** * 创建Cache时,{@link CaffeineCacheManager#createCaffeineCache(String)} 会调用此方法的, 重写了此方法就相当于重写了createCaffeineCache。 *

* TIPS. 低版本的spring-boot里面,是没有此方法的,那时就需要重写{@link CaffeineCacheManager#createCaffeineCache(String)}了 */ @NonNull @Override protected Cache adaptCaffeineCache(@Nullable String name, @Nullable com.github.benmanes.caffeine.cache.Cache cache) { Assert.notNull(name, "Name must not be null"); Assert.notNull(cache, "Cache must not be null"); return new ExtCaffeineCache(name, cache, isAllowNullValues()); } @Override public Cache getCache(@Nullable String name) { Assert.notNull(name, "Name must not be null"); ExtCacheableOop oop = ExtCacheHelper.determineExtCacheOop(name); if (oop == null) { return this.dynamic ? createCaffeineCache(name) : null; } Cache cache = this.customCacheMap.get(oop); if (cache == null) { cache = this.dynamic ? createCaffeineCache(name) : null; if (cache != null) { this.customCacheMap.put(oop, cache); } } return cache; } @Override public void setCacheNames(@Nullable Collection cacheNames) { this.dynamic = cacheNames == null; super.setCacheNames(cacheNames); } /** * 参考{@link org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration}进行改造 */ @Slf4j @SuppressWarnings({"unused", "UnusedReturnValue"}) public static class ExtCaffeineCacheManagerBuilder { private final Caffeine caffeine; private final CacheLoader cacheLoader; private final List cacheNames; private boolean allowNullValues = true; private Caffeine customCaffeine; private CacheLoader customCacheLoader; private ExtCaffeineCacheManagerBuilder(CacheProperties cacheProperties, Caffeine caffeine, CacheLoader cacheLoader) { if (cacheProperties == null || CollectionUtils.isEmpty(cacheProperties.getCacheNames())) { cacheNames = new ArrayList<>(0); } else { cacheNames = cacheProperties.getCacheNames(); } this.caffeine = caffeine; this.cacheLoader = cacheLoader; } public static ExtCaffeineCacheManagerBuilder builder(CacheProperties cacheProperties, ObjectProvider> caffeineProvider, ObjectProvider caffeineSpecProvider, ObjectProvider> cacheLoaderProvider) { Caffeine caffeine = null; if (cacheProperties != null && cacheProperties.getCaffeine() != null && StringUtils.hasText(cacheProperties.getCaffeine().getSpec())) { caffeine = Caffeine.from(cacheProperties.getCaffeine().getSpec()); } else if (caffeineSpecProvider.getIfAvailable() != null) { caffeine = Caffeine.from(caffeineSpecProvider.getIfAvailable()); } else if (caffeineProvider.getIfAvailable() != null) { caffeine = caffeineProvider.getIfAvailable(); } else { log.trace(ExtCacheHelper.LOG_PREFIX + " cannot found any spring-env-config for caffeine-cache"); } return new ExtCaffeineCacheManagerBuilder(cacheProperties, caffeine, cacheLoaderProvider.getIfAvailable()); } public ExtCaffeineCacheManager build() { ExtCaffeineCacheManager cacheManager = new ExtCaffeineCacheManager(); // 如果为空,就为空。不必为了兜底而选择用从容器中取出来的Caffeine // cacheManager.setCaffeine(customCaffeine == null ? caffeine : customCaffeine); cacheManager.setCaffeine(customCaffeine); // 如果为空,就为空。不必为了兜底而选择用从容器中取出来的CacheLoader // cacheManager.setCacheLoader(customCacheLoader == null ? cacheLoader : customCacheLoader); cacheManager.setCacheLoader(customCacheLoader); cacheManager.setAllowNullValues(allowNullValues); if (!CollectionUtils.isEmpty(cacheNames)) { cacheManager.setCacheNames(cacheNames); } return cacheManager; } /** * 定制 命名空间 */ public ExtCaffeineCacheManagerBuilder customAppendCacheNames(Set cacheNameSet, boolean cleanExisted) { if (cleanExisted) { cacheNames.clear(); } cacheNames.addAll(cacheNameSet); return this; } /** * 定制 allowNullValues */ public ExtCaffeineCacheManagerBuilder custom(boolean allowNullValues) { this.allowNullValues = allowNullValues; return this; } /** * 定制 Caffeine */ public ExtCaffeineCacheManagerBuilder custom(Caffeine caffeine) { this.customCaffeine = caffeine; return this; } /** * 定制CacheLoader */ public ExtCaffeineCacheManagerBuilder custom(CacheLoader cacheLoader) { this.customCacheLoader = cacheLoader; return this; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy