Skip to content

Instantly share code, notes, and snippets.

@scriptburn
Created July 19, 2018 23:34
Show Gist options
  • Save scriptburn/9def3bd109ae03e71ed215adbdcba62b to your computer and use it in GitHub Desktop.
Save scriptburn/9def3bd109ae03e71ed215adbdcba62b to your computer and use it in GitHub Desktop.
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