Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var SimpleCache = function() {
var _values = {};
var getTime = function() {
return Date.now() / 1000 | 0
}
this.get = function(key) {
if (!(key in _values)) {
return null
}
var vl = _values[key];
if (!vl.time) {
return vl.value
} else if (vl.time < getTime()) {
this.remove(key)
return null;
} else {
return vl.value
}
}
this.remove = function(key) {
delete _values[key];
};
this.set = function(key, value, time) {
_values[key] = {
'time': time ? getTime() + time : 0,
'value': value
};
};
this.values = function() { return _values; };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.