From eb986dc9095133e458e61438ba3bbc170c21bc63 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 5 Feb 2015 11:18:40 -0800 Subject: [PATCH 01/12] fixing bullet lists in History.md --- History.md | 76 +++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/History.md b/History.md index 01135fe..f6eb905 100644 --- a/History.md +++ b/History.md @@ -1,94 +1,94 @@ - 0.17.0 2015-02-05 - Add Additional Options Parameter (#20) - @seanzx85 - Fixing bug with nested calls to wrap() (#21) + - Add Additional Options Parameter (#20) - @seanzx85 + - Fixing bug with nested calls to wrap() (#21) - 0.16.0 2015-01-07 - Get and pass up feature to update higher caches. (#19) - raadad - Minor style tweaks/jscs update. + - Get and pass up feature to update higher caches. (#19) - raadad + - Minor style tweaks/jscs update. - 0.15.0 2014-12-18 - Moved cache queue before the store get function (up to 2x performance boost). (#18) - aletorrado - Added domain support to make sure the wrap callback function is always called - aletorrado + - Moved cache queue before the store get function (up to 2x performance boost). (#18) - aletorrado + - Added domain support to make sure the wrap callback function is always called - aletorrado - 0.14.0 2014-10-15 - Set ttl in wrap #14 - nguyenchr - Added JSCS for style checking + - Set ttl in wrap #14 - nguyenchr + - Added JSCS for style checking - 0.13.0 2014-10-14 - Applied work function locking for multi_caching (#13). -aletorrado + - Applied work function locking for multi_caching (#13). -aletorrado - 0.12.0 2014-10-09 - Checking for existence of del() method before binding to it. Fixes #11. + - Checking for existence of del() method before binding to it. Fixes #11. - 0.11.0 2014-09-18 - Prevent stalemate by executing callbacks on error. Fixes #10 - elliotttf + - Prevent stalemate by executing callbacks on error. Fixes #10 - elliotttf - 0.10.1 2014-09-10 - Fixed tag/version mismatch + - Fixed tag/version mismatch - 0.10.0 2014-09-10 - Fixing Use call instead of apply for cached results, issue #9 (thanks elliotttf) + - Fixing Use call instead of apply for cached results, issue #9 (thanks elliotttf) - 0.9.0 2014-08-19 - Fixing issue #8 - parallel requests to a wrapped function were calling the - function multiple times. (Thanks alex-whitney). + - Fixing issue #8 - parallel requests to a wrapped function were calling the + function multiple times. (Thanks alex-whitney). - 0.8.0 2014-07-07 - Adding setex() (Thanks evanlucas) + - Adding setex() (Thanks evanlucas) - 0.7.1 2014-06-15 - Adding link to Express.js cache-manager example app + - Adding link to Express.js cache-manager example app - 0.7.0 2014-06-15 - Bumping package versions, mostly devDependencies + - Bumping package versions, mostly devDependencies - 0.6.0 2014-06-15 - Adding caching.keys() function (issue #6) - Updating examples/redis_example/example.js with cache.keys() usage - Allow calling memory store get() without callback + - Adding caching.keys() function (issue #6) + - Updating examples/redis_example/example.js with cache.keys() usage + - Allow calling memory store get() without callback - 0.5.0 2014-05-02 - Adding reset() function to caching.js. Closes #5. + - Adding reset() function to caching.js. Closes #5. - 0.4.0 2014-05-02 - New arg to ignore cache errors. if set cache errors will be ignored - and the cache_manager will go to the backing store. (Thanks londonjamo). + - New arg to ignore cache errors. if set cache errors will be ignored + and the cache_manager will go to the backing store. (Thanks londonjamo). - 0.3.0 2013-12-08 - Bound the get, set and del functions to their original “this” context when assigning a store. - (Thanks to Boyan Rabchev) + - Bound the get, set and del functions to their original “this” context when assigning a store. + (Thanks to Boyan Rabchev) - 0.2.0 2013-10-31 - Better examples, version bump. + - Better examples, version bump. - 0.1.3 2013-10-31 - Fixing unreleased connection in redis example. + - Fixing unreleased connection in redis example. - 0.1.2 2013-10-13 - Wrapping synchronous memory cache callbacks in process.nextTick() for the purists. + - Wrapping synchronous memory cache callbacks in process.nextTick() for the purists. - 0.1.1 2013-10-13 - Travis and Coveralls integration testing. + - Travis and Coveralls integration testing. - 0.1.0 2013-10-13 - Removing built-in Redis store to emphasize that you should plug in your own - cache store. + - Removing built-in Redis store to emphasize that you should plug in your own + cache store. - 0.0.5 2013-10-13 - Removing hiredis requirement. + - Removing hiredis requirement. - 0.0.4 2013-08-01 - Better error checking in multi_cache.wrap(); + - Better error checking in multi_cache.wrap(); - 0.0.3 2013-07-10 - Better error checking in cache.wrap(); + - Better error checking in cache.wrap(); - 0.0.2 2013-04-08 - Added ability to pass in a store module that isn't already instantiated. - E.g., + - Added ability to pass in a store module that isn't already instantiated. E.g., + ```javascript var store = require('/path/to/my_memory_store'); cache = caching({store: store}); ``` - 0.0.1 2013-04-08 - Initial release. + - Initial release. From 5bcc45fc6bb363f14c1e1255ba171cbc5e082737 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 12:07:57 -0800 Subject: [PATCH 02/12] extracting CallbackFiller class --- lib/caching.js | 31 +++++++++++-------------------- lib/callback_filler.js | 19 +++++++++++++++++++ lib/multi_caching.js | 31 +++++++++++-------------------- 3 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 lib/callback_filler.js diff --git a/lib/caching.js b/lib/caching.js index 6cb1841..7c35836 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -1,6 +1,7 @@ /*jshint maxcomplexity:15*/ /*jshint -W072 */ var domain = require('domain'); +var CallbackFiller = require('./callback_filler'); var caching = function(args) { args = args || {}; @@ -21,7 +22,7 @@ var caching = function(args) { // do we handle a cache error the same as a cache miss? self.ignoreCacheErrors = args.ignoreCacheErrors || false; - self.queues = {}; + var callbackFiller = new CallbackFiller(); /** * Wraps a function in cache. I.e., the first time the function is run, @@ -43,44 +44,34 @@ var caching = function(args) { options = undefined; } - if (self.queues[key]) { - self.queues[key].push({cb: cb, domain: process.domain}); + if (callbackFiller.queues[key]) { + callbackFiller.queues[key].push({cb: cb, domain: process.domain}); return; } - self.queues[key] = [{cb: cb, domain: process.domain}]; - - function fillCallbacks(err, data) { - var waiting = self.queues[key]; - delete self.queues[key]; - - waiting.forEach(function(task) { - var taskDomain = task.domain || domain.create(); - taskDomain.bind(task.cb)(err, data); - }); - } + callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; self.store.get(key, options, function(err, result) { if (err && (!self.ignoreCacheErrors)) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else if (result) { - fillCallbacks(null, result); + callbackFiller.fill(key, null, result); } else { domain .create() .on('error', function(err) { - fillCallbacks(err); + callbackFiller.fill(key, err); }) .bind(work)(function(err, data) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); return; } self.store.set(key, data, options, function(err) { if (err && (!self.ignoreCacheErrors)) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else { - fillCallbacks(null, data); + callbackFiller.fill(key, null, data); } }); }); diff --git a/lib/callback_filler.js b/lib/callback_filler.js new file mode 100644 index 0000000..163aa7e --- /dev/null +++ b/lib/callback_filler.js @@ -0,0 +1,19 @@ +var domain = require('domain'); + +function CallbackFiller() { + this.queues = {}; +} + +CallbackFiller.prototype.fill = function(key, err, data) { + var self = this; + + var waiting = self.queues[key]; + delete self.queues[key]; + + waiting.forEach(function(task) { + var taskDomain = task.domain || domain.create(); + taskDomain.bind(task.cb)(err, data); + }); +}; + +module.exports = CallbackFiller; diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 18bfe5e..72d67df 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -1,5 +1,6 @@ var async = require('async'); var domain = require('domain'); +var CallbackFiller = require('./callback_filler'); /** * Module that lets you specify a hierarchy of caches. @@ -10,7 +11,7 @@ var multi_caching = function(caches) { throw new Error('multi_caching requires an array of caches'); } - self.queues = {}; + var callbackFiller = new CallbackFiller(); function get_from_highest_priority_cache(key, options, cb) { if (typeof options === 'function') { @@ -88,26 +89,16 @@ var multi_caching = function(caches) { options = undefined; } - if (self.queues[key]) { - self.queues[key].push({cb: cb, domain: process.domain}); + if (callbackFiller.queues[key]) { + callbackFiller.queues[key].push({cb: cb, domain: process.domain}); return; } - self.queues[key] = [{cb: cb, domain: process.domain}]; - - function fillCallbacks(err, data) { - var waiting = self.queues[key]; - delete self.queues[key]; - - waiting.forEach(function(task) { - var taskDomain = task.domain || domain.create(); - taskDomain.bind(task.cb)(err, data); - }); - } + callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; get_from_highest_priority_cache(key, function(err, result, index) { if (err) { - return fillCallbacks(err); + return callbackFiller.fill(key, err); } else if (result) { var caches_to_update = caches.slice(0, index); var opts = { @@ -121,17 +112,17 @@ var multi_caching = function(caches) { } set_in_multiple_caches(caches_to_update, opts, function(err) { - fillCallbacks(err, result); + callbackFiller.fill(key, err, result); }); } else { domain .create() .on('error', function(err) { - fillCallbacks(err); + callbackFiller.fill(key, err); }) .bind(work)(function(err, data) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); return; } var opts = { @@ -145,9 +136,9 @@ var multi_caching = function(caches) { } set_in_multiple_caches(caches, opts, function(err) { if (err) { - fillCallbacks(err); + callbackFiller.fill(key, err); } else { - fillCallbacks(null, data); + callbackFiller.fill(key, null, data); } }); }); From 6fc00781c7926091e716dc5bc899b9fd7614957e Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 12:09:54 -0800 Subject: [PATCH 03/12] changing async.forEach to each and async.forEachSeries to eachSeries --- lib/multi_caching.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 72d67df..6fbf3b2 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -20,7 +20,7 @@ var multi_caching = function(caches) { } var i = 0; - async.forEachSeries(caches, function(cache, async_cb) { + async.eachSeries(caches, function(cache, async_cb) { var callback = function(err, result) { if (err) { return cb(err); @@ -42,7 +42,7 @@ var multi_caching = function(caches) { } function set_in_multiple_caches(caches, opts, cb) { - async.forEach(caches, function(cache, async_cb) { + async.each(caches, function(cache, async_cb) { if (typeof opts.options !== 'object') { cache.store.set(opts.key, opts.value, opts.ttl, async_cb); } else { @@ -66,7 +66,7 @@ var multi_caching = function(caches) { if (result !== undefined && index) { var cachesToUpdate = caches.slice(0, index); - async.forEach(cachesToUpdate, function(cache, async_cb) { + async.each(cachesToUpdate, function(cache, async_cb) { cache.set(key, result, result.ttl, async_cb); }); } @@ -171,7 +171,7 @@ var multi_caching = function(caches) { cb = options; options = false; } - async.forEach(caches, function(cache, async_cb) { + async.each(caches, function(cache, async_cb) { if (typeof options === 'object') { cache.store.del(key, options, async_cb); } else { From 3b127c2c9ff99a4ddf275ddcca2da25a14abf49c Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 12:42:51 -0800 Subject: [PATCH 04/12] reluctantly converting to camelcase --- .jscs.json | 1 + README.md | 76 ++--- examples/example.js | 68 ++--- examples/redis_example/example.js | 50 ++-- examples/redis_example/redis_store.js | 10 +- index.js | 3 +- lib/caching.js | 4 +- lib/multi_caching.js | 53 ++-- lib/stores/memory.js | 18 +- test/caching.unit.js | 226 +++++++------- test/multi_caching.unit.js | 410 +++++++++++++------------- test/run.js | 26 +- test/stores/memory.unit.js | 6 +- test/stores/options.unit.js | 22 +- test/support.js | 34 +-- 15 files changed, 509 insertions(+), 498 deletions(-) diff --git a/.jscs.json b/.jscs.json index 84ebddd..c6d5947 100644 --- a/.jscs.json +++ b/.jscs.json @@ -12,6 +12,7 @@ "requireSpaceAfterBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], "disallowSpaceAfterBinaryOperators": ["!"], "disallowSpaceBeforeBinaryOperators": [","], + "requireCamelCaseOrUpperCaseIdentifiers": true, "disallowMultipleVarDecl": true, "disallowEmptyBlocks": true, diff --git a/README.md b/README.md index a81961b..4f91225 100644 --- a/README.md +++ b/README.md @@ -35,17 +35,17 @@ First, it includes a `wrap` function that lets you wrap any function in cache. This is probably the feature you're looking for. As an example, where you might have to do this: ```javascript -function get_cached_user(id, cb) { - memory_cache.get(id, function (err, result) { +function getCachedUser(id, cb) { + memoryCache.get(id, function (err, result) { if (err) { return cb(err); } if (result) { return cb(null, result); } - get_user(id, function (err, result) { + getUser(id, function (err, result) { if (err) { return cb(err); } - memory_cache.set(id, result); + memoryCache.set(id, result); cb(null, result); }); }); @@ -54,9 +54,9 @@ function get_cached_user(id, cb) { ... you can instead use the `wrap` function: ```javascript -function get_cached_user(id, cb) { - memory_cache.wrap(id, function (cache_callback) { - get_user(id, cache_callback); +function getCachedUser(id, cb) { + memoryCache.wrap(id, function (cacheCallback) { + getUser(id, cacheCallback); }, ttl, cb); } ``` @@ -86,40 +86,40 @@ Redis cache store with connection pooling. ### Single Store ```javascript -var cache_manager = require('cache-manager'); -var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); +var cacheManager = require('cache-manager'); +var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); var ttl = 5; // Note: callback is optional in set() and del(). -memory_cache.set('foo', 'bar', ttl, function(err) { +memoryCache.set('foo', 'bar', ttl, function(err) { if (err) { throw err; } - memory_cache.get('foo', function(err, result) { + memoryCache.get('foo', function(err, result) { console.log(result); // >> 'bar' - memory_cache.del('foo', function(err) {}); + memoryCache.del('foo', function(err) {}); }); }); -function get_user(id, cb) { +function getUser(id, cb) { setTimeout(function () { console.log("Returning user from slow database."); cb(null, {id: id, name: 'Bob'}); }, 100); } -var user_id = 123; -var key = 'user_' + user_id; +var userId = 123; +var key = 'user_' + userId; // Note: ttl is optional in wrap() -memory_cache.wrap(key, function (cb) { - get_user(user_id, cb); +memoryCache.wrap(key, function (cb) { + getUser(userId, cb); }, ttl, function (err, user) { console.log(user); - // Second time fetches user from memory_cache - memory_cache.wrap(key, function (cb) { - get_user(user_id, cb); + // Second time fetches user from memoryCache + memoryCache.wrap(key, function (cb) { + getUser(userId, cb); }, function (err, user) { console.log(user); }); @@ -143,10 +143,10 @@ function respond(res, err, data) { } app.get('/foo/bar', function(req, res) { - var cache_key = 'foo-bar:' + JSON.stringify(req.query); + var cacheKey = 'foo-bar:' + JSON.stringify(req.query); var ttl = 10; - memory_cache.wrap(cache_key, function(cache_cb) { - DB.find(req.query, cache_cb); + memoryCache.wrap(cacheKey, function(cacheCallback) { + DB.find(req.query, cacheCallback); }, ttl, function(err, result) { respond(res, err, result); }); @@ -162,45 +162,45 @@ in an instance of it, or pass in the path to the module. E.g., ```javascript -var my_store = require('your-homemade-store'); -var cache = cache_manager.caching({store: my_store}); +var myStore = require('your-homemade-store'); +var cache = cacheManager.caching({store: myStore}); // or -var cache = cache_manager.caching({store: '/path/to/your/store'}); +var cache = cacheManager.caching({store: '/path/to/your/store'}); ``` ### Multi-Store ```javascript -var multi_cache = cache_manager.multi_caching([memory_cache, some_other_cache]); -user_id2 = 456; -key2 = 'user_' + user_id; +var multiCache = cacheManager.multiCaching([memoryCache, someOtherCache]); +userId2 = 456; +key2 = 'user_' + userId; ttl = 5; // Sets in all caches. -multi_cache.set('foo2', 'bar2', ttl, function(err) { +multiCache.set('foo2', 'bar2', ttl, function(err) { if (err) { throw err; } // Fetches from highest priority cache that has the key. - multi_cache.get('foo2', function(err, result) { + multiCache.get('foo2', function(err, result) { console.log(result); // >> 'bar2' // Delete from all caches - multi_cache.del('foo2'); + multiCache.del('foo2'); }); }); // Note: ttl is optional in wrap() -multi_cache.wrap(key2, function (cb) { - get_user(user_id2, cb); +multiCache.wrap(key2, function (cb) { + getUser(userId2, cb); }, ttl, function (err, user) { console.log(user); - // Second time fetches user from memory_cache, since it's highest priority. + // Second time fetches user from memoryCache, since it's highest priority. // If the data expires in the memory cache, the next fetch would pull it from - // the 'some_other_cache', and set the data in memory again. - multi_cache.wrap(key2, function (cb) { - get_user(user_id2, cb); + // the 'someOtherCache', and set the data in memory again. + multiCache.wrap(key2, function (cb) { + getUser(userId2, cb); }, function (err, user) { console.log(user); }); diff --git a/examples/example.js b/examples/example.js index ed37064..3f979da 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,20 +1,20 @@ /*jshint unused:false*/ // Note: ttls are in seconds -var cache_manager = require('../'); -var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10}); -var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100}); +var cacheManager = require('../'); +var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10}); +var memoryCache2 = cacheManager.caching({store: 'memory', max: 100, ttl: 100}); var ttl; //Can't use a different ttl per set() call with memory cache // // Basic usage // -memory_cache.set('foo', 'bar', ttl, function(err) { +memoryCache.set('foo', 'bar', ttl, function(err) { if (err) { throw err; } - memory_cache.get('foo', function(err, result) { + memoryCache.get('foo', function(err, result) { console.log(result); // >> 'bar' - memory_cache.del('foo', function(err) { + memoryCache.del('foo', function(err) { if (err) { console.log(err); } @@ -22,49 +22,49 @@ memory_cache.set('foo', 'bar', ttl, function(err) { }); }); -function get_user(id, cb) { +function getUser(id, cb) { setTimeout(function() { console.log("Fetching user from slow database."); cb(null, {id: id, name: 'Bob'}); }, 100); } -var user_id = 123; -var key = 'user_' + user_id; +var userId = 123; +var key = 'user_' + userId; // // wrap() example // // Instead of manually managing the cache like this: -function get_cached_user_manually(id, cb) { - memory_cache.get(id, function(err, result) { +function getCachedUserManually(id, cb) { + memoryCache.get(id, function(err, result) { if (err) { return cb(err); } if (result) { return cb(null, result); } - get_user(id, function(err, result) { + getUser(id, function(err, result) { if (err) { return cb(err); } - memory_cache.set(id, result); + memoryCache.set(id, result); cb(null, result); }); }); } // ... you can instead use the `wrap` function: -function get_cached_user(id, cb) { - memory_cache.wrap(id, function(cache_callback) { - get_user(id, cache_callback); +function getCachedUser(id, cb) { + memoryCache.wrap(id, function(cacheCallback) { + getUser(id, cacheCallback); }, cb); } -get_cached_user(user_id, function(err, user) { +getCachedUser(userId, function(err, user) { // First time fetches the user from the (fake) database: console.log(user); - get_cached_user(user_id, function(err, user) { + getCachedUser(userId, function(err, user) { // Second time fetches from cache. console.log(user); }); @@ -76,14 +76,14 @@ get_cached_user(user_id, function(err, user) { // { id: 123, name: 'Bob' } // Same as above, but written differently: -memory_cache.wrap(key, function(cb) { - get_user(user_id, cb); +memoryCache.wrap(key, function(cb) { + getUser(userId, cb); }, function(err, user) { console.log(user); - // Second time fetches user from memory_cache - memory_cache.wrap(key, function(cb) { - get_user(user_id, cb); + // Second time fetches user from memoryCache + memoryCache.wrap(key, function(cb) { + getUser(userId, cb); }, function(err, user) { console.log(user); }); @@ -92,36 +92,36 @@ memory_cache.wrap(key, function(cb) { // // multi-cache example // -var multi_cache = cache_manager.multi_caching([memory_cache, memory_cache2]); -var user_id2 = 456; -var key2 = 'user_' + user_id; +var multiCache = cacheManager.multiCaching([memoryCache, memoryCache2]); +var userId2 = 456; +var key2 = 'user_' + userId; var ttl2; //Can't use a different ttl per set() call with memory cache -multi_cache.wrap(key2, function(cb) { - get_user(user_id2, cb); +multiCache.wrap(key2, function(cb) { + getUser(userId2, cb); }, function(err, user) { console.log(user); - // Second time fetches user from memory_cache, since it's highest priority. + // Second time fetches user from memoryCache, since it's highest priority. // If the data expires in the memory cache, the next fetch would pull it from // the Redis cache, and set the data in memory again. - multi_cache.wrap(key2, function(cb) { - get_user(user_id2, cb); + multiCache.wrap(key2, function(cb) { + getUser(userId2, cb); }, function(err, user) { console.log(user); }); // Sets in all caches. - multi_cache.set('foo2', 'bar2', ttl2, function(err) { + multiCache.set('foo2', 'bar2', ttl2, function(err) { if (err) { throw err; } // Fetches from highest priority cache that has the key. - multi_cache.get('foo2', function(err, result) { + multiCache.get('foo2', function(err, result) { console.log(result); // >> 'bar2' // Delete from all caches - multi_cache.del('foo2', function(err) { + multiCache.del('foo2', function(err) { if (err) { console.log(err); } diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js index 7de00bf..e7810e7 100644 --- a/examples/redis_example/example.js +++ b/examples/redis_example/example.js @@ -3,88 +3,88 @@ // node examples/redis_example/example.js var util = require('util'); -var cache_manager = require('../../'); -var redis_store = require('./redis_store'); +var cacheManager = require('../../'); +var redisStore = require('./redis_store'); // Note: ttl is in seconds -var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100}); +var redisCache = cacheManager.caching({store: redisStore, db: 0, ttl: 100}); var ttl = 60; console.log("set/get/del example:"); -redis_cache.set('foo', 'bar', {ttl: ttl}, function(err) { +redisCache.set('foo', 'bar', {ttl: ttl}, function(err) { if (err) { throw err; } - redis_cache.get('foo', function(err, result) { + redisCache.get('foo', function(err, result) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redis_cache.del('foo', function(err) { + redisCache.del('foo', function(err) { if (err) { throw err; } }); }); }); // TTL defaults to what we passed into the caching function (100) -redis_cache.set('foo-no-ttl', 'bar-no-ttl', function(err) { +redisCache.set('foo-no-ttl', 'bar-no-ttl', function(err) { if (err) { throw err; } - redis_cache.get('foo-no-ttl', function(err, result) { + redisCache.get('foo-no-ttl', function(err, result) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redis_cache.del('foo-no-ttl', function(err) { + redisCache.del('foo-no-ttl', function(err) { if (err) { throw err; } }); }); }); // Calls Redis 'set' instead of 'setex' -redis_cache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) { +redisCache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) { if (err) { throw err; } - redis_cache.get('foo-zero-ttl', function(err, result) { + redisCache.get('foo-zero-ttl', function(err, result) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redis_cache.del('foo-zero-ttl', function(err) { + redisCache.del('foo-zero-ttl', function(err) { if (err) { throw err; } }); }); }); -var user_id = 123; +var userId = 123; -function create_key(id) { +function createKey(id) { return 'user_' + id; } -function get_user(id, cb) { +function getUser(id, cb) { setTimeout(function() { console.log("\n\nReturning user from slow database."); cb(null, {id: id, name: 'Bob'}); }, 100); } -function get_user_from_cache(id, cb) { - var key = create_key(id); - redis_cache.wrap(key, function(cache_cb) { - get_user(user_id, cache_cb); +function getUserFromCache(id, cb) { + var key = createKey(id); + redisCache.wrap(key, function(cacheCb) { + getUser(userId, cacheCb); }, {ttl: ttl}, cb); } -get_user_from_cache(user_id, function(err, user) { +getUserFromCache(userId, function(err, user) { console.log(user); - // Second time fetches user from redis_cache - get_user_from_cache(user_id, function(err, user) { + // Second time fetches user from redisCache + getUserFromCache(userId, function(err, user) { console.log("user from second cache request:"); console.log(user); - redis_cache.keys(function(err, keys) { + redisCache.keys(function(err, keys) { console.log("keys: " + util.inspect(keys)); - var key = create_key(user_id); - redis_cache.del(key, function(err) { + var key = createKey(userId); + redisCache.del(key, function(err) { if (err) { throw err; } process.exit(); }); diff --git a/examples/redis_example/redis_store.js b/examples/redis_example/redis_store.js index e4b0bd6..12d16f8 100644 --- a/examples/redis_example/redis_store.js +++ b/examples/redis_example/redis_store.js @@ -5,19 +5,21 @@ var RedisPool = require('sol-redis-pool'); -function redis_store(args) { +function redisStore(args) { args = args || {}; var self = {}; var ttlDefault = args.ttl; self.name = 'redis'; self.client = require('redis').createClient(args.port, args.host, args); - var redis_options = { + //jscs:disable requireCamelCaseOrUpperCaseIdentifiers + var redisOptions = { redis_host: args.host || '127.0.0.1', redis_port: args.port || 6379 }; + //jscs:enable requireCamelCaseOrUpperCaseIdentifiers - var pool = new RedisPool(redis_options); + var pool = new RedisPool(redisOptions); function connect(cb) { pool.acquire(function(err, conn) { @@ -108,6 +110,6 @@ function redis_store(args) { module.exports = { create: function(args) { - return redis_store(args); + return redisStore(args); } }; diff --git a/index.js b/index.js index 0236e06..7f57f84 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ var cache = { caching: require('./lib/caching'), - multi_caching: require('./lib/multi_caching') + multi_caching: require('./lib/multi_caching'), //backward compat + multiCaching: require('./lib/multi_caching') }; module.exports = cache; diff --git a/lib/caching.js b/lib/caching.js index 7c35836..9e2f70b 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -15,8 +15,8 @@ var caching = function(args) { } else if (typeof args.store === 'string' && args.store.match(/\//)) { self.store = require(args.store).create(args); } else { - var store_name = args.store || 'memory'; - self.store = require('./stores/' + store_name).create(args); + var storeName = args.store || 'memory'; + self.store = require('./stores/' + storeName).create(args); } // do we handle a cache error the same as a cache miss? diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 6fbf3b2..22a61a4 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -5,22 +5,22 @@ var CallbackFiller = require('./callback_filler'); /** * Module that lets you specify a hierarchy of caches. */ -var multi_caching = function(caches) { +var multiCaching = function(caches) { var self = {}; if (!Array.isArray(caches)) { - throw new Error('multi_caching requires an array of caches'); + throw new Error('multiCaching requires an array of caches'); } var callbackFiller = new CallbackFiller(); - function get_from_highest_priority_cache(key, options, cb) { + function getFromHighestPriorityCache(key, options, cb) { if (typeof options === 'function') { cb = options; options = undefined; } var i = 0; - async.eachSeries(caches, function(cache, async_cb) { + async.eachSeries(caches, function(cache, next) { var callback = function(err, result) { if (err) { return cb(err); @@ -31,7 +31,7 @@ var multi_caching = function(caches) { } i += 1; - async_cb(err); + next(err); }; if (typeof options === 'object') { cache.store.get(key, options, callback); @@ -41,12 +41,12 @@ var multi_caching = function(caches) { }, cb); } - function set_in_multiple_caches(caches, opts, cb) { - async.each(caches, function(cache, async_cb) { + function setInMultipleCaches(caches, opts, cb) { + async.each(caches, function(cache, next) { if (typeof opts.options !== 'object') { - cache.store.set(opts.key, opts.value, opts.ttl, async_cb); + cache.store.set(opts.key, opts.value, opts.ttl, next); } else { - cache.store.set(opts.key, opts.value, opts.options, async_cb); + cache.store.set(opts.key, opts.value, opts.options, next); } }, cb); } @@ -56,8 +56,8 @@ var multi_caching = function(caches) { * * When a key is found in a lower cache, all higher levels are updated */ - self.get_and_pass_up = function(key, cb) { - get_from_highest_priority_cache(key, function(err, result, index) { + self.getAndPassUp = function(key, cb) { + getFromHighestPriorityCache(key, function(err, result, index) { if (err) { return cb(err); } @@ -66,13 +66,20 @@ var multi_caching = function(caches) { if (result !== undefined && index) { var cachesToUpdate = caches.slice(0, index); - async.each(cachesToUpdate, function(cache, async_cb) { - cache.set(key, result, result.ttl, async_cb); + async.each(cachesToUpdate, function(cache, next) { + cache.set(key, result, result.ttl, next); }); } }); }; + /** + * This is for backward-compatibility + */ + //jscs:disable requireCamelCaseOrUpperCaseIdentifiers + self.get_and_pass_up = self.getAndPassUp; + //jscs:enable requireCamelCaseOrUpperCaseIdentifiers + /** * Wraps a function in one or more caches. * Has same API as regular caching module. @@ -96,11 +103,11 @@ var multi_caching = function(caches) { callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; - get_from_highest_priority_cache(key, function(err, result, index) { + getFromHighestPriorityCache(key, function(err, result, index) { if (err) { return callbackFiller.fill(key, err); } else if (result) { - var caches_to_update = caches.slice(0, index); + var cachesToUpdate = caches.slice(0, index); var opts = { key: key, value: result, @@ -111,7 +118,7 @@ var multi_caching = function(caches) { opts.ttl = options; } - set_in_multiple_caches(caches_to_update, opts, function(err) { + setInMultipleCaches(cachesToUpdate, opts, function(err) { callbackFiller.fill(key, err, result); }); } else { @@ -134,7 +141,7 @@ var multi_caching = function(caches) { if (typeof options !== 'object') { opts.ttl = options; } - set_in_multiple_caches(caches, opts, function(err) { + setInMultipleCaches(caches, opts, function(err) { if (err) { callbackFiller.fill(key, err); } else { @@ -155,7 +162,7 @@ var multi_caching = function(caches) { if (typeof options !== 'object') { opts.ttl = options; } - set_in_multiple_caches(caches, opts, cb); + setInMultipleCaches(caches, opts, cb); }; self.get = function(key, options, cb) { @@ -163,7 +170,7 @@ var multi_caching = function(caches) { cb = options; options = false; } - get_from_highest_priority_cache(key, options, cb); + getFromHighestPriorityCache(key, options, cb); }; self.del = function(key, options, cb) { @@ -171,11 +178,11 @@ var multi_caching = function(caches) { cb = options; options = false; } - async.each(caches, function(cache, async_cb) { + async.each(caches, function(cache, next) { if (typeof options === 'object') { - cache.store.del(key, options, async_cb); + cache.store.del(key, options, next); } else { - cache.store.del(key, async_cb); + cache.store.del(key, next); } }, cb); }; @@ -183,4 +190,4 @@ var multi_caching = function(caches) { return self; }; -module.exports = multi_caching; +module.exports = multiCaching; diff --git a/lib/stores/memory.js b/lib/stores/memory.js index 3c3dc24..bcd9730 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -1,19 +1,19 @@ var Lru = require("lru-cache"); -var memory_store = function(args) { +var memoryStore = function(args) { args = args || {}; var self = {}; self.name = 'memory'; var ttl = args.ttl; - var lru_opts = { + var lruOpts = { max: args.max || 500, maxAge: ttl ? ttl * 1000 : null }; - var lru_cache = new Lru(lru_opts); + var lruCache = new Lru(lruOpts); self.set = function(key, value, options, cb) { - lru_cache.set(key, value); + lruCache.set(key, value); if (cb) { process.nextTick(cb); } @@ -23,7 +23,7 @@ var memory_store = function(args) { if (typeof options === 'function') { cb = options; } - var value = lru_cache.get(key); + var value = lruCache.get(key); if (cb) { process.nextTick(function() { cb(null, value); @@ -37,21 +37,21 @@ var memory_store = function(args) { if (typeof options === 'function') { cb = options; } - lru_cache.del(key); + lruCache.del(key); if (cb) { process.nextTick(cb); } }; self.reset = function(cb) { - lru_cache.reset(); + lruCache.reset(); if (cb) { process.nextTick(cb); } }; self.keys = function(cb) { - var keys = lru_cache.keys(); + var keys = lruCache.keys(); if (cb) { process.nextTick(function() { cb(null, keys); @@ -66,7 +66,7 @@ var memory_store = function(args) { var methods = { create: function(args) { - return memory_store(args); + return memoryStore(args); } }; diff --git a/test/caching.unit.js b/test/caching.unit.js index dc2d167..deead08 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -4,12 +4,12 @@ var assert = require('assert'); var async = require('async'); var sinon = require('sinon'); var support = require('./support'); -var check_err = support.check_err; +var checkErr = support.checkErr; var caching = require('../index').caching; -var memory_store = require('../lib/stores/memory'); +var memoryStore = require('../lib/stores/memory'); var methods = { - get_widget: function(name, cb) { + getWidget: function(name, cb) { cb(null, {name: name}); } }; @@ -32,7 +32,7 @@ describe("caching", function() { it("lets us set and get data in cache", function(done) { cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.equal(result, value); done(); @@ -69,7 +69,7 @@ describe("caching", function() { key = support.random.string(20); value = support.random.string(); cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); done(); }); }); @@ -79,7 +79,7 @@ describe("caching", function() { assert.equal(result, value); cache.del(key, function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.ok(!result); @@ -116,7 +116,7 @@ describe("caching", function() { key = support.random.string(20); value = support.random.string(); cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); key2 = support.random.string(20); value2 = support.random.string(); @@ -127,7 +127,7 @@ describe("caching", function() { it("clears the cache", function(done) { cache.reset(function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.ok(!result); @@ -155,10 +155,10 @@ describe("caching", function() { }); context("when store has no del() method", function() { - var fake_store; + var fakeStore; beforeEach(function() { - fake_store = { + fakeStore = { get: function() {}, set: function() {}, }; @@ -166,52 +166,52 @@ describe("caching", function() { it("it doesn't throw an error", function() { assert.doesNotThrow(function() { - caching({store: fake_store}); + caching({store: fakeStore}); }); }); }); }); describe("setex()", function() { - var fake_store; + var fakeStore; beforeEach(function() { - fake_store = { + fakeStore = { get: function() {}, set: function() {}, del: function() {}, setex: function() {} }; - sinon.stub(fake_store, 'setex'); + sinon.stub(fakeStore, 'setex'); - cache = caching({store: fake_store}); + cache = caching({store: fakeStore}); }); it("passes the params to the underlying store's setex() method", function() { cache.setex('foo', 'bar', 'blah'); - assert.ok(fake_store.setex.calledWith('foo', 'bar', 'blah')); + assert.ok(fakeStore.setex.calledWith('foo', 'bar', 'blah')); }); }); describe("keys()", function() { - var key_count; - var saved_keys = []; + var keyCount; + var savedKeys = []; beforeEach(function(done) { - key_count = 10; + keyCount = 10; var processed = 0; cache = caching({store: 'memory'}); - function is_done() { - return processed === key_count; + function isDone() { + return processed === keyCount; } - async.until(is_done, function(cb) { + async.until(isDone, function(cb) { processed += 1; key = support.random.string(20); - saved_keys.push(key); + savedKeys.push(key); value = support.random.string(); cache.set(key, value, ttl, cb); }, done); @@ -219,27 +219,27 @@ describe("caching", function() { it("calls back with all keys in cache", function(done) { cache.keys(function(err, keys) { - check_err(err); - assert.deepEqual(keys.sort, saved_keys.sort); + checkErr(err); + assert.deepEqual(keys.sort, savedKeys.sort); done(); }); }); it("lets us get the keys without a callback (memory store only)", function() { var keys = cache.keys(); - assert.deepEqual(keys.sort, saved_keys.sort); + assert.deepEqual(keys.sort, savedKeys.sort); }); }); describe("wrap()", function() { describe("using memory (lru-cache) store", function() { - var memory_store_stub; + var memoryStoreStub; beforeEach(function() { ttl = 0.1; - memory_store_stub = memory_store.create({ttl: ttl}); + memoryStoreStub = memoryStore.create({ttl: ttl}); - sinon.stub(memory_store, 'create').returns(memory_store_stub); + sinon.stub(memoryStore, 'create').returns(memoryStoreStub); cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: false}); key = support.random.string(20); @@ -247,58 +247,58 @@ describe("caching", function() { }); afterEach(function() { - memory_store.create.restore(); + memoryStore.create.restore(); }); context("calls back with the result of the wrapped function", function() { beforeEach(function() { - sinon.spy(memory_store_stub, 'set'); + sinon.spy(memoryStoreStub, 'set'); }); afterEach(function() { - memory_store_stub.set.restore(); + memoryStoreStub.set.restore(); }); it("when a ttl is passed in", function(done) { cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - sinon.assert.calledWith(memory_store_stub.set, key, {name: name}, ttl); + sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, ttl); done(); }); }); it("when a ttl is not passed in", function(done) { cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - sinon.assert.calledWith(memory_store_stub.set, key, {name: name}, undefined); + sinon.assert.calledWith(memoryStoreStub.set, key, {name: name}, undefined); done(); }); }); }); context("when result is already cached", function() { - function get_cached_widget(name, cb) { - cache.wrap(key, function(cache_cb) { - methods.get_widget(name, cache_cb); + function getCachedWidget(name, cb) { + cache.wrap(key, function(cacheCb) { + methods.getWidget(name, cacheCb); }, ttl, cb); } beforeEach(function(done) { - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.ok(widget); - memory_store_stub.get(key, function(err, result) { - check_err(err); + memoryStoreStub.get(key, function(err, result) { + checkErr(err); assert.ok(result); - sinon.spy(memory_store_stub, 'get'); + sinon.spy(memoryStoreStub, 'get'); done(); }); @@ -306,44 +306,44 @@ describe("caching", function() { }); afterEach(function() { - memory_store_stub.get.restore(); + memoryStoreStub.get.restore(); }); it("retrieves data from cache", function(done) { - var func_called = false; + var funcCalled = false; cache.wrap(key, function(cb) { - methods.get_widget(name, function(err, result) { - func_called = true; + methods.getWidget(name, function(err, result) { + funcCalled = true; cb(err, result); }); }, ttl, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - assert.ok(memory_store_stub.get.calledWith(key)); - assert.ok(!func_called); + assert.ok(memoryStoreStub.get.calledWith(key)); + assert.ok(!funcCalled); done(); }); }); }); it("lets us make nested calls", function(done) { - function get_cached_widget(name, cb) { - cache.wrap(key, function(cache_cb) { - methods.get_widget(name, cache_cb); + function getCachedWidget(name, cb) { + cache.wrap(key, function(cacheCb) { + methods.getWidget(name, cacheCb); }, cb); } - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); done(); }); @@ -353,26 +353,26 @@ describe("caching", function() { it("expires cached result after ttl seconds", function(done) { cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_store_stub.get(key, function(err, result) { - check_err(err); + memoryStoreStub.get(key, function(err, result) { + checkErr(err); assert.ok(result); - var func_called = false; + var funcCalled = false; setTimeout(function() { cache.wrap(key, function(cb) { - methods.get_widget(name, function(err, result) { - func_called = true; + methods.getWidget(name, function(err, result) { + funcCalled = true; cb(err, result); }); }, function(err, widget) { - check_err(err); - assert.ok(func_called); + checkErr(err); + assert.ok(funcCalled); assert.deepEqual(widget, {name: name}); done(); }); @@ -382,17 +382,17 @@ describe("caching", function() { }); context("when an error is thrown in the work function", function() { - var fake_error; + var fakeError; beforeEach(function() { - fake_error = new Error(support.random.string()); + fakeError = new Error(support.random.string()); }); it("bubbles up that error", function(done) { cache.wrap(key, function() { - throw fake_error; + throw fakeError; }, ttl, function(err) { - assert.equal(err, fake_error); + assert.equal(err, fakeError); done(); }); }); @@ -401,17 +401,17 @@ describe("caching", function() { context("when store.get() calls back with an error", function() { context("and ignoreCacheErrors is not set (default is false)", function() { it("bubbles up that error", function(done) { - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'get', function(key, options, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'get', function(key, options, cb) { + cb(fakeError); }); cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err) { - assert.equal(err, fake_error); - memory_store_stub.get.restore(); + assert.equal(err, fakeError); + memoryStoreStub.get.restore(); done(); }); }); @@ -421,17 +421,17 @@ describe("caching", function() { it("does not bubble up that error", function(done) { cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true}); - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'get', function(key, options, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'get', function(key, options, cb) { + cb(fakeError); }); cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err) { assert.equal(err, null); - memory_store_stub.get.restore(); + memoryStoreStub.get.restore(); done(); }); }); @@ -441,17 +441,17 @@ describe("caching", function() { context("when store.set() calls back with an error", function() { context("and ignoreCacheErrors is not set", function() { it("bubbles up that error", function(done) { - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'set', function(key, val, ttl, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'set', function(key, val, ttl, cb) { + cb(fakeError); }); cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err) { - assert.equal(err, fake_error); - memory_store_stub.set.restore(); + assert.equal(err, fakeError); + memoryStoreStub.set.restore(); done(); }); }); @@ -460,17 +460,17 @@ describe("caching", function() { context("and ignoreCacheErrors is set to true", function() { it("does not bubbles up that error", function(done) { cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: true}); - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'set', function(key, val, ttl, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'set', function(key, val, ttl, cb) { + cb(fakeError); }); cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err) { assert.equal(err, null); - memory_store_stub.set.restore(); + memoryStoreStub.set.restore(); done(); }); }); @@ -479,16 +479,16 @@ describe("caching", function() { context("when wrapped function calls back with an error", function() { it("calls back with that error", function(done) { - var fake_error = new Error(support.random.string()); - sinon.stub(methods, 'get_widget', function(name, cb) { - cb(fake_error, {name: name}); + var fakeError = new Error(support.random.string()); + sinon.stub(methods, 'getWidget', function(name, cb) { + cb(fakeError, {name: name}); }); cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + methods.getWidget(name, cb); }, ttl, function(err, widget) { - methods.get_widget.restore(); - assert.equal(err, fake_error); + methods.getWidget.restore(); + assert.equal(err, fakeError); assert.ok(!widget); done(); }); @@ -520,15 +520,15 @@ describe("caching", function() { values.push(i); } - async.each(values, function(val, async_cb) { + async.each(values, function(val, next) { cache.wrap('key', function(cb) { construct(val, cb); }, ttl, function(err, result) { assert.equal(result, 'value'); - async_cb(err); + next(err); }); }, function(err) { - check_err(err); + checkErr(err); assert.equal(construct.callCount, 1); done(); }); @@ -545,10 +545,10 @@ describe("caching", function() { describe("instantiating with custom store", function() { it("allows us to pass in our own store object", function(done) { - var store = memory_store.create({ttl: ttl}); + var store = memoryStore.create({ttl: ttl}); cache = caching({store: store}); cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.equal(result, value); done(); @@ -557,10 +557,10 @@ describe("caching", function() { }); it("allows us to pass in a path to our own store", function(done) { - var store_path = '../lib/stores/memory'; - cache = caching({store: store_path}); + var storePath = '../lib/stores/memory'; + cache = caching({store: storePath}); cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.equal(result, value); done(); @@ -569,10 +569,10 @@ describe("caching", function() { }); it("allows us to pass in a module (uninstantiated)", function(done) { - var store = memory_store; + var store = memoryStore; cache = caching({store: store}); cache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); cache.get(key, function(err, result) { assert.equal(result, value); done(); diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 3387ee9..ce24e19 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -2,33 +2,33 @@ var assert = require('assert'); var async = require('async'); var sinon = require('sinon'); var support = require('./support'); -var check_err = support.check_err; +var checkErr = support.checkErr; var caching = require('../index').caching; -var multi_caching = require('../index').multi_caching; -var memory_store = require('../lib/stores/memory'); +var multiCaching = require('../index').multiCaching; +var memoryStore = require('../lib/stores/memory'); var methods = { - get_widget: function(name, cb) { + getWidget: function(name, cb) { cb(null, {name: name}); } }; -describe("multi_caching", function() { - var memory_cache; - var memory_cache2; - var memory_cache3; - var multi_cache; +describe("multiCaching", function() { + var memoryCache; + var memoryCache2; + var memoryCache3; + var multiCache; var key; - var memory_ttl; + var memoryTtl; var name; var ttl = 5; beforeEach(function() { - memory_ttl = 0.1; + memoryTtl = 0.1; - memory_cache = caching({store: 'memory', ttl: memory_ttl}); - memory_cache2 = caching({store: 'memory', ttl: memory_ttl}); - memory_cache3 = caching({store: 'memory', ttl: memory_ttl}); + memoryCache = caching({store: 'memory', ttl: memoryTtl}); + memoryCache2 = caching({store: 'memory', ttl: memoryTtl}); + memoryCache3 = caching({store: 'memory', ttl: memoryTtl}); key = support.random.string(20); name = support.random.string(); @@ -38,24 +38,24 @@ describe("multi_caching", function() { var value; beforeEach(function() { - multi_cache = multi_caching([memory_cache, memory_cache2, memory_cache3]); + multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]); key = support.random.string(20); value = support.random.string(); }); describe("set()", function() { it("lets us set data in all caches", function(done) { - multi_cache.set(key, value, ttl, function(err) { - check_err(err); - memory_cache.get(key, function(err, result) { + multiCache.set(key, value, ttl, function(err) { + checkErr(err); + memoryCache.get(key, function(err, result) { assert.equal(result, value); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); done(); }); @@ -65,19 +65,19 @@ describe("multi_caching", function() { }); it("lets us set data without a callback", function(done) { - multi_cache.set(key, value, ttl); + multiCache.set(key, value, ttl); setTimeout(function() { - multi_cache.get(key, function(err, result) { + multiCache.get(key, function(err, result) { assert.equal(result, value); - memory_cache.get(key, function(err, result) { + memoryCache.get(key, function(err, result) { assert.equal(result, value); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); done(); }); @@ -88,19 +88,19 @@ describe("multi_caching", function() { }); it("lets us set data without a ttl or callback", function(done) { - multi_cache.set(key, value); + multiCache.set(key, value); setTimeout(function() { - multi_cache.get(key, function(err, result) { + multiCache.get(key, function(err, result) { assert.equal(result, value); - memory_cache.get(key, function(err, result) { + memoryCache.get(key, function(err, result) { assert.equal(result, value); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); done(); }); @@ -113,11 +113,11 @@ describe("multi_caching", function() { describe("get()", function() { it("gets data from first cache that has it", function(done) { - memory_cache3.set(key, value, ttl, function(err) { - check_err(err); + memoryCache3.set(key, value, ttl, function(err) { + checkErr(err); - multi_cache.get(key, function(err, result) { - check_err(err); + multiCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); done(); }); @@ -127,21 +127,21 @@ describe("multi_caching", function() { describe("del()", function() { it("lets us delete data in all caches", function(done) { - multi_cache.set(key, value, ttl, function(err) { - check_err(err); + multiCache.set(key, value, ttl, function(err) { + checkErr(err); - multi_cache.del(key, function(err) { - check_err(err); + multiCache.del(key, function(err) { + checkErr(err); - memory_cache.get(key, function(err, result) { + memoryCache.get(key, function(err, result) { assert.ok(!result); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.ok(!result); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.ok(!result); done(); }); @@ -152,21 +152,21 @@ describe("multi_caching", function() { }); it("lets us delete data without a callback", function(done) { - multi_cache.set(key, value, ttl, function(err) { - check_err(err); + multiCache.set(key, value, ttl, function(err) { + checkErr(err); - multi_cache.del(key); + multiCache.del(key); setTimeout(function() { - memory_cache.get(key, function(err, result) { + memoryCache.get(key, function(err, result) { assert.ok(!result); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.ok(!result); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.ok(!result); done(); }); @@ -178,23 +178,23 @@ describe("multi_caching", function() { }); }); - describe("get_and_pass_up()", function() { + describe("getAndPassUp()", function() { var value; var key; describe("using a single cache store", function() { beforeEach(function() { - multi_cache = multi_caching([memory_cache3]); + multiCache = multiCaching([memoryCache3]); key = support.random.string(20); value = support.random.string(); }); it("gets data from first cache that has it", function(done) { - memory_cache3.set(key, value, ttl, function(err) { - check_err(err); + memoryCache3.set(key, value, ttl, function(err) { + checkErr(err); - multi_cache.get_and_pass_up(key, function(err, result) { - check_err(err); + multiCache.getAndPassUp(key, function(err, result) { + checkErr(err); assert.equal(result, value); done(); }); @@ -207,21 +207,21 @@ describe("multi_caching", function() { beforeEach(function(done) { key = support.random.string(10); - sinon.spy(memory_cache, 'set'); - sinon.spy(memory_cache2, 'set'); - sinon.spy(memory_cache3, 'set'); + sinon.spy(memoryCache, 'set'); + sinon.spy(memoryCache2, 'set'); + sinon.spy(memoryCache3, 'set'); - multi_cache.get_and_pass_up(key, function(err, result) { - check_err(err); + multiCache.getAndPassUp(key, function(err, result) { + checkErr(err); response = result; done(); }); }); afterEach(function() { - memory_cache.set.restore(); - memory_cache2.set.restore(); - memory_cache3.set.restore(); + memoryCache.set.restore(); + memoryCache2.set.restore(); + memoryCache3.set.restore(); }); it("calls back with undefined", function() { @@ -230,9 +230,9 @@ describe("multi_caching", function() { it("does not set anything in caches", function(done) { process.nextTick(function() { - assert.ok(memory_cache.set.notCalled); - assert.ok(memory_cache2.set.notCalled); - assert.ok(memory_cache3.set.notCalled); + assert.ok(memoryCache.set.notCalled); + assert.ok(memoryCache2.set.notCalled); + assert.ok(memoryCache3.set.notCalled); done(); }); }); @@ -240,23 +240,23 @@ describe("multi_caching", function() { describe("using multi cache store", function() { beforeEach(function() { - multi_cache = multi_caching([memory_cache, memory_cache2, memory_cache3]); + multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]); key = support.random.string(20); value = support.random.string(); }); it("checks to see if higher levels have item", function(done) { - memory_cache3.set(key, value, ttl, function(err) { - check_err(err); + memoryCache3.set(key, value, ttl, function(err) { + checkErr(err); - multi_cache.get_and_pass_up(key, function(err, result) { - check_err(err); + multiCache.getAndPassUp(key, function(err, result) { + checkErr(err); assert.equal(result, value); process.nextTick(function() { - memory_cache.get(key, function(err, result) { + memoryCache.get(key, function(err, result) { assert.equal(result, value); - check_err(err); + checkErr(err); done(); }); }); @@ -269,36 +269,36 @@ describe("multi_caching", function() { describe("wrap()", function() { describe("using a single cache store", function() { beforeEach(function() { - multi_cache = multi_caching([memory_cache3]); + multiCache = multiCaching([memoryCache3]); }); context("calls back with the result of a function", function() { beforeEach(function() { - sinon.spy(memory_cache3.store, 'set'); + sinon.spy(memoryCache3.store, 'set'); }); afterEach(function() { - memory_cache3.store.set.restore(); + memoryCache3.store.set.restore(); }); it('when a ttl is passed in', function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, ttl, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - sinon.assert.calledWith(memory_cache3.store.set, key, {name: name}, ttl); + sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, ttl); done(); }); }); it('when a ttl is not passed in', function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - sinon.assert.calledWith(memory_cache3.store.set, key, {name: name}); + sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}); done(); }); }); @@ -306,16 +306,16 @@ describe("multi_caching", function() { context("when wrapped function calls back with an error", function() { it("calls back with that error", function(done) { - var fake_error = new Error(support.random.string()); - sinon.stub(methods, 'get_widget', function(name, cb) { - cb(fake_error, {name: name}); + var fakeError = new Error(support.random.string()); + sinon.stub(methods, 'getWidget', function(name, cb) { + cb(fakeError, {name: name}); }); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - methods.get_widget.restore(); - assert.equal(err, fake_error); + methods.getWidget.restore(); + assert.equal(err, fakeError); assert.ok(!widget); done(); }); @@ -325,32 +325,32 @@ describe("multi_caching", function() { describe("using two cache stores", function() { beforeEach(function() { - multi_cache = multi_caching([memory_cache, memory_cache3]); + multiCache = multiCaching([memoryCache, memoryCache3]); }); it("calls back with the result of a function", function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); done(); }); }); it("sets value in all caches", function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache.get(key, function(err, result) { - check_err(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); done(); }); @@ -360,17 +360,17 @@ describe("multi_caching", function() { context("when value exists in first store but not second", function() { it("returns value from first store, does not set it in second", function(done) { - memory_cache.set(key, {name: name}, ttl, function(err) { - check_err(err); + memoryCache.set(key, {name: name}, ttl, function(err) { + checkErr(err); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.equal(result, null); done(); }); @@ -381,17 +381,17 @@ describe("multi_caching", function() { context("when value exists in second store but not first", function() { it("returns value from second store, sets it in first store", function(done) { - memory_cache3.set(key, {name: name}, ttl, function(err) { - check_err(err); + memoryCache3.set(key, {name: name}, ttl, function(err) { + checkErr(err); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache.get(key, function(err, result) { - check_err(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); done(); }); @@ -403,36 +403,36 @@ describe("multi_caching", function() { describe("using three cache stores", function() { beforeEach(function() { - multi_cache = multi_caching([memory_cache, memory_cache3, memory_cache2]); + multiCache = multiCaching([memoryCache, memoryCache3, memoryCache2]); }); it("calls back with the result of a function", function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); done(); }); }); it("sets value in all caches", function(done) { - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache.get(key, function(err, result) { - check_err(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); done(); }); @@ -443,21 +443,21 @@ describe("multi_caching", function() { context("when value exists in first store only", function() { it("returns value from first store, does not set it in second or third", function(done) { - memory_cache.set(key, {name: name}, ttl, function(err) { - check_err(err); + memoryCache.set(key, {name: name}, ttl, function(err) { + checkErr(err); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.equal(result, null); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.equal(result, null); done(); }); @@ -469,21 +469,21 @@ describe("multi_caching", function() { context("when value exists in second store only", function() { it("returns value from second store, sets it in first store, does not set third store", function(done) { - memory_cache3.set(key, {name: name}, ttl, function(err) { - check_err(err); + memoryCache3.set(key, {name: name}, ttl, function(err) { + checkErr(err); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache.get(key, function(err, result) { - check_err(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); - memory_cache2.get(key, function(err, result) { - check_err(err); + memoryCache2.get(key, function(err, result) { + checkErr(err); assert.equal(result, null); done(); }); @@ -495,21 +495,21 @@ describe("multi_caching", function() { context("when value exists in third store only", function() { it("returns value from third store, sets it in first and second stores", function(done) { - memory_cache2.set(key, {name: name}, ttl, function(err) { - check_err(err); + memoryCache2.set(key, {name: name}, ttl, function(err) { + checkErr(err); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err, widget) { - check_err(err); + checkErr(err); assert.deepEqual(widget, {name: name}); - memory_cache3.get(key, function(err, result) { - check_err(err); + memoryCache3.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); - memory_cache.get(key, function(err, result) { - check_err(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.deepEqual(result, {name: name}); done(); @@ -521,22 +521,22 @@ describe("multi_caching", function() { }); it("lets us make nested calls", function(done) { - function get_cached_widget(name, cb) { - multi_cache.wrap(key, function(cache_cb) { - methods.get_widget(name, cache_cb); + function getCachedWidget(name, cb) { + multiCache.wrap(key, function(cacheCb) { + methods.getWidget(name, cacheCb); }, cb); } - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); - get_cached_widget(name, function(err, widget) { - check_err(err); + getCachedWidget(name, function(err, widget) { + checkErr(err); assert.equal(widget.name, name); done(); }); @@ -546,33 +546,33 @@ describe("multi_caching", function() { }); context("error handling", function() { - var memory_store_stub; + var memoryStoreStub; var ttl; beforeEach(function() { ttl = 0.1; - memory_store_stub = memory_store.create({ttl: ttl}); - sinon.stub(memory_store, 'create').returns(memory_store_stub); - memory_cache = caching({store: 'memory', ttl: ttl}); - multi_cache = multi_caching([memory_cache]); + memoryStoreStub = memoryStore.create({ttl: ttl}); + sinon.stub(memoryStore, 'create').returns(memoryStoreStub); + memoryCache = caching({store: 'memory', ttl: ttl}); + multiCache = multiCaching([memoryCache]); }); afterEach(function() { - memory_store.create.restore(); + memoryStore.create.restore(); }); context("when an error is thrown in the work function", function() { - var fake_error; + var fakeError; beforeEach(function() { - fake_error = new Error(support.random.string()); + fakeError = new Error(support.random.string()); }); it("bubbles up that error", function(done) { - multi_cache.wrap(key, function() { - throw fake_error; + multiCache.wrap(key, function() { + throw fakeError; }, ttl, function(err) { - assert.equal(err, fake_error); + assert.equal(err, fakeError); done(); }); }); @@ -580,17 +580,17 @@ describe("multi_caching", function() { context("when store.get() calls back with an error", function() { it("bubbles up that error", function(done) { - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'get', function(key, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'get', function(key, cb) { + cb(fakeError); }); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err) { - assert.equal(err, fake_error); - memory_store_stub.get.restore(); + assert.equal(err, fakeError); + memoryStoreStub.get.restore(); done(); }); }); @@ -598,17 +598,17 @@ describe("multi_caching", function() { context("when store.set() calls back with an error", function() { it("bubbles up that error", function(done) { - var fake_error = new Error(support.random.string()); + var fakeError = new Error(support.random.string()); - sinon.stub(memory_store_stub, 'set', function(key, val, ttl, cb) { - cb(fake_error); + sinon.stub(memoryStoreStub, 'set', function(key, val, ttl, cb) { + cb(fakeError); }); - multi_cache.wrap(key, function(cb) { - methods.get_widget(name, cb); + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); }, function(err) { - assert.equal(err, fake_error); - memory_store_stub.set.restore(); + assert.equal(err, fakeError); + memoryStoreStub.set.restore(); done(); }); }); @@ -619,7 +619,7 @@ describe("multi_caching", function() { var construct; beforeEach(function() { - multi_cache = multi_caching([memory_cache, memory_cache3]); + multiCache = multiCaching([memoryCache, memoryCache3]); construct = sinon.spy(function(val, cb) { var timeout = support.random.number(100); @@ -635,15 +635,15 @@ describe("multi_caching", function() { values.push(i); } - async.each(values, function(val, async_cb) { - multi_cache.wrap('key', function(cb) { + async.each(values, function(val, next) { + multiCache.wrap('key', function(cb) { construct(val, cb); }, function(err, result) { assert.equal(result, 'value'); - async_cb(err); + next(err); }); }, function(err) { - check_err(err); + checkErr(err); assert.equal(construct.callCount, 1); done(); }); @@ -654,8 +654,8 @@ describe("multi_caching", function() { context("when instantiated with a non-Array 'caches' arg", function() { it("throws an error", function() { assert.throws(function() { - multi_caching({foo: 'bar'}); - }, /multi_caching requires an array/); + multiCaching({foo: 'bar'}); + }, /multiCaching requires an array/); }); }); }); diff --git a/test/run.js b/test/run.js index 6286058..2bfa5c0 100755 --- a/test/run.js +++ b/test/run.js @@ -4,7 +4,7 @@ require('../index'); var Mocha = require('mocha'); var optimist = require('optimist'); -var walk_dir = require('./support').walk_dir; +var walkDir = require('./support').walkDir; var argv = optimist .usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default']( @@ -21,29 +21,29 @@ var argv = optimist var mocha = new Mocha({timeout: argv.timeout, reporter: argv.reporter, ui: 'bdd'}); -var valid_test_types = ['unit', 'functional', 'acceptance', 'integration']; -var requested_types = argv.types.split(','); -var types_to_use = []; +var validTestTypes = ['unit', 'functional', 'acceptance', 'integration']; +var requestedTypes = argv.types.split(','); +var typesToUse = []; -valid_test_types.forEach(function(valid_test_type) { - if (requested_types.indexOf(valid_test_type) !== -1) { - types_to_use.push(valid_test_type); +validTestTypes.forEach(function(validTestType) { + if (requestedTypes.indexOf(validTestType) !== -1) { + typesToUse.push(validTestType); } }); -if (argv.help || types_to_use.length === 0) { +if (argv.help || typesToUse.length === 0) { console.log('\n' + optimist.help()); process.exit(); } -var is_valid_file = function(file) { +var isValidFile = function(file) { if (file.match(/buster/)) { return false; } - for (var i = 0; i < types_to_use.length; i++) { - var test_type = types_to_use[i]; - var ext = test_type + ".js"; + for (var i = 0; i < typesToUse.length; i++) { + var testType = typesToUse[i]; + var ext = testType + ".js"; if (file.indexOf(ext) !== -1) { return true; @@ -54,7 +54,7 @@ var is_valid_file = function(file) { }; function run(cb) { - walk_dir('test', is_valid_file, function(err, files) { + walkDir('test', isValidFile, function(err, files) { if (err) { return cb(err); } files.forEach(function(file) { diff --git a/test/stores/memory.unit.js b/test/stores/memory.unit.js index 88d9608..ab3565d 100644 --- a/test/stores/memory.unit.js +++ b/test/stores/memory.unit.js @@ -1,11 +1,11 @@ var support = require('../support'); -var memory_store = require('../../lib/stores/memory'); +var memoryStore = require('../../lib/stores/memory'); describe("memory store", function() { describe("instantiating", function() { it("lets us pass in no args", function(done) { - var memory_cache = memory_store.create(); - support.test_set_get_del(memory_cache, done); + var memoryCache = memoryStore.create(); + support.testSetGetDel(memoryCache, done); }); }); }); diff --git a/test/stores/options.unit.js b/test/stores/options.unit.js index a201af2..5d87c97 100644 --- a/test/stores/options.unit.js +++ b/test/stores/options.unit.js @@ -1,7 +1,7 @@ var caching = require("../../index"); var assert = require("assert"); var support = require("../support"); -var check_err = support.check_err; +var checkErr = support.checkErr; var memoryFlag = ""; var key; var value; @@ -91,7 +91,7 @@ describe("Methods with options", function() { before(function() { key = support.random.string(20); value = support.random.string(20); - testCache = caching.multi_caching([testInstance]); + testCache = caching.multiCaching([testInstance]); }); describe("get with options", function() { @@ -170,19 +170,19 @@ describe("Multiple stores with options", function() { before(function() { key = support.random.string(20); value = support.random.string(20); - testCache = caching.multi_caching([testInstance, memInstance]); + testCache = caching.multiCaching([testInstance, memInstance]); }); it("lets us pass options which only one store uses", function() { testCache.set(key, value, options, function(err) { - check_err(err); + checkErr(err); testCache.get(key, options, function(err, response) { - check_err(err); + checkErr(err); assert.equal(response, value); testCache.del(key, options, function(err) { - check_err(err); + checkErr(err); testCache.get(key, options, function(err, response) { - check_err(err); + checkErr(err); assert.equal(response, undefined); }); }); @@ -192,14 +192,14 @@ describe("Multiple stores with options", function() { it("lets us not pass options which only one store uses", function() { testCache.set(key, value, ttl, function(err) { - check_err(err); + checkErr(err); testCache.get(key, function(err, response) { - check_err(err); + checkErr(err); assert.equal(response, value); testCache.del(key, function(err) { - check_err(err); + checkErr(err); testCache.get(key, function(err, response) { - check_err(err); + checkErr(err); assert.equal(response, undefined); }); }); diff --git a/test/support.js b/test/support.js index cecb996..8b188f9 100644 --- a/test/support.js +++ b/test/support.js @@ -4,15 +4,15 @@ var assert = require('assert'); var support = { random: { - string: function(str_len) { - str_len = str_len || 8; + string: function(strLen) { + strLen = strLen || 8; var chars = "abcdefghiklmnopqrstuvwxyz"; - var random_str = ''; - for (var i = 0; i < str_len; i++) { + var randomStr = ''; + for (var i = 0; i < strLen; i++) { var rnum = Math.floor(Math.random() * chars.length); - random_str += chars.substring(rnum, rnum + 1); + randomStr += chars.substring(rnum, rnum + 1); } - return random_str; + return randomStr; }, number: function(max) { @@ -21,7 +21,7 @@ var support = { } }, - check_err: function(err) { + checkErr: function(err) { if (err) { var msg; @@ -38,21 +38,21 @@ var support = { } }, - assert_between: function(actual, lower, upper) { + assertBetween: function(actual, lower, upper) { assert.ok(actual >= lower, "Expected " + actual + " to be >= " + lower); assert.ok(actual <= upper, "Expected " + actual + " to be <= " + upper); }, - assert_within: function(actual, expected, delta) { + assertWithin: function(actual, expected, delta) { var lower = expected - delta; var upper = expected + delta; - this.assert_between(actual, lower, upper); + this.assertBetween(actual, lower, upper); }, - walk_dir: function(dir, validation_function, cb) { + walkDir: function(dir, validationFunction, cb) { if (arguments.length === 2) { - cb = validation_function; - validation_function = null; + cb = validationFunction; + validationFunction = null; } var results = []; @@ -67,13 +67,13 @@ var support = { file = dir + '/' + file; fs.stat(file, function(err, stat) { if (stat && stat.isDirectory()) { - support.walk_dir(file, validation_function, function(err, res) { + support.walkDir(file, validationFunction, function(err, res) { results = results.concat(res); if (!--pending) { cb(null, results); } }); } else { - if (typeof validation_function === 'function') { - if (validation_function(file)) { + if (typeof validationFunction === 'function') { + if (validationFunction(file)) { results.push(file); } } else { @@ -87,7 +87,7 @@ var support = { }); }, - test_set_get_del: function(cache, cb) { + testSetGetDel: function(cache, cb) { var key = 'TEST' + support.random.string(); var val = support.random.string(); var ttl; From 1e8f67821e3a8ba87a6533228e160df4d1d3dec1 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 13:19:40 -0800 Subject: [PATCH 05/12] hiding queues inside CallbackFiller --- lib/caching.js | 9 +++------ lib/callback_filler.js | 12 ++++++++++++ lib/multi_caching.js | 9 +++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/caching.js b/lib/caching.js index 9e2f70b..d4c7134 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -44,12 +44,9 @@ var caching = function(args) { options = undefined; } - if (callbackFiller.queues[key]) { - callbackFiller.queues[key].push({cb: cb, domain: process.domain}); - return; - } - - callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; + var hasKey = callbackFiller.has(key); + callbackFiller.add(key, {cb: cb, domain: process.domain}); + if (hasKey) { return; } self.store.get(key, options, function(err, result) { if (err && (!self.ignoreCacheErrors)) { diff --git a/lib/callback_filler.js b/lib/callback_filler.js index 163aa7e..4914bf1 100644 --- a/lib/callback_filler.js +++ b/lib/callback_filler.js @@ -16,4 +16,16 @@ CallbackFiller.prototype.fill = function(key, err, data) { }); }; +CallbackFiller.prototype.has = function(key) { + return this.queues[key]; +}; + +CallbackFiller.prototype.add = function(key, funcObj) { + if (this.queues[key]) { + this.queues[key].push(funcObj); + } else { + this.queues[key] = [funcObj]; + } +}; + module.exports = CallbackFiller; diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 22a61a4..b384a50 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -96,12 +96,9 @@ var multiCaching = function(caches) { options = undefined; } - if (callbackFiller.queues[key]) { - callbackFiller.queues[key].push({cb: cb, domain: process.domain}); - return; - } - - callbackFiller.queues[key] = [{cb: cb, domain: process.domain}]; + var hasKey = callbackFiller.has(key); + callbackFiller.add(key, {cb: cb, domain: process.domain}); + if (hasKey) { return; } getFromHighestPriorityCache(key, function(err, result, index) { if (err) { From 29c76151ca5af2d703d4a2ebd39b9681bb51f309 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 13:51:38 -0800 Subject: [PATCH 06/12] example updates --- examples/example.js | 4 ++-- examples/redis_example/example.js | 31 ++++++++++++++++++++++++++++++- lib/caching.js | 5 ++--- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/examples/example.js b/examples/example.js index 3f979da..afde009 100644 --- a/examples/example.js +++ b/examples/example.js @@ -103,8 +103,8 @@ multiCache.wrap(key2, function(cb) { console.log(user); // Second time fetches user from memoryCache, since it's highest priority. - // If the data expires in the memory cache, the next fetch would pull it from - // the Redis cache, and set the data in memory again. + // If the data expires in memoryCache, the next fetch would pull it from + // the memoryCache2, and set the data in memoryCache. multiCache.wrap(key2, function(cb) { getUser(userId2, cb); }, function(err, user) { diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js index e7810e7..96f9879 100644 --- a/examples/redis_example/example.js +++ b/examples/redis_example/example.js @@ -86,7 +86,6 @@ getUserFromCache(userId, function(err, user) { var key = createKey(userId); redisCache.del(key, function(err) { if (err) { throw err; } - process.exit(); }); }); }); @@ -97,3 +96,33 @@ getUserFromCache(userId, function(err, user) { // user from second cache request: // { id: 123, name: 'Bob' } // keys: [ 'user_123' ] + +var redisCache2 = cacheManager.caching({store: redisStore, db: 1, ttl: 100}); +var multiCache = cacheManager.multiCaching([redisCache, redisCache2]); +var userId2 = 456; +var key2 = 'user_' + userId; +var ttl2 = 50; + +multiCache.wrap(key2, function(cb) { + getUser(userId2, cb); +}, {ttl: ttl2}, function(err, user) { + console.log("user: ", user); + + // Second time fetches user from redisCache, since it's highest priority. + // If the data expires in the redisCache, the next fetch would pull it from + // redisCache2, and set the data in redisCache again. + multiCache.wrap(key2, function(cb) { + getUser(userId2, cb); + }, function(err, user) { + console.log("user, second fetch:", user); + }); + + multiCache.getAndPassUp(key2, function(err, result) { + console.log("\ngetAndPassUp result: ", result); + + multiCache.del(key2, function(err) { + if (err) { throw err; } + process.exit(); + }); + }); +}); diff --git a/lib/caching.js b/lib/caching.js index d4c7134..975533b 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -1,5 +1,4 @@ /*jshint maxcomplexity:15*/ -/*jshint -W072 */ var domain = require('domain'); var CallbackFiller = require('./callback_filler'); @@ -31,9 +30,9 @@ var caching = function(args) { * * @example * - * var key = 'user_' + user_id; + * var key = 'user_' + userId; * cache.wrap(key, function(cb) { - * User.get(user_id, cb); + * User.get(userId, cb); * }, function(err, user) { * console.log(user); * }); From 444d43d93486ef2792f21b237ec77b2e89bb6712 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 14:42:08 -0800 Subject: [PATCH 07/12] refactoring, cleanup, test for error handling in getAndPassUp() --- lib/multi_caching.js | 66 ++++++++++++++++++-------------------- test/multi_caching.unit.js | 48 ++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/lib/multi_caching.js b/lib/multi_caching.js index b384a50..d128bf3 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -23,7 +23,7 @@ var multiCaching = function(caches) { async.eachSeries(caches, function(cache, next) { var callback = function(err, result) { if (err) { - return cb(err); + return next(err); } if (result) { // break out of async loop. @@ -31,8 +31,9 @@ var multiCaching = function(caches) { } i += 1; - next(err); + next(); }; + if (typeof options === 'object') { cache.store.get(key, options, callback); } else { @@ -43,10 +44,10 @@ var multiCaching = function(caches) { function setInMultipleCaches(caches, opts, cb) { async.each(caches, function(cache, next) { - if (typeof opts.options !== 'object') { - cache.store.set(opts.key, opts.value, opts.ttl, next); - } else { + if (typeof opts.options === 'object') { cache.store.set(opts.key, opts.value, opts.options, next); + } else { + cache.store.set(opts.key, opts.value, opts.ttl, next); } }, cb); } @@ -96,6 +97,20 @@ var multiCaching = function(caches) { options = undefined; } + function getOptsForSet(result) { + var opts = { + key: key, + value: result, + options: options + }; + + if (typeof options !== 'object') { + opts.ttl = options; + } + + return opts; + } + var hasKey = callbackFiller.has(key); callbackFiller.add(key, {cb: cb, domain: process.domain}); if (hasKey) { return; } @@ -105,45 +120,26 @@ var multiCaching = function(caches) { return callbackFiller.fill(key, err); } else if (result) { var cachesToUpdate = caches.slice(0, index); - var opts = { - key: key, - value: result, - options: options - }; - - if (typeof options !== 'object') { - opts.ttl = options; - } + var opts = getOptsForSet(result); setInMultipleCaches(cachesToUpdate, opts, function(err) { callbackFiller.fill(key, err, result); }); } else { domain - .create() - .on('error', function(err) { - callbackFiller.fill(key, err); - }) - .bind(work)(function(err, data) { + .create() + .on('error', function(err) { + callbackFiller.fill(key, err); + }) + .bind(work)(function(err, data) { if (err) { - callbackFiller.fill(key, err); - return; - } - var opts = { - key: key, - value: data, - options: options - }; - - if (typeof options !== 'object') { - opts.ttl = options; + return callbackFiller.fill(key, err); } + + var opts = getOptsForSet(data); + setInMultipleCaches(caches, opts, function(err) { - if (err) { - callbackFiller.fill(key, err); - } else { - callbackFiller.fill(key, null, data); - } + callbackFiller.fill(key, err, data); }); }); } diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index ce24e19..a664ef0 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -47,7 +47,9 @@ describe("multiCaching", function() { it("lets us set data in all caches", function(done) { multiCache.set(key, value, ttl, function(err) { checkErr(err); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); memoryCache2.get(key, function(err, result) { @@ -68,8 +70,11 @@ describe("multiCaching", function() { multiCache.set(key, value, ttl); setTimeout(function() { multiCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); memoryCache2.get(key, function(err, result) { @@ -91,8 +96,11 @@ describe("multiCaching", function() { multiCache.set(key, value); setTimeout(function() { multiCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); + memoryCache.get(key, function(err, result) { + checkErr(err); assert.equal(result, value); memoryCache2.get(key, function(err, result) { @@ -159,6 +167,7 @@ describe("multiCaching", function() { setTimeout(function() { memoryCache.get(key, function(err, result) { + checkErr(err); assert.ok(!result); memoryCache2.get(key, function(err, result) { @@ -263,6 +272,32 @@ describe("multiCaching", function() { }); }); }); + + context("when a cache store calls back with an error", function() { + var fakeError; + var memoryStoreStub; + + beforeEach(function() { + memoryStoreStub = memoryStore.create({ttl: ttl}); + sinon.stub(memoryStore, 'create').returns(memoryStoreStub); + memoryCache = caching({store: 'memory', ttl: ttl}); + multiCache = multiCaching([memoryCache]); + fakeError = new Error(support.random.string()); + sinon.stub(memoryStoreStub, 'get').yields(fakeError); + }); + + afterEach(function() { + memoryStore.create.restore(); + }); + + it("bubbles up errors from caches", function(done) { + multiCache.getAndPassUp(key, function(err) { + assert.ok(memoryStoreStub.get.called); + assert.equal(err, fakeError); + done(); + }); + }); + }); }); }); @@ -281,7 +316,7 @@ describe("multiCaching", function() { memoryCache3.store.set.restore(); }); - it('when a ttl is passed in', function(done) { + it('when a ttl number is passed in', function(done) { multiCache.wrap(key, function(cb) { methods.getWidget(name, cb); }, ttl, function(err, widget) { @@ -292,6 +327,17 @@ describe("multiCaching", function() { }); }); + it('when a ttl option is passed in', function(done) { + multiCache.wrap(key, function(cb) { + methods.getWidget(name, cb); + }, {ttl: ttl}, function(err, widget) { + checkErr(err); + assert.deepEqual(widget, {name: name}); + sinon.assert.calledWith(memoryCache3.store.set, key, {name: name}, {ttl: ttl}); + done(); + }); + }); + it('when a ttl is not passed in', function(done) { multiCache.wrap(key, function(cb) { methods.getWidget(name, cb); From 2e33acda47e581c35d3e350e9ad8f6cdfd2d6275 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 14:52:39 -0800 Subject: [PATCH 08/12] updating redis example to use latest redis npm --- examples/redis_example/example.js | 2 +- examples/redis_example/redis_store.js | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js index 96f9879..3e7366d 100644 --- a/examples/redis_example/example.js +++ b/examples/redis_example/example.js @@ -1,5 +1,5 @@ // Setup: -// npm install redis@0.6.7 sol-redis-pool@0.1.0 +// npm install redis@0.12.1 sol-redis-pool@0.2.0 // node examples/redis_example/example.js var util = require('util'); diff --git a/examples/redis_example/redis_store.js b/examples/redis_example/redis_store.js index 12d16f8..31bbfc0 100644 --- a/examples/redis_example/redis_store.js +++ b/examples/redis_example/redis_store.js @@ -10,16 +10,13 @@ function redisStore(args) { var self = {}; var ttlDefault = args.ttl; self.name = 'redis'; - self.client = require('redis').createClient(args.port, args.host, args); - //jscs:disable requireCamelCaseOrUpperCaseIdentifiers var redisOptions = { - redis_host: args.host || '127.0.0.1', - redis_port: args.port || 6379 + host: args.host || '127.0.0.1', + port: args.port || 6379 }; - //jscs:enable requireCamelCaseOrUpperCaseIdentifiers - var pool = new RedisPool(redisOptions); + var pool = new RedisPool(redisOptions, {}); function connect(cb) { pool.acquire(function(err, conn) { From c2eac33895c2a85ce29f5cf8ece6a529f08685e3 Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Sat, 7 Feb 2015 15:13:20 -0800 Subject: [PATCH 09/12] refactored redis example store, added ttl function to caching.js --- examples/redis_example/example.js | 25 +++++++++++-- examples/redis_example/redis_store.js | 51 +++++++++++++++------------ lib/caching.js | 4 +++ test/caching.unit.js | 22 ++++++++++++ 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/examples/redis_example/example.js b/examples/redis_example/example.js index 3e7366d..6321ce6 100644 --- a/examples/redis_example/example.js +++ b/examples/redis_example/example.js @@ -3,6 +3,7 @@ // node examples/redis_example/example.js var util = require('util'); +var assert = require('assert'); var cacheManager = require('../../'); var redisStore = require('./redis_store'); // Note: ttl is in seconds @@ -18,8 +19,14 @@ redisCache.set('foo', 'bar', {ttl: ttl}, function(err) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redisCache.del('foo', function(err) { + + redisCache.ttl('foo', function(err, result) { if (err) { throw err; } + assert.ok(result > 59 && result < 61); + + redisCache.del('foo', function(err) { + if (err) { throw err; } + }); }); }); }); @@ -32,8 +39,14 @@ redisCache.set('foo-no-ttl', 'bar-no-ttl', function(err) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redisCache.del('foo-no-ttl', function(err) { + + redisCache.ttl('foo-no-ttl', function(err, result) { if (err) { throw err; } + assert.ok(result > 99 && result < 101); + + redisCache.del('foo-no-ttl', function(err) { + if (err) { throw err; } + }); }); }); }); @@ -46,8 +59,14 @@ redisCache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) { if (err) { throw err; } console.log("result fetched from cache: " + result); // >> 'bar' - redisCache.del('foo-zero-ttl', function(err) { + + redisCache.ttl('foo-zero-ttl', function(err, result) { if (err) { throw err; } + assert.ok(result < 0); + + redisCache.del('foo-zero-ttl', function(err) { + if (err) { throw err; } + }); }); }); }); diff --git a/examples/redis_example/redis_store.js b/examples/redis_example/redis_store.js index 31bbfc0..93c922b 100644 --- a/examples/redis_example/redis_store.js +++ b/examples/redis_example/redis_store.js @@ -33,6 +33,22 @@ function redisStore(args) { }); } + function handleResponse(conn, cb, opts) { + opts = opts || {}; + + return function(err, result) { + pool.release(conn); + + if (err) { return cb(err); } + + if (opts.parse) { + result = JSON.parse(result); + } + + cb(null, result); + }; + } + self.get = function(key, options, cb) { if (typeof options === 'function') { cb = options; @@ -40,12 +56,7 @@ function redisStore(args) { connect(function(err, conn) { if (err) { return cb(err); } - - conn.get(key, function(err, result) { - pool.release(conn); - if (err) { return cb(err); } - cb(null, JSON.parse(result)); - }); + conn.get(key, handleResponse(conn, cb, {parse: true})); }); }; @@ -60,17 +71,12 @@ function redisStore(args) { connect(function(err, conn) { if (err) { return cb(err); } + var val = JSON.stringify(value); if (ttl) { - conn.setex(key, ttl, JSON.stringify(value), function(err, result) { - pool.release(conn); - cb(err, result); - }); + conn.setex(key, ttl, val, handleResponse(conn, cb)); } else { - conn.set(key, JSON.stringify(value), function(err, result) { - pool.release(conn); - cb(err, result); - }); + conn.set(key, val, handleResponse(conn, cb)); } }); }; @@ -78,11 +84,14 @@ function redisStore(args) { self.del = function(key, cb) { connect(function(err, conn) { if (err) { return cb(err); } + conn.del(key, handleResponse(conn, cb)); + }); + }; - conn.del(key, function(err, result) { - pool.release(conn); - cb(err, result); - }); + self.ttl = function(key, cb) { + connect(function(err, conn) { + if (err) { return cb(err); } + conn.ttl(key, handleResponse(conn, cb)); }); }; @@ -94,11 +103,7 @@ function redisStore(args) { connect(function(err, conn) { if (err) { return cb(err); } - - conn.keys(pattern, function(err, result) { - pool.release(conn); - cb(err, result); - }); + conn.keys(pattern, handleResponse(conn, cb)); }); }; diff --git a/lib/caching.js b/lib/caching.js index 975533b..0090110 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -95,6 +95,10 @@ var caching = function(args) { self.keys = self.store.keys.bind(self.store); } + if (typeof self.store.ttl === 'function') { + self.ttl = self.store.ttl.bind(self.store); + } + return self; }; diff --git a/test/caching.unit.js b/test/caching.unit.js index deead08..6adb2ee 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -194,6 +194,28 @@ describe("caching", function() { }); }); + describe("ttl()", function() { + var fakeStore; + + beforeEach(function() { + fakeStore = { + get: function() {}, + set: function() {}, + del: function() {}, + ttl: function() {} + }; + + sinon.stub(fakeStore, 'ttl'); + + cache = caching({store: fakeStore}); + }); + + it("passes the params to the underlying store's ttl() method", function() { + cache.ttl('foo'); + assert.ok(fakeStore.ttl.calledWith('foo')); + }); + }); + describe("keys()", function() { var keyCount; var savedKeys = []; From 13d1eb16d6202b67f3ff61a3103ea7526579facd Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 12 Feb 2015 10:23:19 -0800 Subject: [PATCH 10/12] not trying to pass ttl into cache.set() in getAndPassUp() --- lib/multi_caching.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/multi_caching.js b/lib/multi_caching.js index d128bf3..1c9a538 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -68,7 +68,8 @@ var multiCaching = function(caches) { if (result !== undefined && index) { var cachesToUpdate = caches.slice(0, index); async.each(cachesToUpdate, function(cache, next) { - cache.set(key, result, result.ttl, next); + // We rely on the cache module's default TTL + cache.set(key, result, next); }); } }); From 14f9c6f3c4a58e74e543f3a1672e084e400cbd2d Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 12 Feb 2015 10:24:59 -0800 Subject: [PATCH 11/12] whitespace removal --- History.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/History.md b/History.md index f6eb905..1e1dab4 100644 --- a/History.md +++ b/History.md @@ -85,7 +85,7 @@ - 0.0.2 2013-04-08 - Added ability to pass in a store module that isn't already instantiated. E.g., - + ```javascript var store = require('/path/to/my_memory_store'); cache = caching({store: store}); From e3c9e0d7c06bf713f51cd9edf20880ae1cf7262e Mon Sep 17 00:00:00 2001 From: Bryan Donovan Date: Thu, 12 Feb 2015 10:27:28 -0800 Subject: [PATCH 12/12] 0.18.0 --- History.md | 9 +++++++++ package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 1e1dab4..3099ad9 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,12 @@ +- 0.18.0 2015-02-12 + - Minor changes and refactorings including: + - converting to camelcase + - hiding queues inside CallbackFiller + - general example updates + - updated redis example to use latest redis npm + - not trying to pass ttl into cache.set() in getAndPassUp() (this didn't + work anyway) + - 0.17.0 2015-02-05 - Add Additional Options Parameter (#20) - @seanzx85 - Fixing bug with nested calls to wrap() (#21) diff --git a/package.json b/package.json index cc91086..9cdbf11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cache-manager", - "version": "0.17.0", + "version": "0.18.0", "description": "Cache module for Node.js", "main": "index.js", "scripts": {