Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
function addValue (key, next) {
self.get(key, function (err, value) {
if (err) {
return next(err);
}
result[key] = value;
next();
});
}
keys = keys || [];
Redis.prototype.merge = function (key, value, callback) {
//
// If the key is not an `Object` or is an `Array`,
// then simply set it. Merging is for Objects.
//
if (typeof value !== 'object' || Array.isArray(value)) {
return this.set(key, value, callback);
}
var self = this,
path = nconf.path(key),
fullKey = nconf.key(this.namespace, key);
// Set the callback if not provided for "fire and forget"
callback = callback || function () { };
//
// Get the set of all children keys for the `key` supplied. If the value
// to be returned is an Object, this list will not be empty.
//
this._addKeys(key, function (err) {
self.redis.smembers(nconf.key(fullKey, 'keys'), function (err, keys) {
function nextMerge (nested, next) {
var keyPath = nconf.key.apply(null, path.concat([nested]));
self.merge(keyPath, value[nested], next);
}
if (keys && keys.length > 0) {
function removeValue (child, next) {
//
// Recursively call `self.clear` here to ensure we remove any
// nested Objects completely from this instance.
//
self.clear(nconf.key(key, child), next);
}
function addValue (child, next) {
//
// Add the child key to the parent key-set, then set the value.
// Recursively call `_setObject` in the event of nested Object(s).
//
self.redis.sadd(nconf.key(key, 'keys'), child, function (err) {
if (err) {
return next(err);
}
var fullKey = nconf.key(key, child),
childValue = value[child];
if (!Array.isArray(childValue) && typeof childValue === 'object') {
self._setObject(fullKey, childValue, next);
}
else {
childValue = JSON.stringify(childValue);
self.redis.set(fullKey, childValue, next);
}
});
}
Redis.prototype.get = function (key, callback) {
var self = this,
result = {},
now = Date.now(),
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.
//
if (mtime && now - mtime < this.ttl) {
return callback(null, this.cache.get(key));
}
//
// Get the set of all children keys for the `key` supplied. If the value
// to be returned is an Object, this list will not be empty.
//
this._addKeys(key, function (err) {
self.redis.smembers(nconf.key(fullKey, 'keys'), function (err, keys) {
function nextMerge (nested, next) {
var keyPath = nconf.key.apply(null, path.concat([nested]));
self.merge(keyPath, value[nested], next);
}
if (keys && keys.length > 0) {
//
// If there are existing keys then we must do a recursive merge
// of the two Objects.
//
return async.forEach(Object.keys(value), nextMerge, callback);
}
//
// Otherwise, we can simply invoke `set` to override the current
// literal or Array value with our new Object value
this._addKeys(key, function (err) {
if (err) {
return callback(err);
}
var fullKey = nconf.key(self.namespace, key);
if (!Array.isArray(value) && value !== null && typeof value === 'object') {
//
// If the value is an `Object` (and not an `Array`) then
// nest into the value and set the child keys appropriately.
// This is done for efficient lookup when setting Object keys.
// (i.e. If you set and Object then wish to later retrieve only a
// member of that Object, the entire Object need not be retrieved).
//
self.cache.set(key, value);
self._setObject(fullKey, value, callback);
}
else {
//
// If the value is a simple literal (or an `Array`) then JSON
// stringify it and put it into Redis.
Redis.prototype.clear = function (key, callback) {
var self = this,
result = {},
path = [this.namespace].concat(nconf.path(key)),
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
//
this.cache.clear(key);
//
// Remove the `key` from the parent set of keys.
//
this.redis.srem(nconf.key.apply(null, path.concat(['keys'])), last, function (err) {
//
// Remove the value from redis by iterating over the set of keys (if any)
// and deleting each value. If no keys, then just delete the simple literal.
function addValue (source, next) {
self.get(nconf.key(key, source), function (err, value) {
if (err) {
return next(err);
}
result[source] = value;
next();
});
}
self.redis.sadd(nconf.key(key, 'keys'), child, function (err) {
if (err) {
return next(err);
}
var fullKey = nconf.key(key, child),
childValue = value[child];
if (!Array.isArray(childValue) && typeof childValue === 'object') {
self._setObject(fullKey, childValue, next);
}
else {
childValue = JSON.stringify(childValue);
self.redis.set(fullKey, childValue, next);
}
});
}