Memory.prototype.set = function (key, value) { var target = this.store, path = nconf.path(key); // // Update the `mtime` (modified time) of the key // this.mtimes[key] = Date.now(); // // Scope into the object to get the appropriate nested context // while (path.length > 1) { key = path.shift(); if (!target[key]) { target[key] = {}; } target = target[key]; } // Set the specified value in the nested JSON structure key = path.shift(); target[key] = value; return true; };
Memory.prototype.clear = function (key) { var target = this.store, path = nconf.path(key); // // Remove the key from the set of `mtimes` (modified times) // delete this.mtimes[key]; // // Scope into the object to get the appropriate nested context // while (path.length > 1) { key = path.shift(); if (!target[key]) { return; } target = target[key]; } // Delete the key from the nested JSON structure key = path.shift(); delete target[key]; return true; };
Mongodb.resolveKey = function(key) { 'use strict'; var path = nconf.path(key); path.splice(1, 0, 'value'); return nconf.key.apply(null, path); };
process.nextTick(function() { var root = nconf.path(key)[0], document = self.cache.get(root); self._collection.save(document, {safe:true}, function(err) { if (err) console.error(err); }); });
Mongodb.prototype.clear = function(key) { 'use strict'; var self = this, root = null, original = null, success = false, modified = false; key = Mongodb.resolveKey(key); root = nconf.path(key)[0], original = this.cache.get(root); success = this.cache.clear(key); modified = this.cache.get(root); if (success) { process.nextTick(function() { if (modified.value === undefined) { self._collection.save(modified, {safe: true}, function(err) { if (err) console.error(err); }); } else { self._collection.remove(original._id, {safe: true}, function(err, count) { if (err || !count) console.error(err || 'Record not removed'); }); } }); } return success; };
Mongodb.prototype.set = function(key, value) { 'use strict'; var self = this, root = null, fullKey = null, document = null, success = true; key = Mongodb.resolveKey(key); root = nconf.path(key)[0]; document = this.get(root); if (!document) { // Document doesn't yet exist in the cache, so // create an empty one and add it. fullKey = nconf.key(this.namespace, root); document = {key: fullKey, value: {}, app: self.app}; success = this.cache.set(root, document); } if (success) { // Document already existed or a default one was successfully created // so update with the specified value success = this.cache.set(key, value); } return success; };
process.nextTick(function() { var root = nconf.path(key)[0], fullKey = nconf.key(self.namespace, root); self._collection.findOne({key:fullKey}, function(err, document) { if (err) return console.error(err); self.cache.set(root, document); }); });
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. // self.redis.smembers(nconf.key(fullKey, 'keys'), function (err, keys) { 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); } if (keys && keys.length > 0) { // // If there are child keys then iterate over them, // removing each child along the way. // async.forEach(keys, removeValue, callback); } else { // // Otherwise if this is just a simple literal, then // simply remove it from Redis directly. // self.redis.del(fullKey, callback); } }); }); };
Redis.prototype._addKeys = function (key, callback) { var self = this, path = nconf.path(key); function addKey (partial, next) { var index = path.indexOf(partial), base = [self.namespace].concat(path.slice(0, index)), parent = nconf.key.apply(null, base.concat(['keys'])); self.redis.sadd(parent, partial, next); }; // // Iterate over the entire key path and add each key to the // parent key-set if it doesn't exist already. // async.forEach(path, addKey, callback); };
Memory.prototype.get = function (key) { var target = this.store, path = nconf.path(key); // // Scope into the object to get the appropriate nested context // while (path.length > 0) { key = path.shift(); if (!target[key]) { return; } target = target[key]; if (path.length === 0) { return target; } } };
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) { // // 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 // self.set(key, value, callback); }); }); };
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 () { }; 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. // self.cache.set(key, value); value = JSON.stringify(value); self.redis.set(fullKey, value, callback); } }); };
docs.forEach(function(item) { // Fill the cache path = nconf.path(item.key); self.cache.set(path[1], item); });