Created
June 11, 2013 04:20
-
-
Save rishabhmhjn/5754463 to your computer and use it in GitHub Desktop.
Implementation of Redis connection pooling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redisConfig = { | |
"host" : "localhost", | |
"port" : "6379", | |
"maxConnections" : 10, | |
"minConnections" : 5, | |
"debug" : true | |
} | |
var redis = require("redis"); | |
var genericPool = require('generic-pool'); | |
// refer to https://github.com/coopernurse/node-pool#documentation for pool options | |
var redisPool = genericPool.Pool({ | |
name : 'redis', | |
create : function (callback) { | |
var client = redis.createClient(redisConfig["port"], | |
redisConfig["host"]); | |
client.on("error", function (err) { | |
console.error(err); | |
}); | |
client._end = client.end; | |
client.end = function () { | |
redisPool.release(client); | |
}; | |
callback(null, client); | |
}, | |
destroy : function (client) { | |
client._end(); | |
}, | |
max : redisConfig["maxConnections"], | |
min : redisConfig["minConnections"], | |
idleTimeoutMillis : 30000, | |
log : redisConfig["debug"] | |
}); | |
redisPool.acquire(function (err, client) { | |
client.get("key", function (err, value) { | |
// do whatever you want | |
// call client.end() to return it back to the pool | |
client.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment