如何使用node.js获取redis中的连接数?

koaltpgm  于 2021-06-09  发布在  Redis
关注(0)|答案(2)|浏览(424)

有没有办法检查到redis数据库的当前连接数?

const redis = require('redis');

var client = redis.createClient(port, host);

client.on('connect', () => {
  //Display how many current connections are to Redis
  console.log( numberOfRedisClients );
});
wnvonmuf

wnvonmuf1#

client list返回有关客户机连接服务器的信息和统计信息,格式基本上是可读的,正如@babak在回答中所说的那样。
如果你只需要 total number of connections 您可以使用info命令。 INFO clients 会打印这样的东西 connected_clients 就是你要找的。


# Clients

connected_clients:2
client_recent_max_input_buffer:8
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0

以下代码执行此工作;

const Redis = require("ioredis");
const redis = new Redis();

async function clients() {
    console.log(await redis.info('clients'));
}

clients();
zbdgwd5y

zbdgwd5y2#

根据redis文档,请尝试以下操作:

const Redis = require("ioredis");
let client =  new Redis();
async function my_function(){
  console.log(await client.send_command('CLIENT', 'LIST'))

}

输出:

id=24 addr=127.0.0.1:47962 fd=8 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=info
id=25 addr=127.0.0.1:47964 fd=9 name= age=0 idle=0 flags=P db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=26 addr=127.0.0.1:47966 fd=10 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client

相关问题