redis / node-redis

Redis Node.js client
https://redis.js.org/
MIT License
16.89k stars 1.88k forks source link

3 socket writes instead of one? #2703

Open amochkin opened 7 months ago

amochkin commented 7 months ago

Hi, @everyone!

Can someone please explain why is this happening:

https://github.com/redis/node-redis/blob/4f85030e42da2eed6a178e54994330af5062761e/index.js#L896-L899

Let`s concatenate the data and send once.

ljluestc commented 3 weeks ago

const redis = require('redis');
const client = redis.createClient();

const commands = [
  client.set('key1', 'value1'),
  client.set('key2', 'value2'),
  client.set('key3', 'value3')
];

const buffer = Buffer.alloc(0);

commands.forEach(command => {
  const commandBuffer = Buffer.from(command);
  buffer = Buffer.concat([buffer, commandBuffer]);
});

client.send_command('MULTI', [buffer], (err, res) => {
  if (err) {
    console.error('Error sending commands:', err);
  } else {
    console.log('Commands sent successfully:', res);
  }
});