CartoDB-SQL-API/app/models/redis_pool.js
2011-06-16 18:23:38 +02:00

55 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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, //60000, //100 in dev
reapIntervalMillis: 1000, //10 in dev
log: false
});
}
}
module.exports = RedisPool;