简体   繁体   English

如何使用 Node.js 获取 Redis 中的连接数?

[英]How to get number of connections in Redis using Node.js?

Is there a way to check how many current connections there are to the Redis database?有没有办法检查 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 );
});

according to redis documentation try this:根据 redis文档试试这个:

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

}

the output: output:

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

CLIENT LIST returns information and statistics about the client connections server in a mostly human readable format as @babak stated in the answer. CLIENT LIST以大多数人类可读的格式返回有关客户端连接服务器的信息和统计信息,如答案中所述的@babak。

If you just need to get total number of connections you may use INFO command.如果您只需要获取total number of connections您可以使用INFO命令。 INFO clients will print something like this and connected_clients would be what you are looking for. 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

The following code do the work;以下代码完成工作;

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

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

clients();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM