cache-manager
Advanced tools
Comparing version 0.17.0 to 0.18.0
@@ -15,2 +15,3 @@ { | ||
"disallowSpaceBeforeBinaryOperators": [","], | ||
"requireCamelCaseOrUpperCaseIdentifiers": true, | ||
@@ -17,0 +18,0 @@ "disallowMultipleVarDecl": true, |
/*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 | ||
@@ -11,9 +11,9 @@ | ||
// | ||
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) { | ||
@@ -26,3 +26,3 @@ console.log(err); | ||
function get_user(id, cb) { | ||
function getUser(id, cb) { | ||
setTimeout(function() { | ||
@@ -34,4 +34,4 @@ console.log("Fetching user from slow database."); | ||
var user_id = 123; | ||
var key = 'user_' + user_id; | ||
var userId = 123; | ||
var key = 'user_' + userId; | ||
@@ -43,4 +43,4 @@ // | ||
// 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); } | ||
@@ -52,5 +52,5 @@ | ||
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); | ||
@@ -62,13 +62,13 @@ }); | ||
// ... 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. | ||
@@ -85,10 +85,10 @@ console.log(user); | ||
// 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) { | ||
@@ -102,17 +102,17 @@ console.log(user); | ||
// | ||
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. | ||
// 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); | ||
// Second time fetches user from memoryCache, since it's highest priority. | ||
// 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) { | ||
@@ -123,7 +123,7 @@ 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); | ||
@@ -133,3 +133,3 @@ // >> 'bar2' | ||
// Delete from all caches | ||
multi_cache.del('foo2', function(err) { | ||
multiCache.del('foo2', function(err) { | ||
if (err) { | ||
@@ -136,0 +136,0 @@ console.log(err); |
// 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'); | ||
var cache_manager = require('../../'); | ||
var redis_store = require('./redis_store'); | ||
var assert = require('assert'); | ||
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; | ||
@@ -14,11 +15,17 @@ | ||
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.ttl('foo', function(err, result) { | ||
if (err) { throw err; } | ||
assert.ok(result > 59 && result < 61); | ||
redisCache.del('foo', function(err) { | ||
if (err) { throw err; } | ||
}); | ||
}); | ||
@@ -29,11 +36,17 @@ }); | ||
// 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.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; } | ||
}); | ||
}); | ||
@@ -44,11 +57,17 @@ }); | ||
// 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.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; } | ||
}); | ||
}); | ||
@@ -58,9 +77,9 @@ }); | ||
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() { | ||
@@ -72,24 +91,23 @@ console.log("\n\nReturning user from slow database."); | ||
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(); | ||
}); | ||
@@ -105,1 +123,31 @@ }); | ||
// 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(); | ||
}); | ||
}); | ||
}); |
@@ -8,3 +8,3 @@ /** | ||
function redis_store(args) { | ||
function redisStore(args) { | ||
args = args || {}; | ||
@@ -14,10 +14,9 @@ var self = {}; | ||
self.name = 'redis'; | ||
self.client = require('redis').createClient(args.port, args.host, args); | ||
var redis_options = { | ||
redis_host: args.host || '127.0.0.1', | ||
redis_port: args.port || 6379 | ||
var redisOptions = { | ||
host: args.host || '127.0.0.1', | ||
port: args.port || 6379 | ||
}; | ||
var pool = new RedisPool(redis_options); | ||
var pool = new RedisPool(redisOptions, {}); | ||
@@ -39,2 +38,18 @@ function connect(cb) { | ||
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) { | ||
@@ -47,8 +62,3 @@ if (typeof options === 'function') { | ||
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})); | ||
}); | ||
@@ -68,13 +78,8 @@ }; | ||
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)); | ||
} | ||
@@ -87,7 +92,10 @@ }); | ||
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)); | ||
}); | ||
@@ -104,7 +112,3 @@ }; | ||
if (err) { return cb(err); } | ||
conn.keys(pattern, function(err, result) { | ||
pool.release(conn); | ||
cb(err, result); | ||
}); | ||
conn.keys(pattern, handleResponse(conn, cb)); | ||
}); | ||
@@ -118,4 +122,4 @@ }; | ||
create: function(args) { | ||
return redis_store(args); | ||
return redisStore(args); | ||
} | ||
}; |
@@ -0,89 +1,98 @@ | ||
- 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) | ||
- 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 | ||
@@ -94,2 +103,2 @@ var store = require('/path/to/my_memory_store'); | ||
- 0.0.1 2013-04-08 | ||
Initial release. | ||
- Initial release. |
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; |
/*jshint maxcomplexity:15*/ | ||
/*jshint -W072 */ | ||
var domain = require('domain'); | ||
var CallbackFiller = require('./callback_filler'); | ||
@@ -17,4 +17,4 @@ var caching = function(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); | ||
} | ||
@@ -25,3 +25,3 @@ | ||
self.queues = {}; | ||
var callbackFiller = new CallbackFiller(); | ||
@@ -35,5 +35,5 @@ /** | ||
* | ||
* 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) { | ||
@@ -49,24 +49,11 @@ * console.log(user); | ||
if (self.queues[key]) { | ||
self.queues[key].push({cb: cb, domain: process.domain}); | ||
return; | ||
} | ||
var hasKey = callbackFiller.has(key); | ||
callbackFiller.add(key, {cb: cb, domain: process.domain}); | ||
if (hasKey) { 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); | ||
}); | ||
} | ||
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 { | ||
@@ -76,7 +63,7 @@ domain | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
callbackFiller.fill(key, err); | ||
}) | ||
.bind(work)(function(err, data) { | ||
if (err) { | ||
fillCallbacks(err); | ||
callbackFiller.fill(key, err); | ||
return; | ||
@@ -86,5 +73,5 @@ } | ||
if (err && (!self.ignoreCacheErrors)) { | ||
fillCallbacks(err); | ||
callbackFiller.fill(key, err); | ||
} else { | ||
fillCallbacks(null, data); | ||
callbackFiller.fill(key, null, data); | ||
} | ||
@@ -117,2 +104,6 @@ }); | ||
if (typeof self.store.ttl === 'function') { | ||
self.ttl = self.store.ttl.bind(self.store); | ||
} | ||
return self; | ||
@@ -119,0 +110,0 @@ }; |
var async = require('async'); | ||
var domain = require('domain'); | ||
var CallbackFiller = require('./callback_filler'); | ||
@@ -7,11 +8,11 @@ /** | ||
*/ | ||
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'); | ||
} | ||
self.queues = {}; | ||
var callbackFiller = new CallbackFiller(); | ||
function get_from_highest_priority_cache(key, options, cb) { | ||
function getFromHighestPriorityCache(key, options, cb) { | ||
if (typeof options === 'function') { | ||
@@ -23,6 +24,6 @@ cb = options; | ||
var i = 0; | ||
async.forEachSeries(caches, function(cache, async_cb) { | ||
async.eachSeries(caches, function(cache, next) { | ||
var callback = function(err, result) { | ||
if (err) { | ||
return cb(err); | ||
return next(err); | ||
} | ||
@@ -35,4 +36,5 @@ if (result) { | ||
i += 1; | ||
async_cb(err); | ||
next(); | ||
}; | ||
if (typeof options === 'object') { | ||
@@ -46,8 +48,8 @@ cache.store.get(key, options, callback); | ||
function set_in_multiple_caches(caches, opts, cb) { | ||
async.forEach(caches, function(cache, async_cb) { | ||
if (typeof opts.options !== 'object') { | ||
cache.store.set(opts.key, opts.value, opts.ttl, 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.options, next); | ||
} else { | ||
cache.store.set(opts.key, opts.value, opts.options, async_cb); | ||
cache.store.set(opts.key, opts.value, opts.ttl, next); | ||
} | ||
@@ -62,4 +64,4 @@ }, cb); | ||
*/ | ||
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) { | ||
@@ -73,4 +75,5 @@ return cb(err); | ||
var cachesToUpdate = caches.slice(0, index); | ||
async.forEach(cachesToUpdate, function(cache, async_cb) { | ||
cache.set(key, result, result.ttl, async_cb); | ||
async.each(cachesToUpdate, function(cache, next) { | ||
// We rely on the cache module's default TTL | ||
cache.set(key, result, next); | ||
}); | ||
@@ -82,2 +85,9 @@ } | ||
/** | ||
* 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. | ||
@@ -98,63 +108,45 @@ * Has same API as regular caching module. | ||
if (self.queues[key]) { | ||
self.queues[key].push({cb: cb, domain: process.domain}); | ||
return; | ||
} | ||
function getOptsForSet(result) { | ||
var opts = { | ||
key: key, | ||
value: result, | ||
options: options | ||
}; | ||
self.queues[key] = [{cb: cb, domain: process.domain}]; | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
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); | ||
}); | ||
return opts; | ||
} | ||
get_from_highest_priority_cache(key, function(err, result, index) { | ||
var hasKey = callbackFiller.has(key); | ||
callbackFiller.add(key, {cb: cb, domain: process.domain}); | ||
if (hasKey) { return; } | ||
getFromHighestPriorityCache(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 = { | ||
key: key, | ||
value: result, | ||
options: options | ||
}; | ||
var cachesToUpdate = caches.slice(0, index); | ||
var opts = getOptsForSet(result); | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
set_in_multiple_caches(caches_to_update, opts, function(err) { | ||
fillCallbacks(err, result); | ||
setInMultipleCaches(cachesToUpdate, opts, function(err) { | ||
callbackFiller.fill(key, err, result); | ||
}); | ||
} else { | ||
domain | ||
.create() | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
}) | ||
.bind(work)(function(err, data) { | ||
.create() | ||
.on('error', function(err) { | ||
callbackFiller.fill(key, err); | ||
}) | ||
.bind(work)(function(err, data) { | ||
if (err) { | ||
fillCallbacks(err); | ||
return; | ||
return callbackFiller.fill(key, err); | ||
} | ||
var opts = { | ||
key: key, | ||
value: data, | ||
options: options | ||
}; | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
set_in_multiple_caches(caches, opts, function(err) { | ||
if (err) { | ||
fillCallbacks(err); | ||
} else { | ||
fillCallbacks(null, data); | ||
} | ||
var opts = getOptsForSet(data); | ||
setInMultipleCaches(caches, opts, function(err) { | ||
callbackFiller.fill(key, err, data); | ||
}); | ||
@@ -175,3 +167,3 @@ }); | ||
} | ||
set_in_multiple_caches(caches, opts, cb); | ||
setInMultipleCaches(caches, opts, cb); | ||
}; | ||
@@ -184,3 +176,3 @@ | ||
} | ||
get_from_highest_priority_cache(key, options, cb); | ||
getFromHighestPriorityCache(key, options, cb); | ||
}; | ||
@@ -193,7 +185,7 @@ | ||
} | ||
async.forEach(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); | ||
} | ||
@@ -206,2 +198,2 @@ }, cb); | ||
module.exports = multi_caching; | ||
module.exports = multiCaching; |
var Lru = require("lru-cache"); | ||
var memory_store = function(args) { | ||
var memoryStore = function(args) { | ||
args = args || {}; | ||
@@ -8,3 +8,3 @@ var self = {}; | ||
var ttl = args.ttl; | ||
var lru_opts = { | ||
var lruOpts = { | ||
max: args.max || 500, | ||
@@ -14,6 +14,6 @@ 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) { | ||
@@ -28,3 +28,3 @@ process.nextTick(cb); | ||
} | ||
var value = lru_cache.get(key); | ||
var value = lruCache.get(key); | ||
if (cb) { | ||
@@ -43,3 +43,3 @@ process.nextTick(function() { | ||
} | ||
lru_cache.del(key); | ||
lruCache.del(key); | ||
if (cb) { | ||
@@ -51,3 +51,3 @@ process.nextTick(cb); | ||
self.reset = function(cb) { | ||
lru_cache.reset(); | ||
lruCache.reset(); | ||
if (cb) { | ||
@@ -59,3 +59,3 @@ process.nextTick(cb); | ||
self.keys = function(cb) { | ||
var keys = lru_cache.keys(); | ||
var keys = lruCache.keys(); | ||
if (cb) { | ||
@@ -75,3 +75,3 @@ process.nextTick(function() { | ||
create: function(args) { | ||
return memory_store(args); | ||
return memoryStore(args); | ||
} | ||
@@ -78,0 +78,0 @@ }; |
{ | ||
"name": "cache-manager", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,4 +38,4 @@ [![build status](https://secure.travis-ci.org/BryanDonovan/node-cache-manager.png)](http://travis-ci.org/BryanDonovan/node-cache-manager) | ||
```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); } | ||
@@ -47,5 +47,5 @@ | ||
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); | ||
@@ -59,5 +59,5 @@ }); | ||
```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); | ||
@@ -92,18 +92,18 @@ } | ||
```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 () { | ||
@@ -115,14 +115,14 @@ console.log("Returning user from slow database."); | ||
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) { | ||
@@ -151,6 +151,6 @@ console.log(user); | ||
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) { | ||
@@ -171,6 +171,6 @@ respond(res, err, result); | ||
```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'}); | ||
``` | ||
@@ -181,13 +181,13 @@ | ||
```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); | ||
@@ -197,3 +197,3 @@ // >> 'bar2' | ||
// Delete from all caches | ||
multi_cache.del('foo2'); | ||
multiCache.del('foo2'); | ||
}); | ||
@@ -203,12 +203,12 @@ }); | ||
// 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) { | ||
@@ -215,0 +215,0 @@ console.log(user); |
@@ -7,8 +7,8 @@ // TODO: These are really a mix of unit and integration tests. | ||
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}); | ||
@@ -36,3 +36,3 @@ } | ||
cache.set(key, value, ttl, function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
cache.get(key, function(err, result) { | ||
@@ -74,3 +74,3 @@ assert.equal(result, value); | ||
cache.set(key, value, ttl, function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
done(); | ||
@@ -85,3 +85,3 @@ }); | ||
cache.del(key, function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
@@ -123,3 +123,3 @@ cache.get(key, function(err, result) { | ||
cache.set(key, value, ttl, function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
@@ -135,3 +135,3 @@ key2 = support.random.string(20); | ||
cache.reset(function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
@@ -164,6 +164,6 @@ cache.get(key, function(err, result) { | ||
context("when store has no del() method", function() { | ||
var fake_store; | ||
var fakeStore; | ||
beforeEach(function() { | ||
fake_store = { | ||
fakeStore = { | ||
get: function() {}, | ||
@@ -176,3 +176,3 @@ set: function() {}, | ||
assert.doesNotThrow(function() { | ||
caching({store: fake_store}); | ||
caching({store: fakeStore}); | ||
}); | ||
@@ -184,6 +184,6 @@ }); | ||
describe("setex()", function() { | ||
var fake_store; | ||
var fakeStore; | ||
beforeEach(function() { | ||
fake_store = { | ||
fakeStore = { | ||
get: function() {}, | ||
@@ -195,5 +195,5 @@ set: function() {}, | ||
sinon.stub(fake_store, 'setex'); | ||
sinon.stub(fakeStore, 'setex'); | ||
cache = caching({store: fake_store}); | ||
cache = caching({store: fakeStore}); | ||
}); | ||
@@ -203,12 +203,34 @@ | ||
cache.setex('foo', 'bar', 'blah'); | ||
assert.ok(fake_store.setex.calledWith('foo', 'bar', 'blah')); | ||
assert.ok(fakeStore.setex.calledWith('foo', 'bar', 'blah')); | ||
}); | ||
}); | ||
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 key_count; | ||
var saved_keys = []; | ||
var keyCount; | ||
var savedKeys = []; | ||
beforeEach(function(done) { | ||
key_count = 10; | ||
keyCount = 10; | ||
var processed = 0; | ||
@@ -218,10 +240,10 @@ | ||
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(); | ||
@@ -234,4 +256,4 @@ cache.set(key, value, ttl, cb); | ||
cache.keys(function(err, keys) { | ||
check_err(err); | ||
assert.deepEqual(keys.sort, saved_keys.sort); | ||
checkErr(err); | ||
assert.deepEqual(keys.sort, savedKeys.sort); | ||
done(); | ||
@@ -243,3 +265,3 @@ }); | ||
var keys = cache.keys(); | ||
assert.deepEqual(keys.sort, saved_keys.sort); | ||
assert.deepEqual(keys.sort, savedKeys.sort); | ||
}); | ||
@@ -250,9 +272,9 @@ }); | ||
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); | ||
@@ -265,3 +287,3 @@ cache = caching({store: 'memory', ttl: ttl, ignoreCacheErrors: false}); | ||
afterEach(function() { | ||
memory_store.create.restore(); | ||
memoryStore.create.restore(); | ||
}); | ||
@@ -271,7 +293,7 @@ | ||
beforeEach(function() { | ||
sinon.spy(memory_store_stub, 'set'); | ||
sinon.spy(memoryStoreStub, 'set'); | ||
}); | ||
afterEach(function() { | ||
memory_store_stub.set.restore(); | ||
memoryStoreStub.set.restore(); | ||
}); | ||
@@ -281,7 +303,7 @@ | ||
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(); | ||
@@ -293,7 +315,7 @@ }); | ||
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(); | ||
@@ -305,5 +327,5 @@ }); | ||
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); | ||
@@ -313,11 +335,11 @@ } | ||
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'); | ||
@@ -330,18 +352,18 @@ done(); | ||
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(); | ||
@@ -353,18 +375,18 @@ }); | ||
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); | ||
@@ -379,22 +401,22 @@ 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}); | ||
@@ -409,6 +431,6 @@ done(); | ||
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()); | ||
}); | ||
@@ -418,5 +440,5 @@ | ||
cache.wrap(key, function() { | ||
throw fake_error; | ||
throw fakeError; | ||
}, ttl, function(err) { | ||
assert.equal(err, fake_error); | ||
assert.equal(err, fakeError); | ||
done(); | ||
@@ -430,13 +452,13 @@ }); | ||
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(); | ||
@@ -451,13 +473,13 @@ }); | ||
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(); | ||
@@ -472,13 +494,13 @@ }); | ||
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(); | ||
@@ -492,13 +514,13 @@ }); | ||
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(); | ||
@@ -512,12 +534,12 @@ }); | ||
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); | ||
@@ -554,3 +576,3 @@ done(); | ||
async.each(values, function(val, async_cb) { | ||
async.each(values, function(val, next) { | ||
cache.wrap('key', function(cb) { | ||
@@ -560,6 +582,6 @@ construct(val, cb); | ||
assert.equal(result, 'value'); | ||
async_cb(err); | ||
next(err); | ||
}); | ||
}, function(err) { | ||
check_err(err); | ||
checkErr(err); | ||
assert.equal(construct.callCount, 1); | ||
@@ -581,6 +603,6 @@ done(); | ||
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) { | ||
@@ -594,6 +616,6 @@ assert.equal(result, value); | ||
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) { | ||
@@ -607,6 +629,6 @@ assert.equal(result, value); | ||
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) { | ||
@@ -613,0 +635,0 @@ assert.equal(result, value); |
@@ -5,9 +5,9 @@ var assert = require('assert'); | ||
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}); | ||
@@ -17,9 +17,9 @@ } | ||
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; | ||
@@ -29,7 +29,7 @@ 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}); | ||
@@ -44,3 +44,3 @@ key = support.random.string(20); | ||
beforeEach(function() { | ||
multi_cache = multi_caching([memory_cache, memory_cache2, memory_cache3]); | ||
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]); | ||
key = support.random.string(20); | ||
@@ -52,13 +52,15 @@ value = support.random.string(); | ||
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) { | ||
checkErr(err); | ||
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); | ||
@@ -73,15 +75,18 @@ done(); | ||
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) { | ||
checkErr(err); | ||
assert.equal(result, value); | ||
memory_cache.get(key, function(err, result) { | ||
memoryCache.get(key, function(err, result) { | ||
checkErr(err); | ||
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); | ||
@@ -97,15 +102,18 @@ done(); | ||
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) { | ||
checkErr(err); | ||
assert.equal(result, value); | ||
memory_cache.get(key, function(err, result) { | ||
memoryCache.get(key, function(err, result) { | ||
checkErr(err); | ||
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); | ||
@@ -123,7 +131,7 @@ done(); | ||
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); | ||
@@ -138,17 +146,17 @@ done(); | ||
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); | ||
@@ -164,17 +172,18 @@ done(); | ||
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) { | ||
checkErr(err); | ||
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); | ||
@@ -191,3 +200,3 @@ done(); | ||
describe("get_and_pass_up()", function() { | ||
describe("getAndPassUp()", function() { | ||
var value; | ||
@@ -198,3 +207,3 @@ var key; | ||
beforeEach(function() { | ||
multi_cache = multi_caching([memory_cache3]); | ||
multiCache = multiCaching([memoryCache3]); | ||
key = support.random.string(20); | ||
@@ -205,7 +214,7 @@ 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); | ||
@@ -223,8 +232,8 @@ 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; | ||
@@ -236,5 +245,5 @@ done(); | ||
afterEach(function() { | ||
memory_cache.set.restore(); | ||
memory_cache2.set.restore(); | ||
memory_cache3.set.restore(); | ||
memoryCache.set.restore(); | ||
memoryCache2.set.restore(); | ||
memoryCache3.set.restore(); | ||
}); | ||
@@ -248,5 +257,5 @@ | ||
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(); | ||
@@ -259,3 +268,3 @@ }); | ||
beforeEach(function() { | ||
multi_cache = multi_caching([memory_cache, memory_cache2, memory_cache3]); | ||
multiCache = multiCaching([memoryCache, memoryCache2, memoryCache3]); | ||
key = support.random.string(20); | ||
@@ -266,13 +275,13 @@ 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(); | ||
@@ -284,2 +293,28 @@ }); | ||
}); | ||
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -291,3 +326,3 @@ }); | ||
beforeEach(function() { | ||
multi_cache = multi_caching([memory_cache3]); | ||
multiCache = multiCaching([memoryCache3]); | ||
}); | ||
@@ -297,16 +332,16 @@ | ||
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); | ||
it('when a ttl number is passed in', function(done) { | ||
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(); | ||
@@ -316,9 +351,20 @@ }); | ||
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) { | ||
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(); | ||
@@ -331,12 +377,12 @@ }); | ||
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); | ||
@@ -351,10 +397,10 @@ done(); | ||
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}); | ||
@@ -366,14 +412,14 @@ 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}); | ||
@@ -388,13 +434,13 @@ done(); | ||
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); | ||
@@ -410,13 +456,13 @@ done(); | ||
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}); | ||
@@ -433,10 +479,10 @@ done(); | ||
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}); | ||
@@ -448,18 +494,18 @@ 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}); | ||
@@ -475,17 +521,17 @@ done(); | ||
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); | ||
@@ -502,17 +548,17 @@ done(); | ||
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); | ||
@@ -529,17 +575,17 @@ done(); | ||
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}); | ||
@@ -556,18 +602,18 @@ | ||
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); | ||
@@ -582,3 +628,3 @@ done(); | ||
context("error handling", function() { | ||
var memory_store_stub; | ||
var memoryStoreStub; | ||
var ttl; | ||
@@ -588,24 +634,24 @@ | ||
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(); | ||
@@ -618,13 +664,13 @@ }); | ||
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(); | ||
@@ -637,13 +683,13 @@ }); | ||
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(); | ||
@@ -659,3 +705,3 @@ }); | ||
beforeEach(function() { | ||
multi_cache = multi_caching([memory_cache, memory_cache3]); | ||
multiCache = multiCaching([memoryCache, memoryCache3]); | ||
@@ -676,11 +722,11 @@ construct = sinon.spy(function(val, cb) { | ||
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); | ||
@@ -696,6 +742,6 @@ done(); | ||
assert.throws(function() { | ||
multi_caching({foo: 'bar'}); | ||
}, /multi_caching requires an array/); | ||
multiCaching({foo: 'bar'}); | ||
}, /multiCaching requires an array/); | ||
}); | ||
}); | ||
}); |
@@ -7,3 +7,3 @@ #!/usr/bin/env node | ||
var optimist = require('optimist'); | ||
var walk_dir = require('./support').walk_dir; | ||
var walkDir = require('./support').walkDir; | ||
@@ -25,13 +25,13 @@ var argv = optimist | ||
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()); | ||
@@ -41,3 +41,3 @@ process.exit(); | ||
var is_valid_file = function(file) { | ||
var isValidFile = function(file) { | ||
if (file.match(/buster/)) { | ||
@@ -47,5 +47,5 @@ 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"; | ||
@@ -61,3 +61,3 @@ if (file.indexOf(ext) !== -1) { | ||
function run(cb) { | ||
walk_dir('test', is_valid_file, function(err, files) { | ||
walkDir('test', isValidFile, function(err, files) { | ||
if (err) { return cb(err); } | ||
@@ -64,0 +64,0 @@ |
var support = require('../support'); | ||
var memory_store = require('../../lib/stores/memory'); | ||
var memoryStore = require('../../lib/stores/memory'); | ||
@@ -7,6 +7,6 @@ describe("memory store", 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); | ||
}); | ||
}); | ||
}); |
var caching = require("../../index"); | ||
var assert = require("assert"); | ||
var support = require("../support"); | ||
var check_err = support.check_err; | ||
var checkErr = support.checkErr; | ||
var memoryFlag = ""; | ||
@@ -94,3 +94,3 @@ var key; | ||
value = support.random.string(20); | ||
testCache = caching.multi_caching([testInstance]); | ||
testCache = caching.multiCaching([testInstance]); | ||
}); | ||
@@ -174,3 +174,3 @@ | ||
value = support.random.string(20); | ||
testCache = caching.multi_caching([testInstance, memInstance]); | ||
testCache = caching.multiCaching([testInstance, memInstance]); | ||
}); | ||
@@ -180,10 +180,10 @@ | ||
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); | ||
@@ -198,10 +198,10 @@ }); | ||
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); | ||
@@ -208,0 +208,0 @@ }); |
@@ -7,11 +7,11 @@ var fs = require('fs'); | ||
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; | ||
}, | ||
@@ -25,3 +25,3 @@ | ||
check_err: function(err) { | ||
checkErr: function(err) { | ||
if (err) { | ||
@@ -43,3 +43,3 @@ var msg; | ||
assert_between: function(actual, lower, upper) { | ||
assertBetween: function(actual, lower, upper) { | ||
assert.ok(actual >= lower, "Expected " + actual + " to be >= " + lower); | ||
@@ -49,12 +49,12 @@ 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; | ||
} | ||
@@ -74,3 +74,3 @@ | ||
if (stat && stat.isDirectory()) { | ||
support.walk_dir(file, validation_function, function(err, res) { | ||
support.walkDir(file, validationFunction, function(err, res) { | ||
results = results.concat(res); | ||
@@ -80,4 +80,4 @@ 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); | ||
@@ -96,3 +96,3 @@ } | ||
test_set_get_del: function(cache, cb) { | ||
testSetGetDel: function(cache, cb) { | ||
var key = 'TEST' + support.random.string(); | ||
@@ -99,0 +99,0 @@ var val = support.random.string(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
101772
25
2223