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

This commit is contained in:
indexzero 2011-04-05 00:09:46 -04:00
parent 81e1883df5
commit 4094125b30

View file

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