Skip to content

Commit

Permalink
Add remoting for KeyValue model TTL feature
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhoibm committed Aug 29, 2016
1 parent 32b879c commit 9db0682
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions common/models/key-value-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module.exports = function(KeyValueModel) {
throwNotAttached(this.modelName, 'expire');
};

// TODO add api docs
KeyValueModel.ttl = function(key, options, callback) {
throwNotAttached(this.modelName, 'ttl');
};

// TODO add api docs
KeyValueModel.keys = function(filter, options, callback) {
throwNotAttached(this.modelName, 'keys');
Expand Down Expand Up @@ -62,6 +67,15 @@ module.exports = function(KeyValueModel) {
http: { path: '/:key/expire', verb: 'put' },
});

this.remoteMethod('ttl', {
accepts: {
arg: 'key', type: 'string', required: true,
http: { source: 'path' },
},
returns: { arg: 'value', type: 'any', root: true },
http: { path: '/:key/ttl', verb: 'get' },
});

this.remoteMethod('keys', {
accepts: {
arg: 'filter', type: 'object', required: false,
Expand Down
41 changes: 41 additions & 0 deletions test/key-value-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,47 @@ describe('KeyValueModel', function() {
.expect(404, done);
});

it('provides "ttl(key)" at "GET /key/ttl"', function(done) {
request.put('/CacheItems/ttl-key?ttl=2000')
.end(function(err, res) {
if (err) return done(err);
request.get('/CacheItems/ttl-key/ttl')
.end(function(err, res) {
if (err) return done(err);
expect(res.body).to.be.number;
done();
});
});
});

it('returns 204 when getting TTL for a key that does not have TTL set',
function(done) {
request.put('/CacheItems/ttl-key')
.end(function(err, res) {
if (err) return done(err);
request.get('/CacheItems/ttl-key/ttl')
.expect(204, done);
});
});

it('returns 404 when getting TTL for a key when TTL has expired',
function(done) {
request.put('/CacheItems/ttl-key?ttl=10')
.end(function(err, res) {
setTimeout(function() {
if (err) return done(err);
request.get('/CacheItems/ttl-key/ttl')
.expect(404, done);
}, 20);
});
});

it('returns 404 when getting TTL for a key that does not exist',
function(done) {
request.get('/CacheItems/key-does-not-exist/ttl')
.expect(404, done);
});

it('provides "keys(filter)" at "GET /keys"', function(done) {
CacheItem.set('list-key', AN_OBJECT_VALUE, function(err) {
if (err) return done(err);
Expand Down

0 comments on commit 9db0682

Please sign in to comment.