From 0af1535383fd7351597c07de2a6bd50477bddff8 Mon Sep 17 00:00:00 2001 From: Sean Cady Date: Mon, 2 Feb 2015 15:28:37 -0500 Subject: [PATCH] Added Options capability to Get, Set, and Delete. This can be used to pass additional parameters to the cache services. --- lib/caching.js | 7 +- lib/multi_caching.js | 28 ++++---- test/stores/options.unit.js | 134 ++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 15 deletions(-) create mode 100644 test/stores/options.unit.js diff --git a/lib/caching.js b/lib/caching.js index 490de42..e057702 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -36,9 +36,10 @@ var caching = function(args) { * console.log(user); * }); */ - self.wrap = function(key, work, ttl, cb) { + self.wrap = function(key, work, ttl, cb, options) { if (typeof ttl === 'function') { cb = ttl; + options = cb; ttl = undefined; } @@ -79,10 +80,10 @@ var caching = function(args) { } else { fillCallbacks(null, data); } - }); + }, options); }); } - }); + }, options); }; self.get = self.store.get.bind(self.store); diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 2719a19..a426bf5 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -10,7 +10,7 @@ var multi_caching = function(caches) { self.queues = {}; - function get_from_highest_priority_cache(key, cb) { + function get_from_highest_priority_cache(key, cb, options) { var i = 0; async.forEachSeries(caches, function(cache, async_cb) { cache.store.get(key, function(err, result) { @@ -22,13 +22,13 @@ var multi_caching = function(caches) { i += 1; async_cb(err); - }); + }, options); }, cb); } function set_in_multiple_caches(caches, opts, cb) { async.forEach(caches, function(cache, async_cb) { - cache.store.set(opts.key, opts.value, opts.ttl, async_cb); + cache.store.set(opts.key, opts.value, opts.ttl, async_cb, opts.options); }, cb); } @@ -64,9 +64,10 @@ var multi_caching = function(caches) { * If a key doesn't exist in a higher-priority cache but exists in a lower-priority * cache, it gets set in all higher-priority caches. */ - self.wrap = function(key, work, ttl, cb) { + self.wrap = function(key, work, ttl, cb, options) { if (typeof ttl === 'function') { cb = ttl; + options = cb; ttl = undefined; } @@ -93,7 +94,8 @@ var multi_caching = function(caches) { var opts = { key: key, value: result, - ttl: ttl + ttl: ttl, + options: options }; set_in_multiple_caches(caches_to_update, opts, function(err) { fillCallbacks(err, result); @@ -112,7 +114,8 @@ var multi_caching = function(caches) { var opts = { key: key, value: data, - ttl: ttl + ttl: ttl, + options: options }; set_in_multiple_caches(caches, opts, function(err) { if (err) { @@ -126,22 +129,23 @@ var multi_caching = function(caches) { }); }; - self.set = function(key, value, ttl, cb) { + self.set = function(key, value, ttl, cb, options) { var opts = { key: key, value: value, - ttl: ttl + ttl: ttl, + options: options }; set_in_multiple_caches(caches, opts, cb); }; - self.get = function(key, cb) { - get_from_highest_priority_cache(key, cb); + self.get = function(key, cb, options) { + get_from_highest_priority_cache(key, cb, options); }; - self.del = function(key, cb) { + self.del = function(key, cb, options) { async.forEach(caches, function(cache, async_cb) { - cache.store.del(key, async_cb); + cache.store.del(key, async_cb, options); }, cb); }; diff --git a/test/stores/options.unit.js b/test/stores/options.unit.js new file mode 100644 index 0000000..1dd531b --- /dev/null +++ b/test/stores/options.unit.js @@ -0,0 +1,134 @@ +var caching = require("../../index"); +var assert = require("assert"); +var support = require('../support'); +var memoryFlag = ""; +var key; +var value; +var testStore = function(args) { + args = args || {}; + var self = {}; + self.name = "options"; + + self.get = function(key, cb, options) { + if (options && options.value) { + return cb(null, options.value + "ValueOption"); + } else if (options && options.fn) { + options.fn("GetFunctionOption"); + return cb(null, "GetFunctionOption"); + } + return cb("Error No Options"); + }; + + self.set = function(key, value, ttl, cb, options) { + if (options && options.value) { + memoryFlag = options.value + "ValueOption"; + return cb(); + } else if (options && options.fn) { + options.fn("SetFunctionOption"); + return cb(); + } + return cb("Error No Options"); + }; + + self.del = function(key, cb, options) { + if (options && options.value) { + memoryFlag = options.value + "ValueOption"; + return cb(); + } else if (options && options.fn) { + options.fn("DeleteFunctionOption"); + return cb(); + } + return cb("Error No Options"); + }; + + return { + create: function() { + return self; + } + }; +}; + +describe("Methods with options", function() { + before(function() { + key = support.random.string(20); + value = support.random.string(20); + }); + describe("get with options", function() { + var testInstance = caching.caching({store: testStore()}); + var testCache; + before(function() { + testCache = caching.multi_caching([testInstance]); + }); + + it("lets us pass options by value", function(done) { + var options = {value: value}; + testCache.get(key, function(err, response) { + assert.equal(response, value + "ValueOption"); + done(); + }, options); + }); + + it("lets us pass options by function", function(done) { + var options = { + fn: function(response) { + assert.equal(response, "GetFunctionOption"); + done(); + } + }; + testCache.get(key, function(err, response) { + assert.equal(response, "GetFunctionOption"); + }, options); + }); + }); + describe("set with options", function() { + var testInstance = caching.caching({store: testStore()}); + var testCache; + var ttl = 60; + before(function() { + testCache = caching.multi_caching([testInstance]); + }); + + it("lets us pass options by value", function(done) { + var options = {value: value}; + testCache.set(key, value, ttl, function() { + assert.equal(memoryFlag,value + "ValueOption"); + done(); + }, options); + }); + + it("lets us pass options by function", function(done) { + var options = { + fn: function(response) { + assert.equal(response, "SetFunctionOption"); + done(); + } + }; + testCache.set(key, value, ttl, function() {}, options); + }); + }); + describe("delete with options", function() { + var testInstance = caching.caching({store: testStore()}); + var testCache; + before(function() { + testCache = caching.multi_caching([testInstance]); + }); + + it("lets us pass options by value", function(done) { + var options = {value: value}; + testCache.del(key, function() { + assert.equal(memoryFlag,value + "ValueOption"); + done(); + }, options); + }); + + it("lets us pass options by function", function(done) { + var options = { + fn: function(response) { + assert.equal(response, "DeleteFunctionOption"); + done(); + } + }; + testCache.del(key, function() {}, options); + }); + }); +});