开发者

Redis实现订单过期删除的方法步骤

目录
  • 前言
  • 代码实现:
  • 兜底策略

前言

设计订单过期,不能单纯靠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.GsDIwQKEgetOrderTime();
         long between = cn.hutool.core.date.DateUtil.between(orderTime, new Date(), DateUn编程客栈it.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("*****[结束:下单十分钟以后系统自动取消订单]*****");
  }

到此这篇关于Redis实现订单过期删除的方法步骤的文章就介绍到这了,更多相关Redis 订单过期删除内容请搜索我们以前的文章编程客栈或继续浏览下面的相关文章希望大开发者_Hbase家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新数据库

数据库排行榜