2015-09-17 17:29:59 +02:00
|
|
|
var crypto = require('crypto');
|
2015-09-02 14:26:23 +02:00
|
|
|
|
|
|
|
function Cache() {
|
|
|
|
this._store = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache.prototype.get = function(key) {
|
|
|
|
key = this._serialize(key);
|
|
|
|
return key in this._store ? this._store[key] : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
Cache.prototype.set = function(key, value) {
|
|
|
|
key = this._serialize(key);
|
|
|
|
this._store[key] = value;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Cache.prototype._serialize = function(mixedKey) {
|
|
|
|
var json = JSON.stringify(mixedKey);
|
|
|
|
return this._hash(json);
|
|
|
|
};
|
|
|
|
|
|
|
|
Cache.prototype._hash = function(str) {
|
2015-09-17 17:29:59 +02:00
|
|
|
var shasum = crypto.createHash('md5');
|
|
|
|
shasum.update(str);
|
|
|
|
return shasum.digest('hex');
|
2015-09-02 14:26:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Cache;
|