update 优化查询预约信息

This commit is contained in:
Guoqs
2026-06-22 17:00:48 +08:00
parent ecc75395ff
commit 710c8fb0fa
5 changed files with 602 additions and 7 deletions

View File

@@ -147,6 +147,8 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
}
sendReservationCommandAndAssertSuccess(pileReservationInfo, pileReservationInfo.getMemberId(), "01");
this.insertOrUpdateSelective(pileReservationInfo);
// 清除缓存
clearReservationCache(pileReservationInfo.getPileConnectorCode());
}
/**
@@ -161,6 +163,8 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
pileReservationInfo.setStatus(Constants.ZERO);
sendReservationCommandAndAssertSuccess(pileReservationInfo, pileReservationInfo.getMemberId(), "02");
this.insertOrUpdateSelective(pileReservationInfo);
// 清除缓存
clearReservationCache(pileReservationInfo.getPileConnectorCode());
}
private void sendReservationCommandAndAssertSuccess(PileReservationInfo pileReservationInfo, String memberId, String operation) {
@@ -304,6 +308,9 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
pileReservationInfo.setDelFlag(DelFlagEnum.DELETE.getValue());
pileReservationInfo.setStatus(Constants.ZERO);
pileReservationInfoMapper.updateByPrimaryKey(pileReservationInfo);
// 清除缓存
clearReservationCache(pileReservationInfo.getPileConnectorCode());
}
@Override
@@ -346,6 +353,10 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
}
sendReservationCommandAndAssertSuccess(reservedInfo, dto.getMemberId(), activeBeforeUpdate ? "03" : "01");
this.insertOrUpdateSelective(reservedInfo);
// 清除缓存
clearReservationCache(dto.getPileConnectorCode());
return reservedInfo.getId();
}
@@ -471,7 +482,12 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
if (updateFlag && (sendFlag == sendResult)) {
log.debug("修改预约充电相应成功, 删除缓存并更新数据库");
redisCache.deleteObject(redisKey);
return this.insertOrUpdateSelective(pileReservationInfo);
int result = this.insertOrUpdateSelective(pileReservationInfo);
// 清除查询接口的缓存
clearReservationCache(pileReservationInfo.getPileConnectorCode());
return result;
}
return 0;
}
@@ -517,12 +533,51 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
@Override
public PileReservationInfoVO queryReservationInfo(PileReservationDTO dto) {
PileReservationInfo pileReservationInfo = pileReservationInfoMapper.selectByPileConnectorCode(dto.getPileConnectorCode());
String pileSn = StringUtils.substring(dto.getPileConnectorCode(), 0, 14);
if (pileReservationInfo == null) {
// 初始化预约信息
pileReservationInfo = this.initPersonalPileReservation(dto.getPileConnectorCode());
String cacheKey = CacheConstants.RESERVATION_INFO + dto.getPileConnectorCode();
// 1. 先查缓存
PileReservationInfoVO cached = redisCache.getCacheObject(cacheKey);
if (cached != null) {
log.debug("命中预约信息缓存: {}", cacheKey);
return cached;
}
// 2. 查数据库
PileReservationInfo pileReservationInfo = pileReservationInfoMapper.selectByPileConnectorCode(dto.getPileConnectorCode());
// 3. 不存在则初始化(加锁防止并发重复初始化)
if (pileReservationInfo == null) {
String lockKey = "init_reservation_" + dto.getPileConnectorCode();
String uuid = com.jsowell.common.util.id.IdUtils.fastUUID();
try {
Boolean lockStatus = redisCache.lock(lockKey, uuid, 10);
if (lockStatus) {
// 双重检查:获取锁后再查一次,防止并发重复初始化
pileReservationInfo = pileReservationInfoMapper.selectByPileConnectorCode(dto.getPileConnectorCode());
if (pileReservationInfo == null) {
log.info("预约信息不存在,开始初始化: {}", dto.getPileConnectorCode());
pileReservationInfo = this.initPersonalPileReservation(dto.getPileConnectorCode());
}
} else {
// 获取锁失败,稍等后重试查询
log.warn("获取初始化锁失败,等待后重试: {}", dto.getPileConnectorCode());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("等待初始化锁时被中断", e);
}
pileReservationInfo = pileReservationInfoMapper.selectByPileConnectorCode(dto.getPileConnectorCode());
}
} finally {
String cacheUid = redisCache.getCacheObject(lockKey);
if (StringUtils.equals(cacheUid, uuid)) {
redisCache.unLock(lockKey);
}
}
}
// 4. 构建VO
PileReservationInfoVO build = PileReservationInfoVO.builder()
.reservedId(pileReservationInfo.getId() + "")
.pileSn(pileReservationInfo.getPileSn())
@@ -533,6 +588,11 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
// .freq(pileReservationInfo.getFreq())
.status(pileReservationInfo.getStatus())
.build();
// 5. 写入缓存5分钟过期
redisCache.setCacheObject(cacheKey, build, CacheConstants.cache_expire_time_5m);
log.debug("写入预约信息缓存: {}, 过期时间: {}秒", cacheKey, CacheConstants.cache_expire_time_5m);
return build;
}
@@ -595,4 +655,17 @@ public class PileReservationInfoServiceImpl implements PileReservationInfoServic
public void deleteReservationByPileSn(String pileSn) {
pileReservationInfoMapper.deleteByPileSn(pileSn);
}
/**
* 清除预约信息缓存
* @param pileConnectorCode 充电桩枪口编号
*/
private void clearReservationCache(String pileConnectorCode) {
if (StringUtils.isBlank(pileConnectorCode)) {
return;
}
String cacheKey = CacheConstants.RESERVATION_INFO + pileConnectorCode;
redisCache.deleteObject(cacheKey);
log.debug("清除预约信息缓存: {}", cacheKey);
}
}

View File

@@ -4,10 +4,14 @@ import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Getter
@Setter
@Builder
public class PileReservationInfoVO {
public class PileReservationInfoVO implements Serializable {
private static final long serialVersionUID = 1L;
private String reservedId;
/**