日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Redis實現訂單過期刪除的方法步驟_Redis

作者:總是幸福的老豌豆 ? 更新時間: 2022-07-30 編程語言

前言

設計訂單過期,不能單純靠Redis,需要兜底策略

代碼實現:

import com.coolplay.trade.dto.req.CancelOrderReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class OrderRedisDelayQueueOperator extends AbstractOrderScheduleDelayQueue {
? ? @Resource(name = "redisTemplate")
? ? private ZSetOperations<String, String> orderRedis;

? ? /**
? ? ?* 預售、現貨生成訂單15分鐘后未支付,需要取消訂單
? ? ?*/
? ? private static final String DELAY_QUEUE_NAME = "order";

? ? /**
? ? ?* 每1秒執行一次
? ? ?*/
? ? @Override
? ? @Scheduled(cron = "0/1 * * * * ? ")
? ? public void orderEventProcess() {
? ? ? ? if (!redisLock.tryLock(this.getClass().getSimpleName(), TimeUnit.MILLISECONDS, 10, 100)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? Set<String> dq = orderRedis.range(DELAY_QUEUE_NAME, 0L, Long.MAX_VALUE);
? ? ? ? if (CollectionUtils.isEmpty(dq)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? for (String orderNo : dq) {
? ? ? ? ? ? Double xs = orderRedis.score(DELAY_QUEUE_NAME, orderNo);
? ? ? ? ? ? Double now = System.currentTimeMillis() * 1.0;
? ? ? ? ? ? if (xs <= now) {
? ? ? ? ? ? ? ? log.info("{} timed out", orderNo);
? ? ? ? ? ? ? ? super.threadPoolTaskExecutor.execute(() -> {
? ? ? ? ? ? ? ? ? ? CancelOrderReq req = new CancelOrderReq();
? ? ? ? ? ? ? ? ? ? req.setOrderNo(orderNo);
? ? ? ? ? ? ? ? ? ? req.setCancelType(OrderActionEnum.TIME_OUT_CANCEL);
? ? ? ? ? ? ? ? ? ? orderService.cancelOrder(req);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? //log.info("{} no time out", orderNo);
? ? ? ? ? ? ? ? //如果最小的都沒有過期,剩余的則不用處理了
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }


? ? public void addToRedis(String orderNo, long delayTime) {
? ? ? ? orderRedis.add(DELAY_QUEUE_NAME, orderNo, delayTime * 1.0);

? ? }

? ? public void removeFromRedis(String orderNo) {

? ? ? ? orderRedis.remove(DELAY_QUEUE_NAME, orderNo);
? ? }
}

兜底策略

/**
? ? ?* 取消訂單--10分鐘--20分鐘執行一次
? ? ?*/
? ? @XxlJob("cancelOrder20Minutes")
? ? public void cancelOrderTenMinutes() {
? ? ? ? log.info("*****[開始:下單十分鐘以后系統自動取消訂單]*****");
? ? ? ? Date start = DateUtil.dateRoll(new Date(), Calendar.MINUTE,-20);
? ? ? ? Date end = new Date();
? ? ? ?List<ClOrder> clorderList =clOrderMapper.selectListAllOrdrWaiting(start,end);
? ? ? ?if(ObjectUtil.isNotEmpty(clorderList)){
? ? ? ? ? ?for(int i=0;i<clorderList.size();i++){
? ? ? ? ? ? ? ?ClOrder clOrder = clorderList.get(i);
? ? ? ? ? ? ? ?if(ObjectUtil.isNotEmpty(clOrder)){
? ? ? ? ? ? ? ? ? ?Date orderTime = clOrder.getOrderTime();
? ? ? ? ? ? ? ? ? ?long between = cn.hutool.core.date.DateUtil.between(orderTime, new Date(), DateUnit.MINUTE);
? ? ? ? ? ? ? ? ? ?if(between>10){
? ? ? ? ? ? ? ? ? ? ? ?ClOrder clOrderTemp = new ClOrder();
? ? ? ? ? ? ? ? ? ? ? ?clOrderTemp.setOrderState("3");
? ? ? ? ? ? ? ? ? ? ? ?clOrderTemp.setId(clOrder.getId());
? ? ? ? ? ? ? ? ? ? ? ?clOrderTemp.setMemberId(clOrder.getMemberId());
? ? ? ? ? ? ? ? ? ? ? ?String msg="您的訂單已經取消,訂單金額已發放至您的賬戶請查收~";
? ? ? ? ? ? ? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ? ? ? ? ?boolean b = orderService.cancelOrder(clOrderTemp,msg);
? ? ? ? ? ? ? ? ? ? ? ? ? ?if(!b){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?log.info("[訂單失效:定時任務兜底策略更新失敗]**訂單ID: {}",clOrderTemp.getId());
? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ?log.info("[Redis訂單取消訂單失效,定時任務兜底策略生效]");
? ? ? ? ? ? ? ? ? ? ? ?}catch (Exception e){
? ? ? ? ? ? ? ? ? ? ? ? ? ?log.info("[訂單失效:定時任務兜底策略更新失敗]**訂單ID: {}",clOrderTemp.getId());
? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ?}


? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?}
? ? ? ? log.info("*****[結束:下單十分鐘以后系統自動取消訂單]*****");
? ? }

原文鏈接:https://blog.csdn.net/qq_32370913/article/details/124773954

欄目分類
最近更新