Skip to content

Commit

Permalink
Merge pull request #9 from dafortune/support-ttl
Browse files Browse the repository at this point in the history
Support TTL
  • Loading branch information
dafortune authored Jan 15, 2019
2 parents 5f6adcc + 3dbb1ad commit 7af4235
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function normalizeType(params) {
'per_interval',
'interval',
'size',
'unlimited'
'unlimited',
'ttl'
]);

INTERVAL_SHORTCUTS.forEach(ish => {
Expand All @@ -52,7 +53,7 @@ function normalizeType(params) {
type.size = type.per_interval;
}

if (type.per_interval) {
if (type.per_interval && !type.ttl) {
type.ttl = (type.size * type.interval) / type.per_interval;

if (process.env.NODE_ENV !== 'test') {
Expand Down
17 changes: 17 additions & 0 deletions test/db.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const types = {
size: 10
}
}
},
ttl_test: {
size: 10,
ttl: 100
}
};

Expand Down Expand Up @@ -138,6 +142,19 @@ describeForEachConfig((getConfig) => {
});
});

it('should support specifying a ttl in the bucket configuration', function (done) {
const params = { type: 'ttl_test', key: '211.45.66.1'};
db.take(params, function (err) {
if (err) return done(err);
setTimeout(function () {
db._types[params.type].db.get(params.key, function (err, result) {
assert.isUndefined(result);
done();
});
}, 300);
});
});

it('should return TRUE with right remaining and reset after filling up the bucket', (done) => {
var now = 1425920267;
db.take({
Expand Down

0 comments on commit 7af4235

Please sign in to comment.