AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何在 ElastiCache 中實作 Redis 鍵空間通知?
我想在 Amazon ElastiCache 中實作 Redis 鍵空間通知。
簡短說明
Redis 鍵空間通知可讓您訂閱 PubSub 通道。透過這些通道,當發生 Redis 命令或資料變更時,會收到發布的事件。當應用程式必須回應特定索引鍵中儲存的值所發生的變更時,這些通知會很有用。此外,您可以使用 Redis 鍵空間通知來追蹤資料集的變化。鍵空間通知功能可在 2.8.6 及更新版本中使用。當呼叫 Redis 鍵空間通知時,會發生鍵空間通知和鍵事件通知。鍵空間通道會接收事件的名稱,而鍵事件通道會以訊息形式接收索引鍵的名稱。
解決方法
注意: 以下解決方法適用於已關閉叢集模式的 ElastiCache 叢集。如需詳細資訊,請參閱 Redis 網站上的 Redis 鍵空間通知。
若要在 ElastiCache 中實作 Redis 鍵空間通知,請完成以下步驟:
啟動 Redis 鍵空間通知
注意: 預設情況下,ElastiCache 會關閉 Redis 鍵空間通知。若要在自訂快取參數群組中啟動鍵空間通知,請使用 notify-keyspace-events 參數。此參數值使用多個參數來決定要使用哪個通道 (鍵空間或鍵事件),以及要發佈到通道的資訊。如需詳細資訊,請參閱 Redis 2.8.6 新增的參數。
- 開啟 ElastiCache 主控台。
- 若要查看可用參數群組的清單,請在導覽窗格中選擇 Parameter Groups (參數群組)。
- 選取您要修改的參數群組。
注意: 您無法修改預設參數群組。如果列出的所有參數群組都是預設的,請建立一個新的參數群組。 - (選用) 若要建立新的參數群組,請選擇 Create Parameter Group (建立參數群組):
對於 Name (名稱) 欄位,輸入一個唯一的名稱。
對於 Description (描述) 欄位,輸入描述。
選取一個 Family (系列)。
選擇 Create (建立)。 - 選擇 Edit parameters values (編輯參數值)。
- 導覽至 notify-keyspace-events,然後在 Value (值) 欄位中輸入 AKE。有關允許值的更多資訊,請參閱 Redis 2.8.6 新增的參數。
- 選擇 Save changes (儲存變更)。變更立即生效,無需重新啟動叢集。
注意: 確保將新修改的參數群組指派給您的 Redis 叢集。
產生事件
若要產生事件,請使用 Redis 命令列介面 (CLI) 或 Redis Python 用戶端:
使用 Redis CLI (redis-cli)
-
若要訂閱通知,請使用 redis-cli 執行以下命令:
#./redis-cli -h testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com -p 6379 --csv psubscribe '__key*__:*' -
若要建立鍵空間事件,請執行任一組命令。以下範例命令會將索引鍵 Redis 和過期事件訂閱者的值和過期時間設定為 3 秒。
#./redis-cli -h testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com -p 6379 testdisabled.xm4oz6.ng.0001.use1.cache.amazonaws.com:6379> testdisabled.xm4oz6.ng.0001.use1.cache.amazonaws.com:6379> SET key1 value1 ex 5 testdisabled.xm4oz6.ng.0001.use1.cache.amazonaws.com:6379> -
確認事件是否產生。以下是產生事件的輸出範例:
#./redis-cli -h testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com -p 6379 --csv psubscribe '__key*__:*' Reading messages... (press Ctrl-C to quit) "psubscribe","__key*__:*",1 "pmessage","__key*__:*","__keyspace@0__:key1","set" "pmessage","__key*__:*","__keyevent@0__:set","key1" "pmessage","__key*__:*","__keyspace@0__:key1","expire" "pmessage","__key*__:*","__keyevent@0__:expire","key1" "pmessage","__key*__:*","__keyspace@0__:key1","expired" "pmessage","__key*__:*","__keyevent@0__:expired","key1"對於前面的範例輸出,第一個事件表示您已成功訂閱 1 個通道。第二個事件是鍵空間通知,表示事件名稱** set** 作為訊息。第三個事件是按鍵事件通知,表示以索引鍵名稱 key1 作為訊息。在第三個事件之後,您可以看到鍵空間和鍵事件通知的過期訊息。
使用 Redis Python 用戶端 (redis-py)
-
若要訂閱通知,請使用 Python 2 或 Python 3 指令碼。
注意: 以下指令碼會訂閱所有按鍵空間通知,並列印收到的事件。
Python 2:# cat subscribe.py import redis import datetime import time r = redis.Redis(host='testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com', port=6379, db=0) #Subscribing for events matching pattern "__key*__:*" p = r.pubsub() p.psubscribe('__key*__:*') print('Starting message loop') while True: message = p.get_message() if message: print datetime.datetime.now(), message else: time.sleep(0.01)Python 3:
import redis from datetime import datetime import time r = redis.Redis(host = 'testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com', port = 6379, db = 0) #Subscribing for events matching pattern "__key*__:* p = r.pubsub() p.psubscribe('__key*__:*') print('Starting message loop') while True: message = p.get_message() if message: print (datetime.now(), message) else: time.sleep(0.01)注意: 當較低的值或缺少休眠時間導致用戶端不斷輪詢通道時,可能會發生伺服器過載。數值越高,伺服器的記憶體使用量就越大。上述指令碼會將 time.sleep 設定為 0.01。
-
在單獨的工作階段中,連接到叢集,然後執行 createkey.py 指令碼來設定索引鍵或使其過期:
注意: 請確定將 example-key 設定為索引鍵,將 example-value 設定為值。另外,過期時間設為 3 秒。# cat createkey.py import redis r = redis.Redis(host='testdisabled.xxxxx.ng.0001.use1.cache.amazonaws.com', port=6379, db=0) r.set('example-key','example-value') r.expire('example-key',3) -
在單獨的工作階段中,執行 subscribe.py 指令碼。
範例輸出結果:# python2 subscribe.py Starting message loop 2024-04-29 23:40:56.356659 {u'pattern': None, u'type': 'psubscribe', u'channel': '__key*__:*', u'data': 1L} 2024-04-29 23:40:56.356659 {u'pattern': None, u'type': 'psubscribe', u'channel': '__key*__:*', u'data': 1L} 2024-04-29 23:41:00.016410 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyspace@0__:example-key', u'data': 'set'} 2024-04-29 23:41:00.016512 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyevent@0__:set', u'data': 'example-key'} 2024-04-29 23:41:00.016582 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyspace@0__:example-key', u'data': 'expire'} 2024-04-29 23:41:00.016650 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyevent@0__:expire', u'data': 'example-key'} 2024-04-29 23:41:03.111509 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyspace@0__:example-key', u'data': 'expired'} 2024-04-29 23:41:03.111601 {u'pattern': '__key*__:*', u'type': 'pmessage', u'channel': '__keyevent@0__:expired', u'data': 'example-key'}注意: 完成上述命令後,您的 Redis 鍵接聽程式就準備就緒了。
相關內容
已提問 3 年前
AWS 官方已更新 1 年前
