Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. var SimpleCache = function() {
  2. var _values = {};
  3. var getTime = function() {
  4. return Date.now() / 1000 | 0
  5. }
  6. this.get = function(key) {
  7. if (!(key in _values)) {
  8. return null
  9. }
  10. var vl = _values[key];
  11. if (!vl.time) {
  12. return vl.value
  13. } else if (vl.time < getTime()) {
  14. this.remove(key)
  15. return null;
  16. } else {
  17. return vl.value
  18. }
  19. }
  20.  
  21. this.remove = function(key) {
  22. delete _values[key];
  23. };
  24. this.set = function(key, value, time) {
  25. _values[key] = {
  26. 'time': time ? getTime() + time : 0,
  27. 'value': value
  28. };
  29. };
  30. this.values = function() { return _values; };
  31. }
Add Comment
Please, Sign In to add comment