-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (50 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var Cacheman = require('cacheman');
var fuzzylogic = require('fuzzylogic');
var Fuchy = function (cacheName, config) {
if ('object' === typeof cacheName) {
config = cacheName;
};
config = config || {};
if (!config.engine) { throw new Error('Missing mandatory engine property'); }
this.noCacheKeys = config.noCacheKeys || [];
this.ttl = config.ttl || 3600;
this.cacheman = new Cacheman(cacheName, {
ttl: this.ttl,
engine: config.engine
});
};
Fuchy.prototype.get = function (key, callback) {
this.cacheman._engine.get(this.cacheman.key(key) + ':info', function (err, info) {
if (info) {
var timelapse = this._ttl * 1000;
var half = timelapse / 2;
var qrt = half / 2;
var mid_time = info.cached_time + half + qrt;
var request_time = Date.now();
var fuzzy = fuzzylogic.trapezoid(request_time, info.cached_time, info.cached_time, mid_time, info.expiration_time);
var valid = !!Math.round(fuzzy);
if (!valid) {
this.del(key);
this._engine.del(this.key(key) + ':info');
}
}
}.bind(this.cacheman));
this.cacheman.get(key, callback);
};
Fuchy.prototype.set = function (key, data, callback) {
var info = {
key: key,
cached_time: Date.now(),
expiration_time: Date.now() + (this.ttl * 1000)
};
if (this.noCacheKeys.indexOf(key) > -1) {
return callback(null, data);
};
this.cacheman._engine.set(this.cacheman.key(key) + ':info', info, this.ttl);
this.cacheman.set(key, data, callback);
};
Fuchy.prototype.verify = function (property) {
return this.noCacheKeys.indexOf(property) > -1;
};
module.exports = Fuchy;