From 5c04c9eeaffd3b3b91a774e947ea7f4dc5591f6e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 17 Jun 2022 07:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20TenantFrameworkServiceImpl?= =?UTF-8?q?=20=E7=9A=84=E7=BC=93=E5=AD=98=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/cache/CacheUtils.java | 25 +++++++++++ .../pom.xml | 7 ++++ .../service/TenantFrameworkServiceImpl.java | 42 +++++++++++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java 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 new file mode 100644 index 00000000..8c9c7f6c --- /dev/null +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/cache/CacheUtils.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.common.util.cache; + +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.Executors; + +/** + * Cache 工具类 + * + * @author 芋道源码 + */ +public class CacheUtils { + + public static LoadingCache buildAsyncReloadingCache(Duration duration, CacheLoader loader) { + return CacheBuilder.newBuilder() + // 只阻塞当前数据加载线程,其他线程返回旧值 + .refreshAfterWrite(duration) + // 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程 + .build(CacheLoader.asyncReloading(loader, Executors.newSingleThreadExecutor())); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml index 634b323d..225d8b47 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml @@ -63,6 +63,13 @@ spring-boot-starter-test test + + + + com.google.guava + guava + + 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 1aaa48c1..301828b6 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 @@ -1,9 +1,14 @@ package cn.iocoder.yudao.framework.tenant.core.service; import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.util.cache.CacheUtils; import cn.iocoder.yudao.module.system.api.tenant.TenantApi; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import java.time.Duration; import java.util.List; /** @@ -16,16 +21,45 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService { private final TenantApi tenantApi; + /** + * 针对 {@link #getTenantIds()} 的缓存 + */ + private final LoadingCache> getTenantIdsCache = CacheUtils.buildAsyncReloadingCache( + Duration.ofMinutes(1L), // 过期时间 1 分钟 + new CacheLoader>() { + + @Override + public List load(Object key) { + CommonResult> tenantIdsResult = tenantApi.getTenantIds(); + tenantIdsResult.checkError(); + return tenantIdsResult.getData(); + } + + }); + + /** + * 针对 {@link #validTenant(Long)} 的缓存 + */ + private final LoadingCache> validTenantCache = CacheUtils.buildAsyncReloadingCache( + Duration.ofMinutes(1L), // 过期时间 1 分钟 + new CacheLoader>() { + + @Override + public CommonResult load(Long id) { + return tenantApi.validTenant(id); + } + + }); + @Override public List getTenantIds() { - CommonResult> tenantIdsResult = tenantApi.getTenantIds(); - tenantIdsResult.checkError(); - return tenantIdsResult.getData(); + return getTenantIdsCache.getUnchecked(Boolean.TRUE); } @Override + @SneakyThrows public void validTenant(Long id) { - tenantApi.validTenant(id).checkError(); + validTenantCache.getUnchecked(id).checkError(); } }