-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (48 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var AbstractClientStore = require('express-brute/lib/AbstractClientStore'),
couchbase = require('couchbase'),
_ = require('underscore');
var CouchbaseStore = module.exports = function (couchoptions, options) {
AbstractClientStore.apply(this, arguments);
this.options = _.extend({}, CouchbaseStore.defaults, options);
this.couchbaseOptions = _.extend({}, CouchbaseStore.bucketdefaults, couchoptions);
var cluster = new couchbase.Cluster(this.couchbaseOptions.cluster);
if (this.couchbaseOptions.username) {
cluster.authenticate(this.couchbaseOptions.username, this.couchbaseOptions.password);
this.client = cluster.openBucket(this.couchbaseOptions.bucket);
} else {
this.client = cluster.openBucket(this.couchbaseOptions.bucket, this.couchbaseOptions.password);
}
};
CouchbaseStore.prototype = Object.create(AbstractClientStore.prototype);
CouchbaseStore.prototype.set = function (key, value, lifetime, callback) {
this.client.upsert(this.options.prefix + key, value, { expiry: lifetime || 0 }, function (err, data) {
typeof callback == 'function' && callback.apply(this, arguments);
});
};
CouchbaseStore.prototype.get = function (key, callback) {
this.client.get(this.options.prefix + key, function (err, data) {
if (err && err.code != 13) { // Key not found
typeof callback == 'function' && callback(err, null);
} else {
if (data) {
data = data.value;
data.lastRequest = new Date(data.lastRequest);
data.firstRequest = new Date(data.firstRequest);
}
typeof callback == 'function' && callback(null, data);
}
});
};
CouchbaseStore.prototype.reset = function (key, callback) {
this.client.remove(this.options.prefix + key, function (err, data) {
typeof callback == 'function' && callback.apply(this, arguments);
});
};
CouchbaseStore.defaults = {
prefix: ''
};
CouchbaseStore.bucketdefaults = {
cluster: 'http://localhost:8091',
bucket: 'express-brute-store',
password: ''
};