From 762c19ecec8ccdf3b825eee3171f7a8aa3901f26 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 24 Jul 2024 22:54:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91PAY=EF=BC=9A=E7=A7=BB=E9=99=A4=20PayClient=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98=EF=BC=8C=E5=87=8F=E5=B0=91=E5=A4=8D=E6=9D=82=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E6=80=A7=E8=83=BD=E8=B6=B3=E5=A4=9F=EF=BC=88=E9=9D=9E?= =?UTF-8?q?=E9=AB=98=E9=A2=91=E8=AF=BB=E5=8F=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../channel/PayChannelServiceImpl.java | 48 ++----------------- .../pay/core/client/PayClientFactory.java | 5 +- .../client/impl/PayClientFactoryImpl.java | 5 +- 3 files changed, 11 insertions(+), 47 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java index 522b2b0ae..ba2bef7b8 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java +++ b/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; import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper; import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; -import lombok.Getter; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.Resource; +import jakarta.validation.Validator; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.PostConstruct; -import jakarta.annotation.Resource; -import jakarta.validation.Validator; -import java.time.Duration; import java.util.Collection; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; /** @@ -42,25 +37,6 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*; @Validated public class PayChannelServiceImpl implements PayChannelService { - /** - * {@link PayClient} 缓存,通过它异步清空 smsClientFactory - */ - @Getter - private final LoadingCache clientCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), - new CacheLoader() { - - @Override - public PayClient load(Long id) { - // 查询,然后尝试清空 - PayChannelDO channel = payChannelMapper.selectById(id); - if (channel != null) { - payClientFactory.createOrUpdatePayClient(channel.getId(), channel.getCode(), channel.getConfig()); - } - return payClientFactory.getPayClient(id); - } - - }); - @Resource private PayClientFactory payClientFactory; @@ -102,9 +78,6 @@ public class PayChannelServiceImpl implements PayChannelService { PayChannelDO channel = PayChannelConvert.INSTANCE.convert(updateReqVO) .setConfig(parseConfig(dbChannel.getCode(), updateReqVO.getConfig())); payChannelMapper.updateById(channel); - - // 清空缓存 - clearCache(channel.getId()); } /** @@ -135,18 +108,6 @@ public class PayChannelServiceImpl implements PayChannelService { // 删除 payChannelMapper.deleteById(id); - - // 清空缓存 - clearCache(id); - } - - /** - * 删除缓存 - * - * @param id 渠道编号 - */ - private void clearCache(Long id) { - clientCache.invalidate(id); } private PayChannelDO validateChannelExists(Long id) { @@ -202,7 +163,8 @@ public class PayChannelServiceImpl implements PayChannelService { @Override public PayClient getPayClient(Long id) { - return clientCache.getUnchecked(id); + PayChannelDO channel = validPayChannel(id); + return payClientFactory.createOrUpdatePayClient(id, channel.getCode(), channel.getConfig()); } } diff --git a/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java b/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java index 53f1a8c06..934b20bd8 100644 --- a/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java +++ b/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 { * @param channelId 渠道编号 * @param channelCode 渠道编码 * @param config 支付配置 + * @return 支付客户端 */ - void createOrUpdatePayClient(Long channelId, String channelCode, - Config config); + PayClient createOrUpdatePayClient(Long channelId, String channelCode, + Config config); /** * 注册支付客户端 Class,用于模块中实现的 PayClient diff --git a/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java b/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java index 815f8d4a6..1a50215cb 100644 --- a/yudao-module-pay/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java +++ b/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 { @Override @SuppressWarnings("unchecked") - public void createOrUpdatePayClient(Long channelId, String channelCode, - Config config) { + public PayClient createOrUpdatePayClient(Long channelId, String channelCode, + Config config) { AbstractPayClient client = (AbstractPayClient) clients.get(channelId); if (client == null) { client = this.createPayClient(channelId, channelCode, config); @@ -81,6 +81,7 @@ public class PayClientFactoryImpl implements PayClientFactory { } else { client.refresh(config); } + return client; } @SuppressWarnings("unchecked")