簡體   English   中英

如果使用 nodejs 在 redis 中發生任何更改/事務,如何獲得通知

[英]how to get notifiction if any changes/transaction happen in redis using nodejs

我是 redis 的新手,如果 redis 中發生任何更改/事務(也來自巴恩德),我需要幫助來通知我正在運行的服務器。 使用 node.js 創建處於運行狀態的服務器

使用密鑰空間通知系統。 這里使用了redis的pub子層

步驟:在redis.conf文件中設置此參數-notify-keyspace-events 默認為空

這將能夠捕獲幾乎所有 DML 事件

我不確定如何獲得有關交易的通知,但這里有一個工作代碼(使用ioredis )來獲得有關 redis 任何更改的通知:

const redisClient = const redisClient = new Redis({
    host: 'localhost',
    port: 20000,
    lazyConnect: true, // because i will try to connect manually in the next line
    connectTimeout: 1000,
  })
await redisClient.connect()

// "AKE" === listen to all events of all redis-operations
await redisClient.config('SET', 'notify-keyspace-events', 'AKE')
await redisClient.psubscribe([`__key*__:*`])

redisClient.on(`pmessage`, (_pattern, redisKey, redisOpeation) => {
   const redisKey = redisKey.replace('__keyspace@0__:', '')
   if(redisKey === 'cool-redis-key' && redisOpeation === 'set'){
      console.log(`there was a "set" operation on "cool-redis-key" key`)
   }
})

補充資料:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM