Skip to content

Instantly share code, notes, and snippets.

@mdenisov
Forked from DzikuVx/gist:5693478
Created July 7, 2013 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdenisov/5944338 to your computer and use it in GitHub Desktop.
Save mdenisov/5944338 to your computer and use it in GitHub Desktop.
/**
* localStorage with expire wrapper
*/
var myStorage = (function() {
var self = {};
/**
* Method unsets value in localStorage
*/
self.unset = function(key) {
localStorage.removeItem(key);
};
/**
* Method gets value from localStorage
* @param key
*/
self.get = function(key) {
if (!localStorage[key]) {
return null;
}
var object = JSON.parse(localStorage[key]);
if (object.timestamp === null || new Date().getTime() < object.timestamp) {
return object.value;
} else {
return null;
}
};
/**
* Method sets value in local storage
*
* @param key
* @param value
* @param expire in seconds
*/
self.set = function(key, value, expire) {
var object;
if (!expire) {
object = {
value : value,
timestamp : null
};
} else {
object = {
value : value,
timestamp : new Date().getTime() + (expire * 1000)
};
}
localStorage[key] = JSON.stringify(object);
};
return self;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment