From 4094125b30ea673938f6e72f5c0a73d380beb186 Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 5 Apr 2011 00:09:46 -0400 Subject: [PATCH] [api] Add support for Redis auth and optional callbacks. --- lib/nconf/stores/redis.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/nconf/stores/redis.js b/lib/nconf/stores/redis.js index b9d7b15..9e99760 100644 --- a/lib/nconf/stores/redis.js +++ b/lib/nconf/stores/redis.js @@ -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 //