houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.tenant.core.service;
H 2
3 import com.iailab.framework.common.pojo.CommonResult;
4 import com.iailab.framework.common.util.cache.CacheUtils;
5 import com.iailab.module.system.api.tenant.TenantApi;
6 import com.google.common.cache.CacheLoader;
7 import com.google.common.cache.LoadingCache;
8 import lombok.RequiredArgsConstructor;
9 import lombok.SneakyThrows;
10
11 import java.time.Duration;
12 import java.util.List;
13
14 import static com.iailab.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
15
16 /**
17  * Tenant 框架 Service 实现类
18  *
19  * @author iailab
20  */
21 @RequiredArgsConstructor
22 public class TenantFrameworkServiceImpl implements TenantFrameworkService {
23
24     private final TenantApi tenantApi;
25
26     /**
27      * 针对 {@link #getTenantIds()} 的缓存
28      */
29     private final LoadingCache<Object, List<Long>> getTenantIdsCache = buildAsyncReloadingCache(
30             Duration.ofMinutes(1L), // 过期时间 1 分钟
31             new CacheLoader<Object, List<Long>>() {
32
33                 @Override
34                 public List<Long> load(Object key) {
35                     return tenantApi.getTenantIdList().getCheckedData();
36                 }
37
38             });
39
40     /**
41      * 针对 {@link #validTenant(Long)} 的缓存
42      */
43     private final LoadingCache<Long, CommonResult<Boolean>> validTenantCache = buildAsyncReloadingCache(
44             Duration.ofMinutes(1L), // 过期时间 1 分钟
45             new CacheLoader<Long, CommonResult<Boolean>>() {
46
47                 @Override
48                 public CommonResult<Boolean> load(Long id) {
49                     return tenantApi.validTenant(id);
50                 }
51
52             });
53
54     @Override
55     @SneakyThrows
56     public List<Long> getTenantIds() {
57         return getTenantIdsCache.get(Boolean.TRUE);
58     }
59
60     @Override
61     @SneakyThrows
62     public void validTenant(Long id) {
63         validTenantCache.get(id).checkError();
64     }
65
66 }