Browse Source

changing "run()" to "wrap()", allowing us to pass in our own custom store.

hotfix/0.7.1
Bryan Donovan 12 years ago
parent
commit
2a51739456
  1. 16
      lib/caching.js
  2. 2
      lib/multi_caching.js
  3. 48
      test/caching.unit.js
  4. 22
      test/multi_caching.unit.js

16
lib/caching.js

@ -1,8 +1,14 @@
var caching = function(args) {
args = args || {};
var self = {};
self.store_name = args.store || 'redis';
self.store = require('./stores/' + self.store_name).create(args);
if (typeof args.store === 'object') {
self.store = args.store;
} else if (typeof args.store === 'string' && args.store.match(/\//)) {
self.store = require(args.store).create(args);
} else {
var store_name = args.store || 'redis';
self.store = require('./stores/' + store_name).create(args);
}
/**
* Wraps a function in cache. I.e., the first time the function is run,
@ -12,13 +18,13 @@ var caching = function(args) {
* @example
*
* var key = 'user_' + user_id;
* cache.run(key, function(cb) {
* user_adapter.get(user_id, cb);
* cache.wrap(key, function(cb) {
* User.get(user_id, cb);
* }, function(err, user) {
* console.log(user);
* });
*/
self.run = function(key, work, cb) {
self.wrap = function(key, work, cb) {
self.store.get(key, function(err, result) {
if (err) { return cb(err); }
if (result) {

2
lib/multi_caching.js

@ -39,7 +39,7 @@ var multi_caching = function(caches) {
* If a key doesn't exist in a higher-priority cache but exists in a lower-priority
* cache, it gets set in all higher-priority caches.
*/
self.run = function(key, work, cb) {
self.wrap = function(key, work, cb) {
get_from_highest_priority_cache(key, function(err, result, index) {
if (err) { return cb(err); }
if (result) {

48
test/caching.unit.js

@ -87,7 +87,7 @@ describe("caching", function() {
});
});
describe("run()", function() {
describe("wrap()", function() {
context("using redis store", function() {
var redis_client;
@ -107,7 +107,7 @@ describe("caching", function() {
});
it("calls back with the result of the wrapped function", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -117,7 +117,7 @@ describe("caching", function() {
});
it("caches the result of the function in redis", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -132,7 +132,7 @@ describe("caching", function() {
});
it("retrieves data from redis when available", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -142,7 +142,7 @@ describe("caching", function() {
sinon.spy(redis_client, 'get');
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -162,7 +162,7 @@ describe("caching", function() {
});
it("expires cached result after ttl seconds", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -197,7 +197,7 @@ describe("caching", function() {
});
it("calls back with the result of a function", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -207,7 +207,7 @@ describe("caching", function() {
});
it("retrieves data from memory when available", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -218,7 +218,7 @@ describe("caching", function() {
sinon.spy(memory_store_stub, 'get');
var func_called = false;
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, function(err, result) {
func_called = true;
cb(err, result);
@ -236,7 +236,7 @@ describe("caching", function() {
});
it("expires cached result after ttl seconds", function(done) {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -248,7 +248,7 @@ describe("caching", function() {
var func_called = false;
setTimeout(function () {
cache.run(key, function(cb) {
cache.wrap(key, function(cb) {
get_widget(name, function(err, result) {
func_called = true;
cb(err, result);
@ -265,4 +265,30 @@ describe("caching", function() {
});
});
});
describe("instantiating with custom store", function() {
it("allows us to pass in our own store object", function(done) {
var store = memory_store.create({ttl: ttl});
cache = caching({store: store});
cache.set(key, value, function(err, result) {
check_err(err);
cache.get(key, function(err, result) {
assert.equal(result, value);
done();
});
});
});
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});
cache.set(key, value, function(err, result) {
check_err(err);
cache.get(key, function(err, result) {
assert.equal(result, value);
done();
});
});
});
});
});

22
test/multi_caching.unit.js

@ -105,14 +105,14 @@ describe("multi_caching", function() {
});
});
describe("run()", function() {
describe("wrap()", function() {
describe("using a single cache store", function() {
beforeEach(function() {
multi_cache = multi_caching([redis_cache]);
});
it("calls back with the result of a function", function(done) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -128,7 +128,7 @@ describe("multi_caching", function() {
});
it("calls back with the result of a function", function(done) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -138,7 +138,7 @@ describe("multi_caching", function() {
});
it("sets value in all caches", function(done) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -160,7 +160,7 @@ describe("multi_caching", function() {
context("when value exists in first store but not second", function() {
it("returns value from first store, does not set it in second", function(done) {
memory_cache.set(key, {name: name}, function(err) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -179,7 +179,7 @@ describe("multi_caching", function() {
context("when value exists in second store but not first", function() {
it("returns value from second store, sets it in first store", function(done) {
redis_cache.set(key, {name: name}, function(err) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -202,7 +202,7 @@ describe("multi_caching", function() {
});
it("calls back with the result of a function", function(done) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -212,7 +212,7 @@ describe("multi_caching", function() {
});
it("sets value in all caches", function(done) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -239,7 +239,7 @@ describe("multi_caching", function() {
context("when value exists in first store only", function() {
it("returns value from first store, does not set it in second or third", function(done) {
memory_cache.set(key, {name: name}, function(err) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -263,7 +263,7 @@ describe("multi_caching", function() {
context("when value exists in second store only", function() {
it("returns value from second store, sets it in first store, does not set third store", function(done) {
redis_cache.set(key, {name: name}, function(err) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);
@ -287,7 +287,7 @@ describe("multi_caching", function() {
context("when value exists in third store only", function() {
it("returns value from third store, sets it in first and second stores", function(done) {
memory_cache2.set(key, {name: name}, function(err) {
multi_cache.run(key, function(cb) {
multi_cache.wrap(key, function(cb) {
get_widget(name, cb);
}, function(err, widget) {
check_err(err);

Loading…
Cancel
Save