From 5be7965b1814d33341f75ca6cdee40c3500f6c98 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 27 Feb 2024 22:11:42 +0800 Subject: [PATCH] =?UTF-8?q?bugfix=EF=BC=9Aasync=20Cache=20=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=A7=9F=E6=88=B7=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/cache/CacheUtils.java | 30 +++++++++++++++---- .../dict/core/util/DictFrameworkUtils.java | 6 ++-- .../service/TenantFrameworkServiceImpl.java | 6 ++-- .../service/SecurityFrameworkServiceImpl.java | 6 ++-- .../security/TokenAuthenticationFilter.java | 5 +++- .../bargain/AppBargainActivityController.java | 3 +- .../AppCombinationActivityController.java | 3 +- .../seckill/AppSeckillActivityController.java | 3 +- .../social/SocialClientServiceImpl.java | 5 ++-- 9 files changed, 50 insertions(+), 17 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java index 41f75405e..12a6e1724 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java @@ -1,12 +1,10 @@ package cn.iocoder.yudao.framework.common.util.cache; -import com.alibaba.ttl.threadpool.TtlExecutors; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import java.time.Duration; -import java.util.concurrent.Executor; import java.util.concurrent.Executors; /** @@ -16,14 +14,36 @@ import java.util.concurrent.Executors; */ public class CacheUtils { + /** + * 构建异步刷新的 LoadingCache 对象 + * + * 注意:如果你的缓存和 ThreadLocal 有关系,要么自己处理 ThreadLocal 的传递,要么使用 {@link #buildCache(Duration, CacheLoader)} 方法 + * + * 或者简单理解: + * 1、和“人”相关的,使用 {@link #buildCache(Duration, CacheLoader)} 方法 + * 2、和“全局”、“系统”相关的,使用当前缓存方法 + * + * @param duration 过期时间 + * @param loader CacheLoader 对象 + * @return LoadingCache 对象 + */ public static LoadingCache buildAsyncReloadingCache(Duration duration, CacheLoader loader) { - Executor executor = Executors.newCachedThreadPool( // TODO 芋艿:可能要思考下,未来要不要做成可配置 - TtlExecutors.getDefaultDisableInheritableThreadFactory()); // TTL 保证 ThreadLocal 可以透传 return CacheBuilder.newBuilder() // 只阻塞当前数据加载线程,其他线程返回旧值 .refreshAfterWrite(duration) // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程 - .build(CacheLoader.asyncReloading(loader, executor)); + .build(CacheLoader.asyncReloading(loader, Executors.newCachedThreadPool())); // TODO 芋艿:可能要思考下,未来要不要做成可配置 + } + + /** + * 构建同步刷新的 LoadingCache 对象 + * + * @param duration 过期时间 + * @param loader CacheLoader 对象 + * @return LoadingCache 对象 + */ + public static LoadingCache buildCache(Duration duration, CacheLoader loader) { + return CacheBuilder.newBuilder().refreshAfterWrite(duration).build(loader); } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java index 8e0661749..ad8de43d7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-dict/src/main/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtils.java @@ -12,6 +12,8 @@ import lombok.extern.slf4j.Slf4j; import java.time.Duration; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; + /** * 字典工具类 * @@ -27,7 +29,7 @@ public class DictFrameworkUtils { /** * 针对 {@link #getDictDataLabel(String, String)} 的缓存 */ - private static final LoadingCache, DictDataRespDTO> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache( + private static final LoadingCache, DictDataRespDTO> GET_DICT_DATA_CACHE = buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader, DictDataRespDTO>() { @@ -42,7 +44,7 @@ public class DictFrameworkUtils { /** * 针对 {@link #parseDictDataValue(String, String)} 的缓存 */ - private static final LoadingCache, DictDataRespDTO> PARSE_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache( + private static final LoadingCache, DictDataRespDTO> PARSE_DICT_DATA_CACHE = buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader, DictDataRespDTO>() { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java index 7877ae1e0..abeecab87 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkServiceImpl.java @@ -11,6 +11,8 @@ import lombok.SneakyThrows; import java.time.Duration; import java.util.List; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; + /** * Tenant 框架 Service 实现类 * @@ -24,7 +26,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { /** * 针对 {@link #getTenantIds()} 的缓存 */ - private final LoadingCache> getTenantIdsCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache> getTenantIdsCache = buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>() { @@ -38,7 +40,7 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { /** * 针对 {@link #validTenant(Long)} 的缓存 */ - private final LoadingCache> validTenantCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache> validTenantCache = buildAsyncReloadingCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>() { diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java index a3e4ad98c..2e4dc15f5 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/service/SecurityFrameworkServiceImpl.java @@ -20,6 +20,8 @@ import java.time.Duration; import java.util.Arrays; import java.util.List; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildCache; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; /** @@ -35,7 +37,7 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService { /** * 针对 {@link #hasAnyRoles(String...)} 的缓存 */ - private final LoadingCache>, Boolean> hasAnyRolesCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache>, Boolean> hasAnyRolesCache = buildCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>, Boolean>() { @@ -49,7 +51,7 @@ public class SecurityFrameworkServiceImpl implements SecurityFrameworkService { /** * 针对 {@link #hasAnyPermissions(String...)} 的缓存 */ - private final LoadingCache>, Boolean> hasAnyPermissionsCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache>, Boolean> hasAnyPermissionsCache = buildCache( Duration.ofMinutes(1L), // 过期时间 1 分钟 new CacheLoader>, Boolean>() { diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/security/TokenAuthenticationFilter.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/security/TokenAuthenticationFilter.java index c944c18fa..c4ace9146 100644 --- a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/security/TokenAuthenticationFilter.java +++ b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/filter/security/TokenAuthenticationFilter.java @@ -26,6 +26,9 @@ import java.time.Duration; import java.util.Objects; import java.util.function.Function; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildCache; + /** * Token 过滤器,验证 token 的有效性 * 1. 验证通过时,将 userId、userType、tenantId 通过 Header 转发给服务 @@ -59,7 +62,7 @@ public class TokenAuthenticationFilter implements GlobalFilter, Ordered { * key1:多租户的编号 * key2:访问令牌 */ - private final LoadingCache, LoginUser> loginUserCache = CacheUtils.buildAsyncReloadingCache(Duration.ofMinutes(1), + private final LoadingCache, LoginUser> loginUserCache = buildAsyncReloadingCache(Duration.ofMinutes(1), new CacheLoader, LoginUser>() { @Override diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainActivityController.java index 8219a5583..1a4386a17 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/bargain/AppBargainActivityController.java @@ -31,6 +31,7 @@ import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildCache; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; @Tag(name = "用户 App - 砍价活动") @@ -42,7 +43,7 @@ public class AppBargainActivityController { /** * {@link AppBargainActivityRespVO} 缓存,通过它异步刷新 {@link #getBargainActivityList0(Integer)} 所要的首页数据 */ - private final LoadingCache> bargainActivityListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), + private final LoadingCache> bargainActivityListCache = buildCache(Duration.ofSeconds(10L), new CacheLoader>() { @Override diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/combination/AppCombinationActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/combination/AppCombinationActivityController.java index 6b0beaeff..1a46a0189 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/combination/AppCombinationActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/combination/AppCombinationActivityController.java @@ -32,6 +32,7 @@ import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildCache; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; @Tag(name = "用户 APP - 拼团活动") @@ -43,7 +44,7 @@ public class AppCombinationActivityController { /** * {@link AppCombinationActivityRespVO} 缓存,通过它异步刷新 {@link #getCombinationActivityList0(Integer)} 所要的首页数据 */ - private final LoadingCache> combinationActivityListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), + private final LoadingCache> combinationActivityListCache = buildCache(Duration.ofSeconds(10L), new CacheLoader>() { @Override diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/seckill/AppSeckillActivityController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/seckill/AppSeckillActivityController.java index bc3609840..8a6b020d0 100644 --- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/seckill/AppSeckillActivityController.java +++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/seckill/AppSeckillActivityController.java @@ -39,6 +39,7 @@ import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildCache; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.isBetween; @@ -52,7 +53,7 @@ public class AppSeckillActivityController { /** * {@link AppSeckillActivityNowRespVO} 缓存,通过它异步刷新 {@link #getNowSeckillActivity()} 所要的首页数据 */ - private final LoadingCache nowSeckillActivityCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), + private final LoadingCache nowSeckillActivityCache = buildCache(Duration.ofSeconds(10L), new CacheLoader() { @Override diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java index ab58cc6bd..2e0a0d144 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java @@ -46,6 +46,7 @@ import java.time.Duration; import java.util.Objects; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; @@ -75,7 +76,7 @@ public class SocialClientServiceImpl implements SocialClientService { * * 为什么要做 WxMpService 缓存?因为 WxMpService 构建成本比较大,所以尽量保证它是单例。 */ - private final LoadingCache wxMpServiceCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache wxMpServiceCache = buildAsyncReloadingCache( Duration.ofSeconds(10L), new CacheLoader() { @@ -96,7 +97,7 @@ public class SocialClientServiceImpl implements SocialClientService { * * 说明同 {@link #wxMpServiceCache} 变量 */ - private final LoadingCache wxMaServiceCache = CacheUtils.buildAsyncReloadingCache( + private final LoadingCache wxMaServiceCache = buildAsyncReloadingCache( Duration.ofSeconds(10L), new CacheLoader() {