Created
July 19, 2018 23:34
-
-
Save scriptburn/9def3bd109ae03e71ed215adbdcba62b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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