cache-manager
Advanced tools
Comparing version 0.16.0 to 0.17.0
@@ -5,2 +5,8 @@ { | ||
"requireSpaceBeforeKeywords": [ | ||
"else", | ||
"while", | ||
"catch" | ||
], | ||
"requireSpaceBeforeBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], | ||
@@ -7,0 +13,0 @@ "requireSpaceAfterBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], |
@@ -13,3 +13,4 @@ // Setup: | ||
console.log("set/get/del example:"); | ||
redis_cache.set('foo', 'bar', ttl, function(err) { | ||
redis_cache.set('foo', 'bar', {ttl: ttl}, function(err) { | ||
if (err) { throw err; } | ||
@@ -27,2 +28,30 @@ | ||
// TTL defaults to what we passed into the caching function (100) | ||
redis_cache.set('foo-no-ttl', 'bar-no-ttl', function(err) { | ||
if (err) { throw err; } | ||
redis_cache.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) { | ||
if (err) { throw err; } | ||
}); | ||
}); | ||
}); | ||
// Calls Redis 'set' instead of 'setex' | ||
redis_cache.set('foo-zero-ttl', 'bar-zero-ttl', {ttl: 0}, function(err) { | ||
if (err) { throw err; } | ||
redis_cache.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) { | ||
if (err) { throw err; } | ||
}); | ||
}); | ||
}); | ||
var user_id = 123; | ||
@@ -45,3 +74,3 @@ | ||
get_user(user_id, cache_cb); | ||
}, ttl, cb); | ||
}, {ttl: ttl}, cb); | ||
} | ||
@@ -48,0 +77,0 @@ |
@@ -37,3 +37,7 @@ /** | ||
self.get = function(key, cb) { | ||
self.get = function(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
} | ||
connect(function(err, conn) { | ||
@@ -50,9 +54,16 @@ if (err) { return cb(err); } | ||
self.set = function(key, value, ttl, cb) { | ||
var ttlToUse = ttl || ttlDefault; | ||
self.set = function(key, value, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
} | ||
options = options || {}; | ||
var ttl = (options.ttl || options.ttl === 0) ? options.ttl : ttlDefault; | ||
connect(function(err, conn) { | ||
if (err) { return cb(err); } | ||
if (ttlToUse) { | ||
conn.setex(key, ttlToUse, JSON.stringify(value), function(err, result) { | ||
if (ttl) { | ||
conn.setex(key, ttl, JSON.stringify(value), function(err, result) { | ||
pool.release(conn); | ||
@@ -59,0 +70,0 @@ cb(err, result); |
@@ -0,1 +1,5 @@ | ||
- 0.17.0 2015-02-05 | ||
Add Additional Options Parameter (#20) - @seanzx85 | ||
Fixing bug with nested calls to wrap() (#21) | ||
- 0.16.0 2015-01-07 | ||
@@ -2,0 +6,0 @@ Get and pass up feature to update higher caches. (#19) - raadad |
/*jshint maxcomplexity:15*/ | ||
/*jshint -W072 */ | ||
var domain = require('domain'); | ||
@@ -39,6 +40,6 @@ | ||
*/ | ||
self.wrap = function(key, work, ttl, cb) { | ||
if (typeof ttl === 'function') { | ||
cb = ttl; | ||
ttl = undefined; | ||
self.wrap = function(key, work, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
} | ||
@@ -54,10 +55,12 @@ | ||
function fillCallbacks(err, data) { | ||
self.queues[key].forEach(function(task) { | ||
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); | ||
}); | ||
delete self.queues[key]; | ||
} | ||
self.store.get(key, function(err, result) { | ||
self.store.get(key, options, function(err, result) { | ||
if (err && (!self.ignoreCacheErrors)) { | ||
@@ -78,3 +81,3 @@ fillCallbacks(err); | ||
} | ||
self.store.set(key, data, ttl, function(err) { | ||
self.store.set(key, data, options, function(err) { | ||
if (err && (!self.ignoreCacheErrors)) { | ||
@@ -81,0 +84,0 @@ fillCallbacks(err); |
@@ -9,11 +9,20 @@ var async = require('async'); | ||
var self = {}; | ||
if (!Array.isArray(caches)) { throw new Error('multi_caching requires an array of caches'); } | ||
if (!Array.isArray(caches)) { | ||
throw new Error('multi_caching requires an array of caches'); | ||
} | ||
self.queues = {}; | ||
function get_from_highest_priority_cache(key, cb) { | ||
function get_from_highest_priority_cache(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
} | ||
var i = 0; | ||
async.forEachSeries(caches, function(cache, async_cb) { | ||
cache.store.get(key, function(err, result) { | ||
if (err) { return cb(err); } | ||
var callback = function(err, result) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (result) { | ||
@@ -26,3 +35,8 @@ // break out of async loop. | ||
async_cb(err); | ||
}); | ||
}; | ||
if (typeof options === 'object') { | ||
cache.store.get(key, options, callback); | ||
} else { | ||
cache.store.get(key, callback); | ||
} | ||
}, cb); | ||
@@ -33,3 +47,7 @@ } | ||
async.forEach(caches, function(cache, async_cb) { | ||
cache.store.set(opts.key, opts.value, opts.ttl, async_cb); | ||
if (typeof opts.options !== 'object') { | ||
cache.store.set(opts.key, opts.value, opts.ttl, async_cb); | ||
} else { | ||
cache.store.set(opts.key, opts.value, opts.options, async_cb); | ||
} | ||
}, cb); | ||
@@ -70,6 +88,6 @@ } | ||
*/ | ||
self.wrap = function(key, work, ttl, cb) { | ||
if (typeof ttl === 'function') { | ||
cb = ttl; | ||
ttl = undefined; | ||
self.wrap = function(key, work, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = undefined; | ||
} | ||
@@ -85,7 +103,9 @@ | ||
function fillCallbacks(err, data) { | ||
self.queues[key].forEach(function(task) { | ||
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); | ||
}); | ||
delete self.queues[key]; | ||
} | ||
@@ -101,4 +121,9 @@ | ||
value: result, | ||
ttl: ttl | ||
options: options | ||
}; | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
set_in_multiple_caches(caches_to_update, opts, function(err) { | ||
@@ -109,7 +134,7 @@ fillCallbacks(err, result); | ||
domain | ||
.create() | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
}) | ||
.bind(work)(function(err, data) { | ||
.create() | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
}) | ||
.bind(work)(function(err, data) { | ||
if (err) { | ||
@@ -122,4 +147,8 @@ fillCallbacks(err); | ||
value: data, | ||
ttl: ttl | ||
options: options | ||
}; | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
set_in_multiple_caches(caches, opts, function(err) { | ||
@@ -137,18 +166,33 @@ if (err) { | ||
self.set = function(key, value, ttl, cb) { | ||
self.set = function(key, value, options, cb) { | ||
var opts = { | ||
key: key, | ||
value: value, | ||
ttl: ttl | ||
options: options | ||
}; | ||
if (typeof options !== 'object') { | ||
opts.ttl = options; | ||
} | ||
set_in_multiple_caches(caches, opts, cb); | ||
}; | ||
self.get = function(key, cb) { | ||
get_from_highest_priority_cache(key, cb); | ||
self.get = function(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = false; | ||
} | ||
get_from_highest_priority_cache(key, options, cb); | ||
}; | ||
self.del = function(key, cb) { | ||
self.del = function(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = false; | ||
} | ||
async.forEach(caches, function(cache, async_cb) { | ||
cache.store.del(key, async_cb); | ||
if (typeof options === 'object') { | ||
cache.store.del(key, options, async_cb); | ||
} else { | ||
cache.store.del(key, async_cb); | ||
} | ||
}, cb); | ||
@@ -155,0 +199,0 @@ }; |
@@ -15,3 +15,3 @@ var Lru = require("lru-cache"); | ||
self.set = function(key, value, ttl, cb) { | ||
self.set = function(key, value, options, cb) { | ||
lru_cache.set(key, value); | ||
@@ -23,3 +23,6 @@ if (cb) { | ||
self.get = function(key, cb) { | ||
self.get = function(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
} | ||
var value = lru_cache.get(key); | ||
@@ -35,3 +38,6 @@ if (cb) { | ||
self.del = function(key, cb) { | ||
self.del = function(key, options, cb) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
} | ||
lru_cache.del(key); | ||
@@ -38,0 +44,0 @@ if (cb) { |
{ | ||
"name": "cache-manager", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -330,2 +330,26 @@ // TODO: These are really a mix of unit and integration tests. | ||
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); | ||
}, cb); | ||
} | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("expires cached result after ttl seconds", function(done) { | ||
@@ -383,3 +407,3 @@ cache.wrap(key, function(cb) { | ||
sinon.stub(memory_store_stub, 'get', function(key, cb) { | ||
sinon.stub(memory_store_stub, 'get', function(key, options, cb) { | ||
cb(fake_error); | ||
@@ -404,3 +428,3 @@ }); | ||
sinon.stub(memory_store_stub, 'get', function(key, cb) { | ||
sinon.stub(memory_store_stub, 'get', function(key, options, cb) { | ||
cb(fake_error); | ||
@@ -407,0 +431,0 @@ }); |
@@ -175,4 +175,4 @@ var assert = require('assert'); | ||
}); | ||
}); | ||
}, 10); | ||
}, 10); | ||
}); | ||
}); | ||
@@ -523,2 +523,26 @@ }); | ||
}); | ||
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); | ||
}, cb); | ||
} | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
get_cached_widget(name, function(err, widget) { | ||
check_err(err); | ||
assert.equal(widget.name, name); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -525,0 +549,0 @@ |
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
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
97874
24
2121