Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved cache queue before of the store get function. (up to 2x performance boost). #18

Merged
merged 5 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 30 additions & 23 deletions lib/caching.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/*jshint maxcomplexity:15*/
var domain = require('domain');

var caching = function (args) {
args = args || {};
var self = {};
Expand Down Expand Up @@ -40,38 +42,43 @@ var caching = function (args) {
ttl = undefined;
}

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aletorrado - Do you know if there's a way to get test coverage for this domain error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just throw an exception in the work function and check that the
callback function is called only once and that it receives that error.
El dic 18, 2014 8:19 PM, "Bryan Donovan" notifications@github.com
escribió:

In lib/caching.js
#18 (diff)
:

         } 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) {
    

@aletorrado https://github.com/aletorrado - Do you know if there's a
way to get test coverage for this domain error?


Reply to this email directly or view it on GitHub
https://github.com/BryanDonovan/node-cache-manager/pull/18/files#r22079610
.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Can you please add tests for this? E.g.,


            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();
                    });
                });
            });

around line 360 in caching.unit.js and something similar in multi_caching.unit.js?

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];
});
});
}
Expand Down
52 changes: 30 additions & 22 deletions lib/multi_caching.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var async = require('async');
var domain = require('domain');

/**
* Module that lets you specify a hierarchy of caches.
Expand Down Expand Up @@ -47,9 +48,24 @@ var multi_caching = function (caches) {
ttl = undefined;
}

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) {
var caches_to_update = caches.slice(0, index);
var opts = {
Expand All @@ -58,38 +74,30 @@ var multi_caching = function (caches) {
ttl: ttl
};
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;
}
var opts = {
key: key,
value: work_args[1],
value: data,
ttl: ttl
};
set_in_multiple_caches(caches, opts, function (err) {
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];
});
});
}
Expand Down
17 changes: 17 additions & 0 deletions test/caching.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,23 @@ describe("caching", function () {
});
});

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 () {
context("and ignoreCacheErrors is not set (default is false)", function () {
it("bubbles up that error", function (done) {
Expand Down
17 changes: 17 additions & 0 deletions test/multi_caching.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,23 @@ describe("multi_caching", function () {
memory_store.create.restore();
});

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 () {
it("bubbles up that error", function (done) {
var fake_error = new Error(support.random.string());
Expand Down