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

Node.js 如果使用nodejs在redis中发生任何更改/事务,如何获取通知,node.js,redis,Node.js,Redis,我是redis新手,如果redis中发生任何更改/事务,我需要帮助从bakend获得通知到我正在运行的服务器。处于运行状态的服务器是使用node创建的。js使用密钥空间通知系统。这将使用redis的发布子层 步骤: 设置此参数-在redis.conf文件中通知键空间事件 默认情况下,它是空的 这将允许捕获几乎所有的DML事件我不确定如何获取有关事务的通知,但下面是一个使用ioredis获取有关redis中任何更改的通知的工作代码: const redisClient = const redisC

我是redis新手,如果redis中发生任何更改/事务,我需要帮助从bakend获得通知到我正在运行的服务器。处于运行状态的服务器是使用node创建的。js使用密钥空间通知系统。这将使用redis的发布子层

步骤: 设置此参数-在redis.conf文件中通知键空间事件 默认情况下,它是空的


这将允许捕获几乎所有的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`)
   }
})
附加信息:

例子: 激活通知: