Skip to content

Commit

Permalink
Merge 2cc6532 into bde80a0
Browse files Browse the repository at this point in the history
  • Loading branch information
woodpig07 committed Mar 1, 2019
2 parents bde80a0 + 2cc6532 commit 8491924
Show file tree
Hide file tree
Showing 5 changed files with 2,471 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ notifications:
email: false
node_js:
- "6"
- "5"
- "4"
services:
- redis-server
- docker
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To use the `redis` backend, setup a model using the [redis connector](https://gi
}
```

To use the `couchbase` backend, setup a model using the [couchbase3 connector](https://github.com/Wiredcraft/loopback-connector-couchbase3) and use the mixin `CacheModel` with the model. The TTL is in seconds usually but also has a special case, see http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.2/Bucket.html#touch.
To use the `couchbase` backend, setup a model using the [couchbase3 connector](https://github.com/Wiredcraft/loopback-connector-couchbase3) and use the mixin `CacheModel` with the model. The TTL is in seconds, for value larger than 30 * 24 * 60 * 60 seconds (30 days), it would be converted to absolute times (from the epoch) in seconds, see http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.2/Bucket.html#touch.

```
# The model JSON
Expand Down
10 changes: 9 additions & 1 deletion backends/couchbase.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

var convertToAbsExpiry = function(ttl) {
return Math.floor(Date.now() / 1000) + ttl;
};

/**
* Same syntax as a mixin.
*/
Expand All @@ -11,7 +15,11 @@ module.exports = function(Model, options) {
// @see https://github.com/Wiredcraft/loopback-connector-couchbase3
if (ctx.Model.getConnector().name === 'couchbase3') {
// @see http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.2/Bucket.html#touch
ctx.options.expiry = options.ttl;
if (options.ttl > 30 * 24 * 60 * 60) {
ctx.options.expiry = convertToAbsExpiry(options.ttl);
} else {
ctx.options.expiry = options.ttl;
}
return next();
}
// Nothing else supported for now.
Expand Down
39 changes: 39 additions & 0 deletions test/backend.couchbase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ var mixin = require('../mixins/cacheModel');
var DataSource = require('loopback-datasource-juggler').DataSource;

describe('Couchbase backend', function() {
var ttl3m = 90 * 24 * 60 * 60;
var expiry = Math.floor(Date.now() / 1000) + ttl3m;
var db;
var Person;
var Staff;
var id;

before(function() {
Expand All @@ -16,6 +19,13 @@ describe('Couchbase backend', function() {
bucket: {
name: 'test_bucket',
password: ''
},
designDocs: {
find: {
views: {
getExpiry: { map: `function(e,m){e._type&&emit(m.expiration)}` }
}
}
}
});
Person = db.createModel('person', {
Expand All @@ -25,10 +35,25 @@ describe('Couchbase backend', function() {
},
name: String
});
Staff = db.createModel('staff', {
id: {
type: String,
id: true
},
name: String
});
mixin(Person, {
backend: 'couchbase',
ttl: 2 //s
});
mixin(Staff, {
backend: 'couchbase',
ttl: ttl3m // 3 months
});
});

before(function() {
return db.autoupdate();
});

after(function() {
Expand Down Expand Up @@ -68,4 +93,18 @@ describe('Couchbase backend', function() {
}).catch(done);
}, 3000);
});

it('TTL larger than 30 days will be converted epoch time', function() {
return Staff.create({
name: 'Staff'
}).then(function(sta) {
return db.connector.view('find', 'getExpiry').then(function(res) {
var staff = res.find(function(item) {
return item.id = sta.id;
});
staff.should.be.Object();
staff.key.should.be.approximately(expiry, 10);
});
});
});
});

0 comments on commit 8491924

Please sign in to comment.