Created
June 12, 2011 15:38
-
-
Save tokumine/1021680 to your computer and use it in GitHub Desktop.
A pool implementation for redis in node.js. 1 pool per redis database.
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 redis = require('redis') | |
, Pool = require('generic-pool').Pool; | |
var RedisPool = { | |
// Acquire resource. | |
// | |
// - `database` {String} redis database name | |
// - `callback` {Function} callback to call once acquired. Takes the form | |
// `callback(err, resource)` | |
acquire: function(database, callback) { | |
if (!this.pools[database]) { | |
this.pools[database] = this.makePool(database); | |
} | |
this.pools[database].acquire(function(resource) { | |
callback(resource); | |
}); | |
}, | |
// Release resource. | |
// | |
// - `database` {String} redis database name | |
// - `resource` {Object} resource object to release | |
release: function(database, resource) { | |
this.pools[database] && this.pools[database].release(resource); | |
}, | |
// Cache of pools by database name. | |
pools: {}, | |
// Factory for pool objects. | |
makePool: function(database) { | |
return Pool({ | |
name: database, | |
create: function(callback){ | |
var client = redis.createClient(global.settings.redis_port, global.settings.redis_host); | |
client.on('connect', function () { | |
client.send_anyway = true; | |
client.select(database); | |
client.send_anyway = false; | |
}); | |
return callback(client); | |
}, | |
destroy: function(client) { | |
return client.quit(); | |
}, | |
max: 50, | |
idleTimeoutMillis: 10000, | |
reapIntervalMillis: 1000, | |
log: false | |
}); | |
} | |
} | |
module.exports = RedisPool; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment