Skip to content

Commit

Permalink
pay 缓存,使用 guava 替代 job 扫描,目的:提升启动速度,加快缓存失效
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Sep 16, 2023
1 parent a51579a commit 222daa5
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AbstractPayClient(Long channelId, String channelCode, Config config) {
*/
public final void init() {
doInit();
log.info("[init][客户端({}) 初始化完成]", getId());
log.debug("[init][客户端({}) 初始化完成]", getId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,38 @@ public void testGetFileConfig() {

@Test
public void testGetFileClient() {
// mock 数据
FileConfigDO fileConfig = randomFileConfigDO().setMaster(false);
fileConfigMapper.insert(fileConfig);
// 准备参数
Long id = randomLongId();
Long id = fileConfig.getId();
// mock 获得 Client
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);

// 调用,并断言
assertSame(fileClient, fileConfigService.getFileClient(id));
// 断言缓存
verify(fileClientFactory).createOrUpdateFileClient(eq(id), eq(fileConfig.getStorage()),
eq(fileConfig.getConfig()));
}

@Test
public void testGetMasterFileClient() {
// mock 数据
FileConfigDO fileConfig = randomFileConfigDO().setMaster(true);
fileConfigMapper.insert(fileConfig);
// 准备参数
Long id = fileConfig.getId();
// mock 获得 Client
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
when(fileClientFactory.getFileClient(eq(0L))).thenReturn(fileClient);

// 调用,并断言
assertSame(fileClient, fileConfigService.getMasterFileClient());
// 断言缓存
verify(fileClientFactory).createOrUpdateFileClient(eq(0L), eq(fileConfig.getStorage()),
eq(fileConfig.getConfig()));
}

private FileConfigDO randomFileConfigDO() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import cn.iocoder.yudao.module.pay.controller.admin.notify.vo.PayNotifyTaskDetailRespVO;
Expand All @@ -16,6 +15,7 @@
import cn.iocoder.yudao.module.pay.dal.dataobject.notify.PayNotifyLogDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.notify.PayNotifyTaskDO;
import cn.iocoder.yudao.module.pay.service.app.PayAppService;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.notify.PayNotifyService;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
Expand Down Expand Up @@ -53,9 +53,8 @@ public class PayNotifyController {
private PayNotifyService notifyService;
@Resource
private PayAppService appService;

@Resource
private PayClientFactory payClientFactory;
private PayChannelService channelService;

@PostMapping(value = "/order/{channelId}")
@Operation(summary = "支付渠道的统一【支付】回调")
Expand All @@ -66,7 +65,7 @@ public String notifyOrder(@PathVariable("channelId") Long channelId,
@RequestBody(required = false) String body) {
log.info("[notifyOrder][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = payClientFactory.getPayClient(channelId);
PayClient payClient = channelService.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(CHANNEL_NOT_FOUND);
Expand All @@ -87,7 +86,7 @@ public String notifyRefund(@PathVariable("channelId") Long channelId,
@RequestBody(required = false) String body) {
log.info("[notifyRefund][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = payClientFactory.getPayClient(channelId);
PayClient payClient = channelService.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(CHANNEL_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ default List<PayChannelDO> selectListByAppId(Long appId, Integer status) {
.eq(PayChannelDO::getStatus, status));
}

@Select("SELECT COUNT(*) FROM pay_channel WHERE update_time > #{maxUpdateTime}")
Long selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* @author 芋道源码
*/
@Component
@TenantJob // 多租户
@Slf4j
public class PayNotifyJob implements JobHandler {

@Resource
private PayNotifyService payNotifyService;

@Override
@TenantJob
public String execute(String param) throws Exception {
int notifyCount = payNotifyService.executeNotify();
return String.format("执行支付通知 %s 个", notifyCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* @author 芋道源码
*/
@Component
@TenantJob
public class PayOrderExpireJob implements JobHandler {

@Resource
private PayOrderService orderService;

@Override
@TenantJob
public String execute(String param) {
int count = orderService.expireOrder();
return StrUtil.format("支付过期 {} 个", count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @author 芋道源码
*/
@Component
@TenantJob
public class PayOrderSyncJob implements JobHandler {

/**
Expand All @@ -34,6 +33,7 @@ public class PayOrderSyncJob implements JobHandler {
private PayOrderService orderService;

@Override
@TenantJob
public String execute(String param) {
LocalDateTime minCreateTime = LocalDateTime.now().minus(CREATE_TIME_DURATION_BEFORE);
int count = orderService.syncOrder(minCreateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* @author 芋道源码
*/
@Component
@TenantJob
public class PayRefundSyncJob implements JobHandler {

@Resource
private PayRefundService refundService;

@Override
@TenantJob
public String execute(String param) {
int count = refundService.syncRefund();
return StrUtil.format("同步退款订单 {} 个", count);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.pay.service.channel;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelUpdateReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
Expand Down Expand Up @@ -92,4 +93,12 @@ public interface PayChannelService {
*/
List<PayChannelDO> getEnableChannelList(Long appId);

/**
* 获得指定编号的支付客户端
*
* @param id 编号
* @return 支付客户端
*/
PayClient getPayClient(Long id);

}
Loading

0 comments on commit 222daa5

Please sign in to comment.