Do I need to quit my node redis client instance us

2019-07-16 13:27发布

问题:

Looking at this code below (taken from the git page)

var redis  = require("redis"),
    client = redis.createClient(), multi;

// runs immediately
client.mset("incr thing", 100, "incr other thing", 1, redis.print);
multi = client.multi();

// drains multi queue and runs atomically
multi.exec(function (err, replies) {
    console.log(replies); // 101, 2
    client.quit(); // IS THIS OPTIONAL?
});

I want to know if client.quit() is optional, or if multi.exec() automatically performs a quit for me? I'm trying to debug a memory leak in my redis and I realized I am not using .quit() anywhere. Should I be?

Meaning, should my code look like this?

client = redis.createClient();
multi = clent.multi();
multi.exec( {something} );
client.quit();

Basically, where does client.quit go and do I even need it?

回答1:

"MULTI commands are queued up until an EXEC is issued, and then all commands are run atomically by Redis."

This is a example from github:

// multi chain with an individual callback
client.multi()
    .scard("bigset")
    .smembers("bigset")
    .keys("*", function (err, replies) {
        client.mget(replies, redis.print);
    })
    .dbsize()
    .exec(function (err, replies) {
        console.log("MULTI got " + replies.length + " replies");
        replies.forEach(function (reply, index) {
            console.log("Reply " + index + ": " + reply.toString());
        });
    });

Your question: do i need to use client.quit() ? Yes, you need to because your redis connection will not be closed until you restart your redis server. You should use client.quit() when all your process have been completed. (In your last callback for example)



标签: node.js redis