Node.js – Redis 中的监控模式
node.jsserver side programmingprogramming
Redis 还支持 monitor 命令,该命令允许用户查看 Redis 服务器在所有客户端连接中收到的所有命令。这些连接包含来自各个地方的命令,包括其他客户端库和计算机。
monitor 事件将监控在启用 monitor 的 Redis 服务器上执行的所有命令。来自 monitor 的回调会接收来自 Redis 服务器的时间戳、命令数组以及原始监控字符串。
语法
client.monitor( function(callback) )
示例 1
创建一个名为"monitor.js"的文件并复制以下代码。创建文件后,使用命令"nodemonitor.js"运行此代码,如下例所示:
// 监控模式示例 // 导入 redis 模块 const redis = require("redis"); // 创建 redis 客户端 const client = redis.createClient(); // 声明我们正在进入监控模式 client.monitor(function(err, res) { console.log("进入监控模式。"); }); // 设置 redis 中的值 client.set("hello", "tutorialspoint"); // 定义监控输出 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
我们再举一个例子 −
// 监控模式示例 // 导入 redis 模块 const redis = require("redis"); // 创建 redis 客户端 const client = redis.createClient(); // 声明我们正在进入监控模式 client.monitor(function(err, res) { console.log("Entering monitoring mode."); }); // 在 redis 中设置值 client.set("hello", "tutorialspoint"); client.get("hello"); client.set("key", "value"); client.set("key2", "value2"); client.get("key2"); // 定义监视器输出 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