[api] Add support for Redis auth and optional callbacks.

master
indexzero 2011-04-05 00:09:46 -04:00
parent 81e1883df5
commit 4094125b30
1 changed files with 22 additions and 0 deletions

View File

@ -31,6 +31,10 @@ var Redis = exports.Redis = function (options) {
this.ttl = options.ttl || 60 * 60 * 1000;
this.cache = new Memory();
this.redis = redis.createClient(options.port, options.host);
if (options.auth) {
this.redis.auth(options.auth);
}
};
//
@ -46,6 +50,9 @@ Redis.prototype.get = function (key, callback) {
mtime = this.cache.mtimes[key],
fullKey = nconf.key(this.namespace, key);
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
//
// If the key exists in the cache and the ttl is less than
// the value set for this instance, return from the cache.
@ -117,6 +124,9 @@ Redis.prototype.set = function (key, value, callback) {
var self = this,
path = nconf.path(key);
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
function addKey (partial, next) {
var index = path.indexOf(partial),
base = [self.namespace].concat(path.slice(0, index)),
@ -171,6 +181,9 @@ Redis.prototype.clear = function (key, callback) {
last = path.pop(),
fullKey = nconf.key(this.namespace, key);
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
//
// Clear the key from the cache for this instance
//
@ -226,6 +239,9 @@ Redis.prototype.save = function (value, callback) {
var self = this,
keys = Object.keys(value);
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
//
// Clear all existing keys associated with this instance.
//
@ -252,6 +268,9 @@ Redis.prototype.load = function (callback) {
var self = this,
result = {};
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
this.redis.smembers(nconf.key(this.namespace, 'keys'), function (err, keys) {
if (err) {
return callback(err);
@ -282,6 +301,9 @@ Redis.prototype.load = function (callback) {
Redis.prototype.reset = function (callback) {
var self = this;
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
//
// Get the list of of top-level keys, then clear each of them
//