Created
May 25, 2016 19:31
-
-
Save alixaxel/15987ab0237fb34c63e4276819ba8baa to your computer and use it in GitHub Desktop.
Storage.js
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
Storage.prototype.get = function (key) { | |
try { | |
var result = JSON.parse(this.getItem(key)); | |
if ((result !== null) && (result.hasOwnProperty('expiration') === true)) { | |
if ((result.expiration >= (new Date())) || (result.expiration == null)) { | |
return result.value || null; | |
} | |
this.removeItem(key); | |
} else { | |
return this.getItem(key); | |
} | |
} catch (exception) { | |
console.error(exception); | |
} | |
return null; | |
}; | |
Storage.prototype.set = function (key, value, expiration) { | |
if (Number.isFinite(expiration) === true) { | |
expiration = (new Date()) + (expiration * 1000); | |
} | |
return this.setItem(key, JSON.stringify({ | |
value: value, | |
expiration: expiration | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment