潘志宝
5 天以前 4d7e3bb9a93ac0bdba9075e5efa536a165f8aae9
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.tenant.core.service;
H 2
d45017 3 import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
e7c126 4 import com.iailab.framework.common.pojo.CommonResult;
H 5 import com.iailab.framework.common.util.cache.CacheUtils;
6 import com.iailab.module.system.api.tenant.TenantApi;
7 import com.google.common.cache.CacheLoader;
8 import com.google.common.cache.LoadingCache;
d45017 9
10 import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO;
e7c126 11 import lombok.RequiredArgsConstructor;
H 12 import lombok.SneakyThrows;
13
14 import java.time.Duration;
15 import java.util.List;
16
17 import static com.iailab.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
18
19 /**
20  * Tenant 框架 Service 实现类
21  *
22  * @author iailab
23  */
24 @RequiredArgsConstructor
25 public class TenantFrameworkServiceImpl implements TenantFrameworkService {
26
27     private final TenantApi tenantApi;
28
29     /**
30      * 针对 {@link #getTenantIds()} 的缓存
31      */
32     private final LoadingCache<Object, List<Long>> getTenantIdsCache = buildAsyncReloadingCache(
33             Duration.ofMinutes(1L), // 过期时间 1 分钟
34             new CacheLoader<Object, List<Long>>() {
35
36                 @Override
37                 public List<Long> load(Object key) {
38                     return tenantApi.getTenantIdList().getCheckedData();
39                 }
40
41             });
42
43     /**
44      * 针对 {@link #validTenant(Long)} 的缓存
45      */
46     private final LoadingCache<Long, CommonResult<Boolean>> validTenantCache = buildAsyncReloadingCache(
47             Duration.ofMinutes(1L), // 过期时间 1 分钟
48             new CacheLoader<Long, CommonResult<Boolean>>() {
49
50                 @Override
51                 public CommonResult<Boolean> load(Long id) {
52                     return tenantApi.validTenant(id);
53                 }
54
55             });
56
d45017 57     /**
58      * 针对 {@link #getDataSourceProperty(Long)} 的缓存
59      */
60     private final LoadingCache<Long, DataSourceProperty> dataSourcePropertyCache = CacheUtils.buildAsyncReloadingCache(
61             Duration.ofMinutes(1L), // 过期时间 1 分钟
62             new CacheLoader<Long, DataSourceProperty>() {
63
64                 @Override
65                 public DataSourceProperty load(Long id) {
66                     // 获得租户对应的数据源配置
67                     TenantDataSourceConfigRespDTO dataSourceConfig = tenantApi.getTenantDataSourceConfig(id);
68                     if (dataSourceConfig == null) {
69                         return null;
70                     }
71                     // 转换成 dynamic-datasource 配置
ac01ad 72 //                    return new DataSourceProperty()
73 //                            .setPoolName(dataSourceConfig.getName()).setUrl(dataSourceConfig.getUrl())
74 //                            .setUsername(dataSourceConfig.getUsername()).setPassword(dataSourceConfig.getPassword());
d45017 75
76                     DataSourceProperty ds = new DataSourceProperty();
77                     ds.setPoolName(dataSourceConfig.getName());
78                     ds.setUrl(dataSourceConfig.getUrl());
79                     ds.setUsername(dataSourceConfig.getUsername());
80                     ds.setPassword(dataSourceConfig.getPassword());
81                     return ds;
82                 }
83
84             });
85
e7c126 86     @Override
H 87     @SneakyThrows
88     public List<Long> getTenantIds() {
89         return getTenantIdsCache.get(Boolean.TRUE);
90     }
91
92     @Override
93     @SneakyThrows
94     public void validTenant(Long id) {
95         validTenantCache.get(id).checkError();
96     }
97
d45017 98     @Override
99     @SneakyThrows
100     public DataSourceProperty getDataSourceProperty(Long id) {
101         return dataSourcePropertyCache.get(id);
102     }
e7c126 103 }