Browse Source

Added Options capability to Get, Set, and Delete. This can be used to pass additional parameters to the cache services.

feature/specify-what-to-cache
Sean Cady 10 years ago
parent
commit
0af1535383
  1. 7
      lib/caching.js
  2. 28
      lib/multi_caching.js
  3. 134
      test/stores/options.unit.js

7
lib/caching.js

@ -36,9 +36,10 @@ var caching = function(args) {
* console.log(user);
* });
*/
self.wrap = function(key, work, ttl, cb) {
self.wrap = function(key, work, ttl, cb, options) {
if (typeof ttl === 'function') {
cb = ttl;
options = cb;
ttl = undefined;
}
@ -79,10 +80,10 @@ var caching = function(args) {
} else {
fillCallbacks(null, data);
}
});
}, options);
});
}
});
}, options);
};
self.get = self.store.get.bind(self.store);

28
lib/multi_caching.js

@ -10,7 +10,7 @@ var multi_caching = function(caches) {
self.queues = {};
function get_from_highest_priority_cache(key, cb) {
function get_from_highest_priority_cache(key, cb, options) {
var i = 0;
async.forEachSeries(caches, function(cache, async_cb) {
cache.store.get(key, function(err, result) {
@ -22,13 +22,13 @@ var multi_caching = function(caches) {
i += 1;
async_cb(err);
});
}, options);
}, cb);
}
function set_in_multiple_caches(caches, opts, cb) {
async.forEach(caches, function(cache, async_cb) {
cache.store.set(opts.key, opts.value, opts.ttl, async_cb);
cache.store.set(opts.key, opts.value, opts.ttl, async_cb, opts.options);
}, cb);
}
@ -64,9 +64,10 @@ 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.wrap = function(key, work, ttl, cb) {
self.wrap = function(key, work, ttl, cb, options) {
if (typeof ttl === 'function') {
cb = ttl;
options = cb;
ttl = undefined;
}
@ -93,7 +94,8 @@ var multi_caching = function(caches) {
var opts = {
key: key,
value: result,
ttl: ttl
ttl: ttl,
options: options
};
set_in_multiple_caches(caches_to_update, opts, function(err) {
fillCallbacks(err, result);
@ -112,7 +114,8 @@ var multi_caching = function(caches) {
var opts = {
key: key,
value: data,
ttl: ttl
ttl: ttl,
options: options
};
set_in_multiple_caches(caches, opts, function(err) {
if (err) {
@ -126,22 +129,23 @@ var multi_caching = function(caches) {
});
};
self.set = function(key, value, ttl, cb) {
self.set = function(key, value, ttl, cb, options) {
var opts = {
key: key,
value: value,
ttl: ttl
ttl: ttl,
options: options
};
set_in_multiple_caches(caches, opts, cb);
};
self.get = function(key, cb) {
get_from_highest_priority_cache(key, cb);
self.get = function(key, cb, options) {
get_from_highest_priority_cache(key, cb, options);
};
self.del = function(key, cb) {
self.del = function(key, cb, options) {
async.forEach(caches, function(cache, async_cb) {
cache.store.del(key, async_cb);
cache.store.del(key, async_cb, options);
}, cb);
};

134
test/stores/options.unit.js

@ -0,0 +1,134 @@
var caching = require("../../index");
var assert = require("assert");
var support = require('../support');
var memoryFlag = "";
var key;
var value;
var testStore = function(args) {
args = args || {};
var self = {};
self.name = "options";
self.get = function(key, cb, options) {
if (options && options.value) {
return cb(null, options.value + "ValueOption");
} else if (options && options.fn) {
options.fn("GetFunctionOption");
return cb(null, "GetFunctionOption");
}
return cb("Error No Options");
};
self.set = function(key, value, ttl, cb, options) {
if (options && options.value) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("SetFunctionOption");
return cb();
}
return cb("Error No Options");
};
self.del = function(key, cb, options) {
if (options && options.value) {
memoryFlag = options.value + "ValueOption";
return cb();
} else if (options && options.fn) {
options.fn("DeleteFunctionOption");
return cb();
}
return cb("Error No Options");
};
return {
create: function() {
return self;
}
};
};
describe("Methods with options", function() {
before(function() {
key = support.random.string(20);
value = support.random.string(20);
});
describe("get with options", function() {
var testInstance = caching.caching({store: testStore()});
var testCache;
before(function() {
testCache = caching.multi_caching([testInstance]);
});
it("lets us pass options by value", function(done) {
var options = {value: value};
testCache.get(key, function(err, response) {
assert.equal(response, value + "ValueOption");
done();
}, options);
});
it("lets us pass options by function", function(done) {
var options = {
fn: function(response) {
assert.equal(response, "GetFunctionOption");
done();
}
};
testCache.get(key, function(err, response) {
assert.equal(response, "GetFunctionOption");
}, options);
});
});
describe("set with options", function() {
var testInstance = caching.caching({store: testStore()});
var testCache;
var ttl = 60;
before(function() {
testCache = caching.multi_caching([testInstance]);
});
it("lets us pass options by value", function(done) {
var options = {value: value};
testCache.set(key, value, ttl, function() {
assert.equal(memoryFlag,value + "ValueOption");
done();
}, options);
});
it("lets us pass options by function", function(done) {
var options = {
fn: function(response) {
assert.equal(response, "SetFunctionOption");
done();
}
};
testCache.set(key, value, ttl, function() {}, options);
});
});
describe("delete with options", function() {
var testInstance = caching.caching({store: testStore()});
var testCache;
before(function() {
testCache = caching.multi_caching([testInstance]);
});
it("lets us pass options by value", function(done) {
var options = {value: value};
testCache.del(key, function() {
assert.equal(memoryFlag,value + "ValueOption");
done();
}, options);
});
it("lets us pass options by function", function(done) {
var options = {
fn: function(response) {
assert.equal(response, "DeleteFunctionOption");
done();
}
};
testCache.del(key, function() {}, options);
});
});
});
Loading…
Cancel
Save