增加 TenantFrameworkServiceImpl 的缓存,提升性能
This commit is contained in:
parent
5598f62456
commit
5c04c9eeaf
|
@ -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 <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
|
||||||
|
return CacheBuilder.newBuilder()
|
||||||
|
// 只阻塞当前数据加载线程,其他线程返回旧值
|
||||||
|
.refreshAfterWrite(duration)
|
||||||
|
// 通过 asyncReloading 实现全异步加载,包括 refreshAfterWrite 被阻塞的加载线程
|
||||||
|
.build(CacheLoader.asyncReloading(loader, Executors.newSingleThreadExecutor()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -63,6 +63,13 @@
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 工具类相关 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
package cn.iocoder.yudao.framework.tenant.core.service;
|
package cn.iocoder.yudao.framework.tenant.core.service;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
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 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.RequiredArgsConstructor;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,16 +21,45 @@ public class TenantFrameworkServiceImpl implements TenantFrameworkService {
|
||||||
|
|
||||||
private final TenantApi tenantApi;
|
private final TenantApi tenantApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link #getTenantIds()} 的缓存
|
||||||
|
*/
|
||||||
|
private final LoadingCache<Object, List<Long>> getTenantIdsCache = CacheUtils.buildAsyncReloadingCache(
|
||||||
|
Duration.ofMinutes(1L), // 过期时间 1 分钟
|
||||||
|
new CacheLoader<Object, List<Long>>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Long> load(Object key) {
|
||||||
|
CommonResult<List<Long>> tenantIdsResult = tenantApi.getTenantIds();
|
||||||
|
tenantIdsResult.checkError();
|
||||||
|
return tenantIdsResult.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对 {@link #validTenant(Long)} 的缓存
|
||||||
|
*/
|
||||||
|
private final LoadingCache<Long, CommonResult<Boolean>> validTenantCache = CacheUtils.buildAsyncReloadingCache(
|
||||||
|
Duration.ofMinutes(1L), // 过期时间 1 分钟
|
||||||
|
new CacheLoader<Long, CommonResult<Boolean>>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommonResult<Boolean> load(Long id) {
|
||||||
|
return tenantApi.validTenant(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Long> getTenantIds() {
|
public List<Long> getTenantIds() {
|
||||||
CommonResult<List<Long>> tenantIdsResult = tenantApi.getTenantIds();
|
return getTenantIdsCache.getUnchecked(Boolean.TRUE);
|
||||||
tenantIdsResult.checkError();
|
|
||||||
return tenantIdsResult.getData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
public void validTenant(Long id) {
|
public void validTenant(Long id) {
|
||||||
tenantApi.validTenant(id).checkError();
|
validTenantCache.getUnchecked(id).checkError();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue