[英]How to get redis value for a given redis key using 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.