cache-manager
Advanced tools
Comparing version 0.14.0 to 0.15.0
@@ -0,1 +1,5 @@ | ||
- 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 | ||
- 0.14.0 2014-10-15 | ||
@@ -2,0 +6,0 @@ Set ttl in wrap #14 - nguyenchr |
/*jshint maxcomplexity:15*/ | ||
var domain = require('domain'); | ||
var caching = function (args) { | ||
@@ -43,34 +45,39 @@ args = args || {}; | ||
if (self.queues[key]) { | ||
self.queues[key].push({cb: cb, domain: process.domain}); | ||
return; | ||
} | ||
self.queues[key] = [{cb: cb, domain: process.domain}]; | ||
function fillCallbacks(err, data) { | ||
self.queues[key].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) { | ||
if (err && (!self.ignoreCacheErrors)) { | ||
cb(err); | ||
fillCallbacks(err); | ||
} else if (result) { | ||
cb.call(cb, null, result); | ||
} else if (self.queues[key]) { | ||
self.queues[key].push(cb); | ||
fillCallbacks(null, result); | ||
} else { | ||
self.queues[key] = [cb]; | ||
work(function () { | ||
var work_args = Array.prototype.slice.call(arguments, 0); | ||
if (work_args[0]) { // assume first arg is an error | ||
self.queues[key].forEach(function (done) { | ||
done.call(null, work_args[0]); | ||
}); | ||
delete self.queues[key]; | ||
domain | ||
.create() | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
}) | ||
.bind(work)(function (err, data) { | ||
if (err) { | ||
fillCallbacks(err); | ||
return; | ||
} | ||
// Subsequently assume second arg is result. | ||
self.store.set(key, work_args[1], ttl, function (err) { | ||
self.store.set(key, data, ttl, function (err) { | ||
if (err && (!self.ignoreCacheErrors)) { | ||
self.queues[key].forEach(function (done) { | ||
done.call(null, err); | ||
}); | ||
fillCallbacks(err); | ||
} else { | ||
self.queues[key].forEach(function (done) { | ||
done.apply(null, work_args); | ||
}); | ||
fillCallbacks(null, data); | ||
} | ||
delete self.queues[key]; | ||
}); | ||
@@ -77,0 +84,0 @@ }); |
var async = require('async'); | ||
var domain = require('domain'); | ||
@@ -50,5 +51,20 @@ /** | ||
if (self.queues[key]) { | ||
self.queues[key].push({cb: cb, domain: process.domain}); | ||
return; | ||
} | ||
self.queues[key] = [{cb: cb, domain: process.domain}]; | ||
function fillCallbacks(err, data) { | ||
self.queues[key].forEach(function(task) { | ||
var taskDomain = task.domain || domain.create(); | ||
taskDomain.bind(task.cb)(err, data); | ||
}); | ||
delete self.queues[key]; | ||
} | ||
get_from_highest_priority_cache(key, function (err, result, index) { | ||
if (err) { | ||
return cb(err); | ||
return fillCallbacks(err); | ||
} else if (result) { | ||
@@ -62,15 +78,13 @@ var caches_to_update = caches.slice(0, index); | ||
set_in_multiple_caches(caches_to_update, opts, function (err) { | ||
cb(err, result); | ||
fillCallbacks(err, result); | ||
}); | ||
} else if (self.queues[key]) { | ||
self.queues[key].push(cb); | ||
} else { | ||
self.queues[key] = [cb]; | ||
work(function () { | ||
var work_args = Array.prototype.slice.call(arguments, 0); | ||
if (work_args[0]) { // assume first arg is an error | ||
self.queues[key].forEach(function (done) { | ||
done.call(null, work_args[0]); | ||
}); | ||
delete self.queues[key]; | ||
domain | ||
.create() | ||
.on('error', function(err) { | ||
fillCallbacks(err); | ||
}) | ||
.bind(work)(function (err, data) { | ||
if (err) { | ||
fillCallbacks(err); | ||
return; | ||
@@ -80,3 +94,3 @@ } | ||
key: key, | ||
value: work_args[1], | ||
value: data, | ||
ttl: ttl | ||
@@ -86,12 +100,6 @@ }; | ||
if (err) { | ||
self.queues[key].forEach(function (done) { | ||
done.call(null, err); | ||
}); | ||
delete self.queues[key]; | ||
return; | ||
fillCallbacks(err); | ||
} else { | ||
fillCallbacks(null, data); | ||
} | ||
self.queues[key].forEach(function (done) { | ||
done.apply(null, work_args); | ||
}); | ||
delete self.queues[key]; | ||
}); | ||
@@ -98,0 +106,0 @@ }); |
{ | ||
"name": "cache-manager", | ||
"version": "0.14.0", | ||
"version": "0.15.0", | ||
"description": "Cache module for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -360,2 +360,19 @@ // TODO: These are really a mix of unit and integration tests. | ||
context("when an error is thrown in the work function", function () { | ||
var fake_error; | ||
beforeEach(function() { | ||
fake_error = new Error(support.random.string()); | ||
}); | ||
it("bubbles up that error", function (done) { | ||
cache.wrap(key, function () { | ||
throw fake_error; | ||
}, ttl, function (err) { | ||
assert.equal(err, fake_error); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
context("when store.get() calls back with an error", function () { | ||
@@ -362,0 +379,0 @@ context("and ignoreCacheErrors is not set (default is false)", function () { |
@@ -452,2 +452,19 @@ var assert = require('assert'); | ||
context("when an error is thrown in the work function", function () { | ||
var fake_error; | ||
beforeEach(function() { | ||
fake_error = new Error(support.random.string()); | ||
}); | ||
it("bubbles up that error", function (done) { | ||
multi_cache.wrap(key, function () { | ||
throw fake_error; | ||
}, ttl, function (err) { | ||
assert.equal(err, fake_error); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
context("when store.get() calls back with an error", function () { | ||
@@ -454,0 +471,0 @@ it("bubbles up that error", function (done) { |
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
82323
1697