-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
146 lines (127 loc) · 3.26 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var crypto = require('crypto');
var Memcache = require('memcached');
var gracenode = require('../../');
var log = gracenode.log.create('datacache');
var config = null;
/*
* "datacache": {
* "hosts": ["host", "host"...] or { "host": load balance(int)... },
* "ttl": int,
* "options": {}
* }
*/
module.exports.readConfig = function (configIn) {
if (!configIn.hosts || !configIn.ttl) {
throw new Error('invalid configuration: \n' + JSON.stringify(configIn, null, 4));
}
config = configIn;
};
module.exports.create = function (name) {
return new Cache(name);
};
function Cache(name) {
this._cache = new Memcache(config.hosts, config.options || null);
this._name = name;
}
Cache.prototype.get = function (rawKey, cb) {
var sDate = new Date();
var start = sDate.getTime();
var that = this;
this._getBaseCache(function (error, timestamp) {
if (error) {
return cb(error);
}
if (timestamp) {
// try cache
var key = that._createKey(rawKey + timestamp + that._name);
return that._cache.get(key, function (error, value) {
if (error) {
return cb(error);
}
if (value !== null) {
log.verbose('cache found [true]: ' + key);
}
var eDate = new Date();
var end = eDate.getTime();
log.verbose('get cache took [' + (end - start) + ' ms]');
cb(null, value);
});
}
cb();
});
};
Cache.prototype.set = function (rawKey, value, cb) {
var that = this;
var setKey = function (timestamp, callback) {
var key = that._createKey(rawKey + timestamp + that._name);
that._cache.set(key, value, config.ttl, function (error) {
if (error) {
return cb(error);
}
log.verbose('cache set:', rawKey, key);
callback();
});
};
this._getBaseCache(function (error, timestamp) {
if (error) {
return cb(error);
}
if (!timestamp) {
// no timestamp
return that._createBaseCache(function (error, timestamp) {
if (error) {
return cb(error);
}
setKey(timestamp, cb);
});
}
// found timestamp
setKey(timestamp, cb);
});
};
// call this when you change the data to flush out the old cache
Cache.prototype.update = function (cb) {
log.verbose('update base cache');
this._createBaseCache(cb);
};
Cache.flush = function (cb) {
var key = this._createKey(this._name);
var that = this;
this.del(key, function (error) {
if (error) {
return cb(error);
}
log.verbose('base cache deleted > cache for "' + that._name + '" flushed:', that._name);
cb();
});
};
Cache.prototype._getBaseCache = function (cb) {
var key = this._createKey(this._name);
var that = this;
this._cache.get(key, function (error, value) {
if (error) {
return cb(error);
}
log.verbose('base cache found [' + (value ? true : false) + ']: ' + that._name);
cb(null, value);
});
};
Cache.prototype._createBaseCache = function (cb) {
var key = this._createKey(this._name);
var that = this;
var d = new Date();
var timestamp = d.getTime();
this._cache.set(key, timestamp, config.ttl, function (error) {
if (error) {
return cb(error);
}
log.verbose('base cache created:', that._name, timestamp);
cb(null, timestamp);
});
};
Cache.prototype._createKey = function (src) {
var md5 = crypto.createHash('md5');
var key = md5.update(src).digest('base64');
log.verbose('create cache key: ' + src + ' -> ' + key);
return key;
};