Node.js – 在 Redis 中的监视模式


Redis 还支持 monitor 命令,该命令允许用户查看 Redis 服务器通过所有客户端连接接收到的所有命令。这些连接包括来自所有地方的命令,包括其他客户端库和计算机。

monitor 事件将监视在启用了 monitor 的 Redis 服务器上执行的所有命令。monitor 的回调从 Redis 服务器接收时间戳、一个命令数组以及原始监控字符串。

语法

client.monitor( function(callback) )

示例 1

创建一个名为 "monitor.js" 的文件,并复制以下代码。创建文件后,使用命令 "node monitor.js" 运行此代码,如下面示例中所示

// Monitor mode Demo Example

// Importing the redis module
const redis = require("redis");

// Creating redis client
const client = redis.createClient();

// Stating that we are entering into the monitor mode
client.monitor(function(err, res) {
   console.log("Entering monitoring mode.");
});

// Setting value in redis
client.set("hello", "tutorialspoint");

// Defining the monitor output
client.on("monitor", function(time, args, rawReply) {
   console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar']
});

输出

Entering monitoring mode.
1623087421.448855: set,hello,tutorialspoint

示例 2

我们再看一个示例 −

// Monitor mode Demo Example

// Importing the redis module
const redis = require("redis");

// Creating redis client
const client = redis.createClient();

// Stating that we are entering into the monitor mode
client.monitor(function(err, res) {
   console.log("Entering monitoring mode.");
});

// Setting value in redis
client.set("hello", "tutorialspoint");
client.get("hello");
client.set("key", "value");
client.set("key2", "value2");
client.get("key2");

// Defining the monitor output
client.on("monitor", function(time, args, rawReply) {
   console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar']
});

输出

Entering monitoring mode.
1623088106.382888: set,hello,tutorialspoint
1623088106.382902: get,hello
1623088106.382919: set,key,value
1623088106.383023: set,key2,value2
1623088106.383033: get,key2

更新于:24-11-2021

489 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.