【修复】微信支付时,无法保证父线程的 ThreadLocal 传入子线程

This commit is contained in:
YunaiV 2024-03-09 11:03:17 +08:00
parent 1f35ef59d1
commit d30700d060

View File

@ -7,6 +7,7 @@ import com.google.common.cache.LoadingCache;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
/** /**
@ -17,8 +18,11 @@ import java.util.concurrent.Executors;
public class CacheUtils { public class CacheUtils {
public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) { public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
Executor executor = Executors.newCachedThreadPool( // TODO 芋艿可能要思考下未来要不要做成可配置 // 1. 使用 TTL 包装 ExecutorService实现 ThreadLocal 的透传
TtlExecutors.getDefaultDisableInheritableThreadFactory()); // TTL 保证 ThreadLocal 可以透传 // https://github.com/YunaiV/ruoyi-vue-pro/issues/432
ExecutorService executorService = Executors.newCachedThreadPool(); // TODO 芋艿可能要思考下未来要不要做成可配置
Executor executor = TtlExecutors.getTtlExecutorService(executorService);
// 2. 创建 Guava LoadingCache
return CacheBuilder.newBuilder() return CacheBuilder.newBuilder()
// 只阻塞当前数据加载线程其他线程返回旧值 // 只阻塞当前数据加载线程其他线程返回旧值
.refreshAfterWrite(duration) .refreshAfterWrite(duration)