Skip to content

Instantly share code, notes, and snippets.

@alixaxel
Created May 25, 2016 19:31
Show Gist options
  • Save alixaxel/15987ab0237fb34c63e4276819ba8baa to your computer and use it in GitHub Desktop.
Save alixaxel/15987ab0237fb34c63e4276819ba8baa to your computer and use it in GitHub Desktop.
Storage.js
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