Commit a685402688004f989886018942b80deaa4a50526

Authored by YunaiV
1 parent a5f82fed

【功能优化】短信:简化短信 channel 的缓存逻辑

yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/SmsClientFactory.java
... ... @@ -30,7 +30,8 @@ public interface SmsClientFactory {
30 30 * 创建短信 Client
31 31 *
32 32 * @param properties 配置对象
  33 + * @return 短信 Client
33 34 */
34   - void createOrUpdateSmsClient(SmsChannelProperties properties);
  35 + SmsClient createOrUpdateSmsClient(SmsChannelProperties properties);
35 36  
36 37 }
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/framework/sms/core/client/impl/SmsClientFactoryImpl.java
... ... @@ -59,7 +59,7 @@ public class SmsClientFactoryImpl implements SmsClientFactory {
59 59 }
60 60  
61 61 @Override
62   - public void createOrUpdateSmsClient(SmsChannelProperties properties) {
  62 + public SmsClient createOrUpdateSmsClient(SmsChannelProperties properties) {
63 63 AbstractSmsClient client = channelIdClients.get(properties.getId());
64 64 if (client == null) {
65 65 client = this.createSmsClient(properties);
... ... @@ -68,6 +68,7 @@ public class SmsClientFactoryImpl implements SmsClientFactory {
68 68 } else {
69 69 client.refresh(properties);
70 70 }
  71 + return client;
71 72 }
72 73  
73 74 private AbstractSmsClient createSmsClient(SmsChannelProperties properties) {
... ...
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelServiceImpl.java
1 1 package cn.iocoder.yudao.module.system.service.sms;
2 2  
3   -import cn.hutool.core.util.StrUtil;
4 3 import cn.iocoder.yudao.framework.common.pojo.PageResult;
5 4 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
6   -import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClient;
7   -import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClientFactory;
8   -import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
9 5 import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
10 6 import cn.iocoder.yudao.module.system.controller.admin.sms.vo.channel.SmsChannelSaveReqVO;
11 7 import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO;
12 8 import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsChannelMapper;
13   -import com.google.common.cache.CacheLoader;
14   -import com.google.common.cache.LoadingCache;
15   -import lombok.Getter;
  9 +import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClient;
  10 +import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClientFactory;
  11 +import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
  12 +import jakarta.annotation.Resource;
16 13 import lombok.extern.slf4j.Slf4j;
17 14 import org.springframework.stereotype.Service;
18 15  
19   -import jakarta.annotation.Resource;
20   -import java.time.Duration;
21 16 import java.util.List;
22 17  
23 18 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
24   -import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
25 19 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
26 20 import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS;
27 21  
... ... @@ -34,46 +28,6 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SMS_CHANNE
34 28 @Slf4j
35 29 public class SmsChannelServiceImpl implements SmsChannelService {
36 30  
37   - /**
38   - * {@link SmsClient} 缓存,通过它异步刷新 smsClientFactory
39   - */
40   - @Getter
41   - private final LoadingCache<Long, SmsClient> idClientCache = buildAsyncReloadingCache(Duration.ofSeconds(10L),
42   - new CacheLoader<Long, SmsClient>() {
43   -
44   - @Override
45   - public SmsClient load(Long id) {
46   - // 查询,然后尝试刷新
47   - SmsChannelDO channel = smsChannelMapper.selectById(id);
48   - if (channel != null) {
49   - SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
50   - smsClientFactory.createOrUpdateSmsClient(properties);
51   - }
52   - return smsClientFactory.getSmsClient(id);
53   - }
54   -
55   - });
56   -
57   - /**
58   - * {@link SmsClient} 缓存,通过它异步刷新 smsClientFactory
59   - */
60   - @Getter
61   - private final LoadingCache<String, SmsClient> codeClientCache = buildAsyncReloadingCache(Duration.ofSeconds(60L),
62   - new CacheLoader<String, SmsClient>() {
63   -
64   - @Override
65   - public SmsClient load(String code) {
66   - // 查询,然后尝试刷新
67   - SmsChannelDO channel = smsChannelMapper.selectByCode(code);
68   - if (channel != null) {
69   - SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
70   - smsClientFactory.createOrUpdateSmsClient(properties);
71   - }
72   - return smsClientFactory.getSmsClient(code);
73   - }
74   -
75   - });
76   -
77 31 @Resource
78 32 private SmsClientFactory smsClientFactory;
79 33  
... ... @@ -93,41 +47,22 @@ public class SmsChannelServiceImpl implements SmsChannelService {
93 47 @Override
94 48 public void updateSmsChannel(SmsChannelSaveReqVO updateReqVO) {
95 49 // 校验存在
96   - SmsChannelDO channel = validateSmsChannelExists(updateReqVO.getId());
  50 + validateSmsChannelExists(updateReqVO.getId());
97 51 // 更新
98 52 SmsChannelDO updateObj = BeanUtils.toBean(updateReqVO, SmsChannelDO.class);
99 53 smsChannelMapper.updateById(updateObj);
100   -
101   - // 清空缓存
102   - clearCache(updateReqVO.getId(), channel.getCode());
103 54 }
104 55  
105 56 @Override
106 57 public void deleteSmsChannel(Long id) {
107 58 // 校验存在
108   - SmsChannelDO channel = validateSmsChannelExists(id);
  59 + validateSmsChannelExists(id);
109 60 // 校验是否有在使用该账号的模版
110 61 if (smsTemplateService.getSmsTemplateCountByChannelId(id) > 0) {
111 62 throw exception(SMS_CHANNEL_HAS_CHILDREN);
112 63 }
113 64 // 删除
114 65 smsChannelMapper.deleteById(id);
115   -
116   - // 清空缓存
117   - clearCache(id, channel.getCode());
118   - }
119   -
120   - /**
121   - * 清空指定渠道编号的缓存
122   - *
123   - * @param id 渠道编号
124   - * @param code 渠道编码
125   - */
126   - private void clearCache(Long id, String code) {
127   - idClientCache.invalidate(id);
128   - if (StrUtil.isNotEmpty(code)) {
129   - codeClientCache.invalidate(code);
130   - }
131 66 }
132 67  
133 68 private SmsChannelDO validateSmsChannelExists(Long id) {
... ... @@ -155,12 +90,14 @@ public class SmsChannelServiceImpl implements SmsChannelService {
155 90  
156 91 @Override
157 92 public SmsClient getSmsClient(Long id) {
158   - return idClientCache.getUnchecked(id);
  93 + SmsChannelDO channel = smsChannelMapper.selectById(id);
  94 + SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
  95 + return smsClientFactory.createOrUpdateSmsClient(properties);
159 96 }
160 97  
161 98 @Override
162 99 public SmsClient getSmsClient(String code) {
163   - return codeClientCache.getUnchecked(code);
  100 + return smsClientFactory.getSmsClient(code);
164 101 }
165 102  
166 103 }
... ...
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsChannelServiceTest.java
... ... @@ -57,9 +57,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
57 57 // 校验记录的属性是否正确
58 58 SmsChannelDO smsChannel = smsChannelMapper.selectById(smsChannelId);
59 59 assertPojoEquals(reqVO, smsChannel, "id");
60   - // 断言 cache
61   - assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
62   - assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
63 60 }
64 61  
65 62 @Test
... ... @@ -79,9 +76,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
79 76 // 校验是否更新正确
80 77 SmsChannelDO smsChannel = smsChannelMapper.selectById(reqVO.getId()); // 获取最新的
81 78 assertPojoEquals(reqVO, smsChannel);
82   - // 断言 cache
83   - assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
84   - assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
85 79 }
86 80  
87 81 @Test
... ... @@ -105,9 +99,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
105 99 smsChannelService.deleteSmsChannel(id);
106 100 // 校验数据不存在了
107 101 assertNull(smsChannelMapper.selectById(id));
108   - // 断言 cache
109   - assertNull(smsChannelService.getIdClientCache().getIfPresent(dbSmsChannel.getId()));
110   - assertNull(smsChannelService.getCodeClientCache().getIfPresent(dbSmsChannel.getCode()));
111 102 }
112 103  
113 104 @Test
... ... @@ -196,29 +187,23 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
196 187 // mock 数据
197 188 SmsChannelDO channel = randomPojo(SmsChannelDO.class);
198 189 smsChannelMapper.insert(channel);
199   - // mock 参数
  190 + // 准备参数
200 191 Long id = channel.getId();
201 192 // mock 方法
202 193 SmsClient mockClient = mock(SmsClient.class);
203   - when(smsClientFactory.getSmsClient(eq(id))).thenReturn(mockClient);
  194 + SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
  195 + when(smsClientFactory.createOrUpdateSmsClient(eq(properties))).thenReturn(mockClient);
204 196  
205 197 // 调用
206 198 SmsClient client = smsChannelService.getSmsClient(id);
207 199 // 断言
208 200 assertSame(client, mockClient);
209   - verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
210   - SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
211   - return properties.equals(arg);
212   - }));
213 201 }
214 202  
215 203 @Test
216 204 public void testGetSmsClient_code() {
217   - // mock 数据
218   - SmsChannelDO channel = randomPojo(SmsChannelDO.class);
219   - smsChannelMapper.insert(channel);
220   - // mock 参数
221   - String code = channel.getCode();
  205 + // 准备参数
  206 + String code = randomString();
222 207 // mock 方法
223 208 SmsClient mockClient = mock(SmsClient.class);
224 209 when(smsClientFactory.getSmsClient(eq(code))).thenReturn(mockClient);
... ... @@ -227,10 +212,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
227 212 SmsClient client = smsChannelService.getSmsClient(code);
228 213 // 断言
229 214 assertSame(client, mockClient);
230   - verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
231   - SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
232   - return properties.equals(arg);
233   - }));
234 215 }
235 216  
236 217 }
... ...