pitaya-launcher/js/util/cache.js

30 lines
611 B
JavaScript

var crypto = require('crypto');
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) {
var shasum = crypto.createHash('md5');
shasum.update(str);
return shasum.digest('hex');
};
module.exports = Cache;