Comparing version 4.0.3 to 4.1.0
@@ -19,4 +19,5 @@ // Load modules | ||
this.rule = internals.Policy.compile(options, !!cache); | ||
this._cache = cache; | ||
this._pendings = {}; // id -> [callbacks] | ||
this.rules(options); | ||
@@ -27,3 +28,2 @@ if (cache) { | ||
this._cache = cache; | ||
this._segment = segment; | ||
@@ -34,2 +34,8 @@ } | ||
internals.Policy.prototype.rules = function (options) { | ||
this.rule = internals.Policy.compile(options, !!this._cache); | ||
}; | ||
internals.Policy.prototype.get = function (key, callback) { // key: string or { id: 'id' } | ||
@@ -36,0 +42,0 @@ |
{ | ||
"name": "catbox", | ||
"description": "Multi-strategy object caching service", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"repository": "git://github.com/hapijs/catbox", | ||
@@ -19,3 +19,4 @@ "main": "index", | ||
"devDependencies": { | ||
"lab": "4.x.x" | ||
"code": "1.x.x", | ||
"lab": "5.x.x" | ||
}, | ||
@@ -22,0 +23,0 @@ "scripts": { |
@@ -55,5 +55,5 @@ ![catbox Logo](https://raw.github.com/hapijs/catbox/master/images/catbox.png) | ||
If found, the `cached` object contains the following: | ||
- `item` - the value stored in the cache using `set()`. | ||
- `stored` - the timestamp when the item was stored in the cache (in milliseconds). | ||
- `ttl` - the remaining time-to-live (not the original value used when storing the object). | ||
- `item` - the value stored in the cache using `set()`. | ||
- `stored` - the timestamp when the item was stored in the cache (in milliseconds). | ||
- `ttl` - the remaining time-to-live (not the original value used when storing the object). | ||
- `set(key, value, ttl, callback)` - store an item in the cache for a specified length of time, where: | ||
@@ -130,2 +130,3 @@ - `key` - a cache key object (see below). | ||
- `ttl(created)` - given a `created` timestamp in milliseconds, returns the time-to-live left based on the configured rules. | ||
- `rules(options) - changes the policy rules after construction (note that items already stored will not be affected) where: | ||
- `options` - the same `options` as the `Policy` constructor. |
// Load modules | ||
var Catbox = require('..'); | ||
var Code = require('code'); | ||
var Lab = require('lab'); | ||
var Catbox = require('..'); | ||
@@ -15,5 +16,5 @@ | ||
var lab = exports.lab = Lab.script(); | ||
var expect = Lab.expect; | ||
var describe = lab.experiment; | ||
var it = lab.test; | ||
var expect = Code.expect; | ||
@@ -29,3 +30,3 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
@@ -35,7 +36,7 @@ var key = { id: 'x', segment: 'test' }; | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
client.get(key, function (err, result) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(result.item).to.equal('123'); | ||
@@ -54,3 +55,3 @@ done(); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
@@ -60,7 +61,7 @@ var key = { id: 'x', segment: 'test' }; | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
client.get(key, function (err, result) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(result.item).to.equal('123'); | ||
@@ -89,3 +90,3 @@ done(); | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
expect(err.message).to.equal('fail'); | ||
@@ -110,3 +111,3 @@ done(); | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
done(); | ||
@@ -168,3 +169,3 @@ }); | ||
expect(cached.stored).to.equal('test2'); | ||
expect(cached.ttl).to.exist; | ||
expect(cached.ttl).to.exist(); | ||
done(); | ||
@@ -292,7 +293,7 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
client.get({}, function (err) { | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Invalid key'); | ||
@@ -299,0 +300,0 @@ done(); |
// Load modules | ||
var Catbox = require('..'); | ||
var Code = require('code'); | ||
var Lab = require('lab'); | ||
var Catbox = require('..'); | ||
var Import = require('./import'); | ||
@@ -16,5 +17,5 @@ | ||
var lab = exports.lab = Lab.script(); | ||
var expect = Lab.expect; | ||
var describe = lab.experiment; | ||
var it = lab.test; | ||
var expect = Code.expect; | ||
@@ -54,3 +55,3 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
client.get(key, function (err, result) { | ||
@@ -107,3 +108,3 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(client.isReady()).to.equal(true); | ||
@@ -113,3 +114,3 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(client.isReady()).to.equal(true); | ||
@@ -143,3 +144,3 @@ done(); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
setTimeout(function () { | ||
@@ -218,3 +219,3 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
done(); | ||
@@ -245,4 +246,4 @@ }); | ||
expect(err).to.exist; | ||
expect(result).to.not.exist; | ||
expect(err).to.exist(); | ||
expect(result).to.not.exist(); | ||
done(); | ||
@@ -259,3 +260,3 @@ }); | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
done(); | ||
@@ -272,3 +273,3 @@ }); | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
done(); | ||
@@ -312,3 +313,3 @@ }); | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
done(); | ||
@@ -315,0 +316,0 @@ }); |
// Load modules | ||
var Catbox = require('..'); | ||
var Code = require('code'); | ||
var Lab = require('lab'); | ||
var Catbox = require('..'); | ||
var Import = require('./import'); | ||
@@ -16,5 +17,5 @@ | ||
var lab = exports.lab = Lab.script(); | ||
var expect = Lab.expect; | ||
var describe = lab.experiment; | ||
var it = lab.test; | ||
var expect = Code.expect; | ||
@@ -31,11 +32,11 @@ | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.set('x', '123', null, function (err) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.get('x', function (err, value, cached, report) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.equal('123'); | ||
@@ -55,11 +56,11 @@ done(); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.set('x', '123', null, function (err) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.get('x', function (err, value, cached, report) { | ||
expect(err).to.not.exist; | ||
expect(value).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.not.exist(); | ||
done(); | ||
@@ -78,10 +79,10 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.set('x', '123', 1000, function (err) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.get('x', function (err, value, cached, report) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.equal('123'); | ||
@@ -100,4 +101,4 @@ done(); | ||
expect(err).to.not.exist; | ||
expect(value).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.not.exist(); | ||
done(); | ||
@@ -113,3 +114,3 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
done(); | ||
@@ -125,3 +126,3 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
done(); | ||
@@ -143,4 +144,4 @@ }); | ||
expect(err).to.not.exist; | ||
expect(value).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.not.exist(); | ||
done(); | ||
@@ -178,11 +179,11 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.set({ id: 'x' }, '123', null, function (err) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.get({ id: 'x' }, function (err, value, cached, report) { | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.equal('123'); | ||
@@ -202,7 +203,7 @@ done(); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
policy.set(null, '123', null, function (err) { | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Invalid key'); | ||
@@ -212,3 +213,3 @@ | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Invalid key'); | ||
@@ -251,3 +252,3 @@ done(); | ||
expect(err).to.be.instanceOf(Error); | ||
expect(value).to.not.exist; | ||
expect(value).to.not.exist(); | ||
done(); | ||
@@ -351,5 +352,5 @@ }); | ||
expect(err).to.not.exist; | ||
expect(err).to.not.exist(); | ||
expect(value).to.equal('new result'); | ||
expect(cached).to.not.exist; | ||
expect(cached).to.not.exist(); | ||
done(); | ||
@@ -386,2 +387,35 @@ }); | ||
it('switches rules after construction', function (done) { | ||
var rule = { | ||
expiresIn: 100, | ||
staleIn: 20, | ||
staleTimeout: 5, | ||
generateFunc: function (id, next) { | ||
return next(null, { gen: ++gen }); | ||
} | ||
}; | ||
var client = new Catbox.Client(Import, { partition: 'test-partition' }); | ||
var policy = new Catbox.Policy({ expiresIn: 100 }, client, 'test-segment'); | ||
var gen = 0; | ||
client.start(function () { | ||
policy.get('test', function (err, value, cached, report) { | ||
expect(value).to.not.exist(); | ||
policy.rules(rule); | ||
policy.get('test', function (err, value, cached, report) { | ||
expect(value.gen).to.equal(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('returns the processed cached item after cache error', function (done) { | ||
@@ -862,3 +896,3 @@ | ||
expect(err).to.exist; | ||
expect(err).to.exist(); | ||
@@ -1563,3 +1597,3 @@ policy._get('test', function (err, value3) { | ||
expect(ttl).to.be.closeTo(22 * 60 * 60 * 1000, 60 * 1000); | ||
expect(ttl).to.be.about(22 * 60 * 60 * 1000, 60 * 1000); | ||
done(); | ||
@@ -1579,3 +1613,3 @@ }); | ||
var ttl = Catbox.policy.ttl(rule); | ||
expect(ttl).to.be.closeTo(23 * 60 * 60 * 1000, 60 * 60 * 1000); | ||
expect(ttl).to.be.about(23 * 60 * 60 * 1000, 60 * 60 * 1000); | ||
done(); | ||
@@ -1597,3 +1631,3 @@ }); | ||
var ttl = Catbox.policy.ttl(rule, created); | ||
expect(ttl).to.be.closeTo(60 * 60 * 1000, 60 * 60 * 1000); | ||
expect(ttl).to.be.about(60 * 60 * 1000, 60 * 60 * 1000); | ||
done(); | ||
@@ -1600,0 +1634,0 @@ }); |
145647
2090
131
2