mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-16 03:55:06 +08:00
简化本地缓存的实现,萌新更容易看懂!
This commit is contained in:
@ -8,9 +8,7 @@ import cn.iocoder.yudao.module.pay.controller.admin.merchant.vo.channel.PayChann
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.merchant.PayChannelDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@ -21,9 +19,6 @@ public interface PayChannelMapper extends BaseMapperX<PayChannelDO> {
|
||||
return selectOne(PayChannelDO::getAppId, appId, PayChannelDO::getCode, code);
|
||||
}
|
||||
|
||||
@Select("SELECT COUNT(*) FROM pay_channel WHERE update_time > #{maxUpdateTime}")
|
||||
Long selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime);
|
||||
|
||||
default PageResult<PayChannelDO> selectPage(PayChannelPageReqVO reqVO) {
|
||||
return selectPage(reqVO, new QueryWrapperX<PayChannelDO>()
|
||||
.eqIfPresent("code", reqVO.getCode())
|
||||
@ -67,14 +62,14 @@ public interface PayChannelMapper extends BaseMapperX<PayChannelDO> {
|
||||
* 根据条件获取渠道
|
||||
*
|
||||
* @param merchantId 商户编号
|
||||
* @param appid 应用编号 // TODO @aquan:appid =》appId
|
||||
* @param appI 应用编号
|
||||
* @param code 渠道编码
|
||||
* @return 数量
|
||||
*/
|
||||
default PayChannelDO selectOne(Long merchantId, Long appid, String code) {
|
||||
default PayChannelDO selectOne(Long merchantId, Long appI, String code) {
|
||||
return this.selectOne((new QueryWrapper<PayChannelDO>().lambda()
|
||||
.eq(PayChannelDO::getMerchantId, merchantId)
|
||||
.eq(PayChannelDO::getAppId, appid)
|
||||
.eq(PayChannelDO::getAppId, appI)
|
||||
.eq(PayChannelDO::getCode, code)
|
||||
));
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import cn.hutool.json.JSONUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig;
|
||||
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.PayChannelEnum;
|
||||
@ -19,7 +18,6 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.merchant.PayChannelDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.mysql.merchant.PayChannelMapper;
|
||||
import cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@ -44,12 +42,6 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.CHANNEL_NOT_E
|
||||
@Validated
|
||||
public class PayChannelServiceImpl implements PayChannelService {
|
||||
|
||||
/**
|
||||
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
|
||||
* 因为已经通过 Redis Pub/Sub 机制,所以频率不需要高
|
||||
*/
|
||||
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
|
||||
|
||||
/**
|
||||
* 缓存菜单的最大更新时间,用于后续的增量轮询,判断是否有更新
|
||||
*/
|
||||
@ -70,40 +62,15 @@ public class PayChannelServiceImpl implements PayChannelService {
|
||||
@Override
|
||||
@PostConstruct
|
||||
public void initLocalCache() {
|
||||
initLocalCacheIfUpdate(null);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
|
||||
public void schedulePeriodicRefresh() {
|
||||
initLocalCacheIfUpdate(this.maxUpdateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新本地缓存
|
||||
*
|
||||
* @param maxUpdateTime 最大更新时间
|
||||
* 1. 如果 maxUpdateTime 为 null,则“强制”刷新缓存
|
||||
* 2. 如果 maxUpdateTime 不为 null,判断自 maxUpdateTime 是否有数据发生变化,有的情况下才刷新缓存
|
||||
*/
|
||||
private void initLocalCacheIfUpdate(LocalDateTime maxUpdateTime) {
|
||||
// 注意:忽略自动多租户,因为要全局初始化缓存
|
||||
TenantUtils.executeIgnore(() -> {
|
||||
// 第一步:基于 maxUpdateTime 判断缓存是否刷新。
|
||||
// 如果没有增量的数据变化,则不进行本地缓存的刷新
|
||||
if (maxUpdateTime != null
|
||||
&& channelMapper.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
|
||||
log.info("[initLocalCacheIfUpdate][数据未发生变化({}),本地缓存不刷新]", maxUpdateTime);
|
||||
return;
|
||||
}
|
||||
// 第一步:查询数据
|
||||
List<PayChannelDO> channels = channelMapper.selectList();
|
||||
log.info("[initLocalCacheIfUpdate][缓存支付渠道,数量为:{}]", channels.size());
|
||||
log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
|
||||
|
||||
// 第二步:构建缓存。创建或更新支付 Client
|
||||
// 第二步:构建缓存:创建或更新支付 Client
|
||||
channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
|
||||
payChannel.getCode(), payChannel.getConfig()));
|
||||
|
||||
// 第三步:设置最新的 maxUpdateTime,用于下次的增量判断。
|
||||
this.maxUpdateTime = CollectionUtils.getMaxValue(channels, PayChannelDO::getUpdateTime);
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user