com.ideaaedi.extspringcache.support.ExtCacheHelper Maven / Gradle / Ivy
package com.ideaaedi.extspringcache.support;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Weigher;
import com.ideaaedi.extspringcache.annotation.CaffeineOop;
import com.ideaaedi.extspringcache.annotation.ExtCacheableOop;
import com.ideaaedi.extspringcache.caffeine.ExtCaffeineCacheInitParser;
import com.ideaaedi.extspringcache.caffeine.ExtCaffeineCacheManager;
import com.ideaaedi.extspringcache.enums.CaffeineExpireStrategyEnum;
import com.ideaaedi.extspringcache.enums.CaffeineKeyQuoteTypeEnum;
import com.ideaaedi.extspringcache.enums.CaffeineValueQuoteTypeEnum;
import com.ideaaedi.extspringcache.model.ExtCacheCounter;
import com.ideaaedi.extspringcache.model.ViaMethod;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;
import java.time.Duration;
import java.util.Stack;
/**
* 图画里,龙不吟虎不啸, 小小书童可笑可笑
*
* @author JustryDeng
* @since 2020/11/5 21:54:19
*/
@Slf4j
public final class ExtCacheHelper {
/** ext-spring-cache 统一日志前缀 */
public static final String LOG_PREFIX = "|EXT-SPRING-CACHE|";
private ExtCacheHelper() {
throw new UnsupportedOperationException("util-class cannot support create-instance");
}
/**
* 定位到当前ExtCacheableOop对象
*
* @return 当前ExtCacheableOop对象
*/
@Nullable
public static ExtCacheableOop determineExtCacheOop() {
return ExtCacheHelper.determineExtCacheOop(null);
}
/**
* 定位到当前命名空间对应的ExtCacheableOop对象
*
* @param cacheName
* cacheName-命名空间
* @return 对应的ExtCacheableOop对象
*/
@Nullable
public static ExtCacheableOop determineExtCacheOop(@Nullable String cacheName) {
Stack stack = SafeContainer.THREAD_LOCAL_CACHE_COUNTER.get();
if (stack == null || stack.empty()) {
log.warn(LOG_PREFIX + " stack(from thread-local) is empty. curr cacheName is -> {}", cacheName);
return null;
}
ExtCacheCounter counter = stack.peek();
ViaMethod viaMethod = counter.getViaMethod();
ExtCacheableOop extCacheableOop = SafeContainer.viaMethodAndOopMap().get(viaMethod);
if (extCacheableOop == null) {
log.warn(LOG_PREFIX + " extCacheableOop(from thread-local) is null. curr cacheName is -> {}", cacheName);
return null;
}
if (StringUtils.isEmpty(cacheName)) {
return extCacheableOop;
}
// 校验cacheName
String[] cacheNames = extCacheableOop.getCacheNames();
if (!ArrayUtils.contains(cacheNames, cacheName)) {
log.warn(LOG_PREFIX + " cannot found target cacheName in cacheNames. curr cacheName is -> {}, "
+ "curr extCacheableOop is -> {}", cacheName, extCacheableOop);
return null;
}
return extCacheableOop;
}
/**
* 获取Caffeine实例对象
*
* P.S. 这里没有在对值进行运用前进行相关校验, 是因为在{@link ExtCaffeineCacheInitParser#caffeineParserLogic(ExtCacheableOop)}
* 这里就已经完成了相关校验了
*
* @param caffeineOop
* ExtCacheable注解中@Caffeine对应的信息
* @param applicationContext
* spring-context
* @param managerBuilder
* ExtCaffeineCacheManager建造器
* @return Caffeine实例对象
*/
@SuppressWarnings("unchecked")
public static Caffeine