如果使用nodejs在redis中发生任何更改/事务,如何获得通知

时间:2017-05-18 10:05:08

标签: node.js redis

我是redis的新手,如果在redis中发生任何chages /事务,我需要帮助才能通知我正在运行的服务器(也可以来自bakend)。使用node.js

创建处于运行状态的服务器

2 个答案:

答案 0 :(得分:0)

使用密钥空间通知系统。这使用了redis的pub子层

步骤: 在redis.conf文件中设置此参数-notify-keyspace-events 默认为空

这将捕获几乎所有DML事件

答案 1 :(得分:0)

我不确定如何获取有关交易的通知,但这是一个工作代码(使用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`)
   }
})

其他信息: