人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

利用Redis實(shí)現(xiàn)訂單30分鐘自動(dòng)取消

發(fā)布日期:2022-07-15 19:18 | 文章來源:腳本之家

業(yè)務(wù)場景

我們以訂單功能為例說明下:

生成訂單后一段時(shí)間不支付訂單會(huì)自動(dòng)關(guān)閉。最簡單的想法是設(shè)置定時(shí)任務(wù)輪詢,但是每個(gè)訂單的創(chuàng)建時(shí)間不一樣,定時(shí)任務(wù)的規(guī)則無法設(shè)定,如果將定時(shí)任務(wù)執(zhí)行的間隔設(shè)置的過短,太影響效率。

還有一種想法,在用戶進(jìn)入訂單界面的時(shí)候,判斷時(shí)間執(zhí)行相關(guān)操作。方式可能有很多,在這里介紹一種監(jiān)聽 Redis 鍵值對(duì)過期時(shí)間來實(shí)現(xiàn)訂單自動(dòng)關(guān)閉。

實(shí)現(xiàn)思路

在生成訂單時(shí),向 Redis 中增加一個(gè) KV 鍵值對(duì),K 為訂單號(hào),保證通過 K 能定位到數(shù)據(jù)庫中的某個(gè)訂單即可,V 可為任意值。

假設(shè),生成訂單時(shí)向 Redis 中存放 K 為訂單號(hào),V 也為訂單號(hào)的鍵值對(duì),并設(shè)置過期時(shí)間為 30 分鐘,如果該鍵值對(duì)在 30 分鐘過期后能夠發(fā)送給程序一個(gè)通知,或者執(zhí)行一個(gè)方法,那么即可解決訂單關(guān)閉問題。

實(shí)現(xiàn):通過監(jiān)聽 Redis 提供的過期隊(duì)列來實(shí)現(xiàn),監(jiān)聽過期隊(duì)列后,如果 Redis 中某一個(gè) KV 鍵值對(duì)過期了,那么將向監(jiān)聽者發(fā)送消息,監(jiān)聽者可以獲取到該鍵值對(duì)的 K,注意,是獲取不到 V 的,因?yàn)橐呀?jīng)過期了,這就是上面所提到的,為什么要保證能通過 K 來定位到訂單,而 V 為任意值即可。拿到 K 后,通過 K 定位訂單,并判斷其狀態(tài),如果是未支付,更新為關(guān)閉,或者取消狀態(tài)即可。

開啟 Redis key 過期提醒

修改 redis 相關(guān)事件配置。找到 redis 配置文件 redis.conf,查看 notify-keyspace-events 配置項(xiàng),如果沒有,添加 notify-keyspace-events Ex,如果有值,則追加 Ex,相關(guān)參數(shù)說明如下:

  • K:keyspace 事件,事件以 keyspace@ 為前綴進(jìn)行發(fā)布
  • E:keyevent 事件,事件以 keyevent@ 為前綴進(jìn)行發(fā)布
  • g:一般性的,非特定類型的命令,比如del,expire,rename等
  • $:字符串特定命令
  • l:列表特定命令
  • s:集合特定命令
  • h:哈希特定命令
  • z:有序集合特定命令
  • x:過期事件,當(dāng)某個(gè)鍵過期并刪除時(shí)會(huì)產(chǎn)生該事件
  • e:驅(qū)逐事件,當(dāng)某個(gè)鍵因 maxmemore 策略而被刪除時(shí),產(chǎn)生該事件
  • A:g$lshzxe的別名,因此”AKE”意味著所有事件

引入依賴

在 pom.xml 中添加 org.springframework.boot:spring-boot-starter-data-redis 依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

相關(guān)配置

定義配置 RedisListenerConfig 實(shí)現(xiàn)監(jiān)聽 Redis key 過期時(shí)間

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
@Configuration
public class RedisListenerConfig {
? ? @Bean
? ? RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
? ? ? ? RedisMessageListenerContainer container = new RedisMessageListenerContainer();
? ? ? ? container.setConnectionFactory(connectionFactory);
? ? ? ? return container;
? ? }
}

定義監(jiān)聽器 RedisKeyExpirationListener,實(shí)現(xiàn)KeyExpirationEventMessageListener 接口,查看源碼發(fā)現(xiàn),該接口監(jiān)聽所有 db 的過期事件 keyevent@*:expired"

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
?* 監(jiān)聽所有db的過期事件__keyevent@*__:expired"
?*/
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
? ? public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
? ? ? ? super(listenerContainer);
? ? }
? ? /**
? ? ?* 針對(duì) redis 數(shù)據(jù)失效事件,進(jìn)行數(shù)據(jù)處理
? ? ?* @param message
? ? ?* @param pattern
? ? ?*/
? ? @Override
? ? public void onMessage(Message message, byte[] pattern) {
? ? ? ? // 獲取到失效的 key,進(jìn)行取消訂單業(yè)務(wù)處理
? ? ? ? String expiredKey = message.toString();
? ? ? ? System.out.println(expiredKey);
? ? }
}

redis 過期監(jiān)聽真的好么?

在 Redis 官方手冊(cè)的keyspace-notifications: timing-of-expired-events中明確指出:

Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero

redis 自動(dòng)過期的實(shí)現(xiàn)方式是:定時(shí)任務(wù)離線掃描并刪除部分過期鍵;在訪問鍵時(shí)惰性檢查是否過期并刪除過期鍵。redis 從未保證會(huì)在設(shè)定的過期時(shí)間立即刪除并發(fā)送過期通知。實(shí)際上,過期通知晚于設(shè)定的過期時(shí)間數(shù)分鐘的情況也比較常見。

此外鍵空間通知采用的是發(fā)送即忘(fire and forget)策略,并不像消息隊(duì)列一樣保證送達(dá)。當(dāng)訂閱事件的客戶端會(huì)丟失所有在斷線期間所有分發(fā)給它的事件。

這是一種比定時(shí)掃描數(shù)據(jù)庫更 “LOW” 的解決方案,不建議使用。

實(shí)現(xiàn)關(guān)閉訂單的方法

一般實(shí)現(xiàn)的方法有幾種:

  • 使用 rocketmq、rabbitmq、pulsar 等消息隊(duì)列的延時(shí)投遞功能
  • 使用 redisson 提供的 DelayedQueue

有一些方案雖然廣為流傳但存在著致命缺陷,不要用來實(shí)現(xiàn)延時(shí)任務(wù)

  • 使用 redis 的過期監(jiān)聽
  • 使用 rabbitmq 的死信隊(duì)列
  • 使用非持久化的時(shí)間輪

到此這篇關(guān)于利用Redis實(shí)現(xiàn)訂單30分鐘自動(dòng)取消的文章就介紹到這了,更多相關(guān)Redis訂單30分鐘自動(dòng)取消內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

美國穩(wěn)定服務(wù)器

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

實(shí)時(shí)開通

自選配置、實(shí)時(shí)開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對(duì)1客戶咨詢顧問

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部