-
-
Save anhang/1096149 to your computer and use it in GitHub Desktop.
AZHU.storage = { | |
save : function(key, jsonData, expirationMin){ | |
if (!Modernizr.localstorage){return false;} | |
var expirationMS = expirationMin * 60 * 1000; | |
var record = {value: JSON.stringify(jsonData), timestamp: new Date().getTime() + expirationMS} | |
localStorage.setItem(key, JSON.stringify(record)); | |
return jsonData; | |
}, | |
load : function(key){ | |
if (!Modernizr.localstorage){return false;} | |
var record = JSON.parse(localStorage.getItem(key)); | |
if (!record){return false;} | |
return (new Date().getTime() < record.timestamp && JSON.parse(record.value)); | |
} | |
} |
boiawang
commented
Jul 6, 2015
I like it.
I switched the Modernizr check for just:
if (typeof (Storage) == "undefined") { return false; }
Put together a fiddle showing this in action: http://jsfiddle.net/7nzdzvj8/1/
cheers for this
Ha, after 5 years, people are finding this gist!
yep , i just find it :)
Awesome, I found it too. Thank
Just found this gist too. Great share, thanks!
Thanks for this @anhang!
I can't believe that this is 6 years old and despite looking for how long local storage is valid for this is the (seemingly) only function that allows you to add an expiry date. Good Stuff @anhang!
Thanks
👍
👍
This would just not load the storage, while it is still available in the browser for other scripts.
Maybe a safer solution could be to encrypt the local storage and store the key in an cookie with expiry date?
7 years and still very helpful. Thank you!
Good contributions endure over time. Thank you.
This code has one problem (as I see!?), it didn't delete the expired cache.
https://github.com/liesislukas/localstorage-ttl/blob/master/index.js