您好,登錄后才能下訂單哦!
這篇文章主要介紹“Redis如何實現訂單過期刪除”,在日常操作中,相信很多人在Redis如何實現訂單過期刪除問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”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.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("*****[結束:下單十分鐘以后系統自動取消訂單]*****"); }
到此,關于“Redis如何實現訂單過期刪除”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。