Commit 762c19ecec8ccdf3b825eee3171f7a8aa3901f26
1 parent
844bdd73
【代码优化】PAY:移除 PayClient 缓存,减少复杂性,性能足够(非高频读取)
Showing
3 changed files
with
11 additions
and
47 deletions
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java
| @@ -14,22 +14,17 @@ import cn.iocoder.yudao.module.pay.convert.channel.PayChannelConvert; | @@ -14,22 +14,17 @@ import cn.iocoder.yudao.module.pay.convert.channel.PayChannelConvert; | ||
| 14 | import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; | 14 | import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; |
| 15 | import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper; | 15 | import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper; |
| 16 | import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; | 16 | import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; |
| 17 | -import com.google.common.cache.CacheLoader; | ||
| 18 | -import com.google.common.cache.LoadingCache; | ||
| 19 | -import lombok.Getter; | 17 | +import jakarta.annotation.PostConstruct; |
| 18 | +import jakarta.annotation.Resource; | ||
| 19 | +import jakarta.validation.Validator; | ||
| 20 | import lombok.extern.slf4j.Slf4j; | 20 | import lombok.extern.slf4j.Slf4j; |
| 21 | import org.springframework.stereotype.Service; | 21 | import org.springframework.stereotype.Service; |
| 22 | import org.springframework.validation.annotation.Validated; | 22 | import org.springframework.validation.annotation.Validated; |
| 23 | 23 | ||
| 24 | -import jakarta.annotation.PostConstruct; | ||
| 25 | -import jakarta.annotation.Resource; | ||
| 26 | -import jakarta.validation.Validator; | ||
| 27 | -import java.time.Duration; | ||
| 28 | import java.util.Collection; | 24 | import java.util.Collection; |
| 29 | import java.util.List; | 25 | import java.util.List; |
| 30 | 26 | ||
| 31 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 32 | -import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; | ||
| 33 | import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; | 28 | import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; |
| 34 | 29 | ||
| 35 | /** | 30 | /** |
| @@ -42,25 +37,6 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; | @@ -42,25 +37,6 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; | ||
| 42 | @Validated | 37 | @Validated |
| 43 | public class PayChannelServiceImpl implements PayChannelService { | 38 | public class PayChannelServiceImpl implements PayChannelService { |
| 44 | 39 | ||
| 45 | - /** | ||
| 46 | - * {@link PayClient} 缓存,通过它异步清空 smsClientFactory | ||
| 47 | - */ | ||
| 48 | - @Getter | ||
| 49 | - private final LoadingCache<Long, PayClient> clientCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), | ||
| 50 | - new CacheLoader<Long, PayClient>() { | ||
| 51 | - | ||
| 52 | - @Override | ||
| 53 | - public PayClient load(Long id) { | ||
| 54 | - // 查询,然后尝试清空 | ||
| 55 | - PayChannelDO channel = payChannelMapper.selectById(id); | ||
| 56 | - if (channel != null) { | ||
| 57 | - payClientFactory.createOrUpdatePayClient(channel.getId(), channel.getCode(), channel.getConfig()); | ||
| 58 | - } | ||
| 59 | - return payClientFactory.getPayClient(id); | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - }); | ||
| 63 | - | ||
| 64 | @Resource | 40 | @Resource |
| 65 | private PayClientFactory payClientFactory; | 41 | private PayClientFactory payClientFactory; |
| 66 | 42 | ||
| @@ -102,9 +78,6 @@ public class PayChannelServiceImpl implements PayChannelService { | @@ -102,9 +78,6 @@ public class PayChannelServiceImpl implements PayChannelService { | ||
| 102 | PayChannelDO channel = PayChannelConvert.INSTANCE.convert(updateReqVO) | 78 | PayChannelDO channel = PayChannelConvert.INSTANCE.convert(updateReqVO) |
| 103 | .setConfig(parseConfig(dbChannel.getCode(), updateReqVO.getConfig())); | 79 | .setConfig(parseConfig(dbChannel.getCode(), updateReqVO.getConfig())); |
| 104 | payChannelMapper.updateById(channel); | 80 | payChannelMapper.updateById(channel); |
| 105 | - | ||
| 106 | - // 清空缓存 | ||
| 107 | - clearCache(channel.getId()); | ||
| 108 | } | 81 | } |
| 109 | 82 | ||
| 110 | /** | 83 | /** |
| @@ -135,18 +108,6 @@ public class PayChannelServiceImpl implements PayChannelService { | @@ -135,18 +108,6 @@ public class PayChannelServiceImpl implements PayChannelService { | ||
| 135 | 108 | ||
| 136 | // 删除 | 109 | // 删除 |
| 137 | payChannelMapper.deleteById(id); | 110 | payChannelMapper.deleteById(id); |
| 138 | - | ||
| 139 | - // 清空缓存 | ||
| 140 | - clearCache(id); | ||
| 141 | - } | ||
| 142 | - | ||
| 143 | - /** | ||
| 144 | - * 删除缓存 | ||
| 145 | - * | ||
| 146 | - * @param id 渠道编号 | ||
| 147 | - */ | ||
| 148 | - private void clearCache(Long id) { | ||
| 149 | - clientCache.invalidate(id); | ||
| 150 | } | 111 | } |
| 151 | 112 | ||
| 152 | private PayChannelDO validateChannelExists(Long id) { | 113 | private PayChannelDO validateChannelExists(Long id) { |
| @@ -202,7 +163,8 @@ public class PayChannelServiceImpl implements PayChannelService { | @@ -202,7 +163,8 @@ public class PayChannelServiceImpl implements PayChannelService { | ||
| 202 | 163 | ||
| 203 | @Override | 164 | @Override |
| 204 | public PayClient getPayClient(Long id) { | 165 | public PayClient getPayClient(Long id) { |
| 205 | - return clientCache.getUnchecked(id); | 166 | + PayChannelDO channel = validPayChannel(id); |
| 167 | + return payClientFactory.createOrUpdatePayClient(id, channel.getCode(), channel.getConfig()); | ||
| 206 | } | 168 | } |
| 207 | 169 | ||
| 208 | } | 170 | } |
yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java
| @@ -23,9 +23,10 @@ public interface PayClientFactory { | @@ -23,9 +23,10 @@ public interface PayClientFactory { | ||
| 23 | * @param channelId 渠道编号 | 23 | * @param channelId 渠道编号 |
| 24 | * @param channelCode 渠道编码 | 24 | * @param channelCode 渠道编码 |
| 25 | * @param config 支付配置 | 25 | * @param config 支付配置 |
| 26 | + * @return 支付客户端 | ||
| 26 | */ | 27 | */ |
| 27 | - <Config extends PayClientConfig> void createOrUpdatePayClient(Long channelId, String channelCode, | ||
| 28 | - Config config); | 28 | + <Config extends PayClientConfig> PayClient createOrUpdatePayClient(Long channelId, String channelCode, |
| 29 | + Config config); | ||
| 29 | 30 | ||
| 30 | /** | 31 | /** |
| 31 | * 注册支付客户端 Class,用于模块中实现的 PayClient | 32 | * 注册支付客户端 Class,用于模块中实现的 PayClient |
yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java
| @@ -71,8 +71,8 @@ public class PayClientFactoryImpl implements PayClientFactory { | @@ -71,8 +71,8 @@ public class PayClientFactoryImpl implements PayClientFactory { | ||
| 71 | 71 | ||
| 72 | @Override | 72 | @Override |
| 73 | @SuppressWarnings("unchecked") | 73 | @SuppressWarnings("unchecked") |
| 74 | - public <Config extends PayClientConfig> void createOrUpdatePayClient(Long channelId, String channelCode, | ||
| 75 | - Config config) { | 74 | + public <Config extends PayClientConfig> PayClient createOrUpdatePayClient(Long channelId, String channelCode, |
| 75 | + Config config) { | ||
| 76 | AbstractPayClient<Config> client = (AbstractPayClient<Config>) clients.get(channelId); | 76 | AbstractPayClient<Config> client = (AbstractPayClient<Config>) clients.get(channelId); |
| 77 | if (client == null) { | 77 | if (client == null) { |
| 78 | client = this.createPayClient(channelId, channelCode, config); | 78 | client = this.createPayClient(channelId, channelCode, config); |
| @@ -81,6 +81,7 @@ public class PayClientFactoryImpl implements PayClientFactory { | @@ -81,6 +81,7 @@ public class PayClientFactoryImpl implements PayClientFactory { | ||
| 81 | } else { | 81 | } else { |
| 82 | client.refresh(config); | 82 | client.refresh(config); |
| 83 | } | 83 | } |
| 84 | + return client; | ||
| 84 | } | 85 | } |
| 85 | 86 | ||
| 86 | @SuppressWarnings("unchecked") | 87 | @SuppressWarnings("unchecked") |