Commit 2fb36563ec1a659497e65cb55c32a33c0224ff4e

Authored by YunaiV
1 parent 51968c4c

🐛 修复 CacheUtils 异步缓存的 ThreadLocal 变量传递

yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java
1 package cn.iocoder.yudao.framework.common.util.cache; 1 package cn.iocoder.yudao.framework.common.util.cache;
2 2
  3 +import com.alibaba.ttl.threadpool.TtlExecutors;
3 import com.google.common.cache.CacheBuilder; 4 import com.google.common.cache.CacheBuilder;
4 import com.google.common.cache.CacheLoader; 5 import com.google.common.cache.CacheLoader;
5 import com.google.common.cache.LoadingCache; 6 import com.google.common.cache.LoadingCache;
6 7
7 import java.time.Duration; 8 import java.time.Duration;
  9 +import java.util.concurrent.Executor;
8 import java.util.concurrent.Executors; 10 import java.util.concurrent.Executors;
9 11
10 /** 12 /**
@@ -15,11 +17,13 @@ import java.util.concurrent.Executors; @@ -15,11 +17,13 @@ import java.util.concurrent.Executors;
15 public class CacheUtils { 17 public class CacheUtils {
16 18
17 public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) { 19 public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
  20 + Executor executor = Executors.newCachedThreadPool( // TODO 芋艿:可能要思考下,未来要不要做成可配置
  21 + TtlExecutors.getDefaultDisableInheritableThreadFactory()); // TTL 保证 ThreadLocal 可以透传
18 return CacheBuilder.newBuilder() 22 return CacheBuilder.newBuilder()
19 // 只阻塞当前数据加载线程,其他线程返回旧值 23 // 只阻塞当前数据加载线程,其他线程返回旧值
20 .refreshAfterWrite(duration) 24 .refreshAfterWrite(duration)
21 // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程 25 // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程
22 - .build(CacheLoader.asyncReloading(loader, Executors.newCachedThreadPool())); // TODO 芋艿:可能要思考下,未来要不要做成可配置 26 + .build(CacheLoader.asyncReloading(loader, executor));
23 } 27 }
24 28
25 } 29 }