Commit bf5ac4ede2ef8e0ba9736cff74c571d05baad981

Authored by YunaiV
1 parent 99442ec0

feat:CacheUtils 增加 CACHE_MAX_SIZE 默认大小

yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java
@@ -15,6 +15,13 @@ import java.util.concurrent.Executors; @@ -15,6 +15,13 @@ import java.util.concurrent.Executors;
15 public class CacheUtils { 15 public class CacheUtils {
16 16
17 /** 17 /**
  18 + * 异步刷新的 LoadingCache 最大缓存数量
  19 + *
  20 + * @see <a href="">本地缓存 CacheUtils 工具类建议</a>
  21 + */
  22 + private static final Integer CACHE_MAX_SIZE = 10000;
  23 +
  24 + /**
18 * 构建异步刷新的 LoadingCache 对象 25 * 构建异步刷新的 LoadingCache 对象
19 * 26 *
20 * 注意:如果你的缓存和 ThreadLocal 有关系,要么自己处理 ThreadLocal 的传递,要么使用 {@link #buildCache(Duration, CacheLoader)} 方法 27 * 注意:如果你的缓存和 ThreadLocal 有关系,要么自己处理 ThreadLocal 的传递,要么使用 {@link #buildCache(Duration, CacheLoader)} 方法
@@ -29,6 +36,7 @@ public class CacheUtils { @@ -29,6 +36,7 @@ public class CacheUtils {
29 */ 36 */
30 public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) { 37 public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
31 return CacheBuilder.newBuilder() 38 return CacheBuilder.newBuilder()
  39 + .maximumSize(CACHE_MAX_SIZE)
32 // 只阻塞当前数据加载线程,其他线程返回旧值 40 // 只阻塞当前数据加载线程,其他线程返回旧值
33 .refreshAfterWrite(duration) 41 .refreshAfterWrite(duration)
34 // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程 42 // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程
@@ -43,7 +51,11 @@ public class CacheUtils { @@ -43,7 +51,11 @@ public class CacheUtils {
43 * @return LoadingCache 对象 51 * @return LoadingCache 对象
44 */ 52 */
45 public static <K, V> LoadingCache<K, V> buildCache(Duration duration, CacheLoader<K, V> loader) { 53 public static <K, V> LoadingCache<K, V> buildCache(Duration duration, CacheLoader<K, V> loader) {
46 - return CacheBuilder.newBuilder().refreshAfterWrite(duration).build(loader); 54 + return CacheBuilder.newBuilder()
  55 + .maximumSize(CACHE_MAX_SIZE)
  56 + // 只阻塞当前数据加载线程,其他线程返回旧值
  57 + .refreshAfterWrite(duration)
  58 + .build(loader);
47 } 59 }
48 60
49 } 61 }