简体   繁体   English

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

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

I'am new to redis, i need help to get notify to my running server if any chages/transaction happen in redis(either from bakend also).我是 redis 的新手,如果 redis 中发生任何更改/事务(也来自巴恩德),我需要帮助来通知我正在运行的服务器。 The server which is in running state is created using node.js使用 node.js 创建处于运行状态的服务器

Use the keyspace notification system.使用密钥空间通知系统。 This uses the pub sub layer of redis这里使用了redis的pub子层

Steps : Set this parameter -notify-keyspace-events in redis.conf file By default its empty步骤:在redis.conf文件中设置此参数-notify-keyspace-events 默认为空

This will enable capture of almost all DML events这将能够捕获几乎所有 DML 事件

I'm not sure about how to get notification about transaction, but here is a working code (using ioredis ) to get notified about any change in redis:我不确定如何获得有关交易的通知,但这里有一个工作代码(使用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`)
   }
})

Addtional info:补充资料:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM