【代码优化】PAY:移除 PayClient 缓存,减少复杂性,性能足够(非高频读取)

This commit is contained in:
YunaiV
2024-07-24 23:03:59 +08:00
parent c0df6eab4e
commit f717c0f9c6
6 changed files with 25 additions and 49 deletions

View File

@@ -22,9 +22,9 @@ import java.time.LocalDateTime;
public class PayOrderSyncJob {
/**
* 同步创建时间在 N 分钟之的订单
* 同步创建时间在 N 分钟之的订单
*
* 为什么同步 10 分钟之的订单?
* 为什么同步 10 分钟之的订单?
* 因为一个订单发起支付,到支付成功,大多数在 10 分钟内,需要保证轮询到。
* 如果设置为 30、60 或者更大时间范围,会导致轮询的订单太多,影响性能。当然,你也可以根据自己的业务情况来处理。
*/

View File

@@ -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 jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.validation.Validator;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
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<Long, PayClient> clientCache = buildAsyncReloadingCache(Duration.ofSeconds(10L),
new CacheLoader<Long, PayClient>() {
@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());
}
}