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
廣告