Last active
December 17, 2015 04:38
-
-
Save stephenhandley/5551497 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var LocalStorageCache = (function () { | |
| function _now() { | |
| return (new Date()).getTime(); | |
| } | |
| function _isExpired(json) { | |
| var expired = false; | |
| if (json.hasOwnProperty('ttl')) { | |
| expired = ((json.timestamp + json.ttl) < _now()); | |
| } | |
| return expired; | |
| } | |
| function LocalStorageCache (config) { | |
| // TODO: support namespace scoped clear by storing list of keys saved | |
| // TODO: also, support iteration over those keys | |
| if (!config.hasOwnProperty('namespace')) { | |
| config.namespace = null; | |
| } | |
| if (!config.hasOwnProperty('permanent')) { | |
| config.permanent = false; | |
| } | |
| this.config = config; | |
| } | |
| LocalStorageCache.prototype.namespacedKey = function namespacedKey (key) { | |
| if (this.config.namespace) { | |
| key = this.config.namespace + ':' + key; | |
| }; | |
| return key; | |
| } | |
| LocalStorageCache.prototype.get = function get (key) { | |
| var result = null; | |
| key = this.namespacedKey(key); | |
| var json_string = localStorage.getItem(key); | |
| if (json_string) { | |
| var wrapper = JSON.parse(json_string); | |
| if (_isExpired(wrapper)) { | |
| this.remove(key); | |
| } else { | |
| result = wrapper.data; | |
| } | |
| } | |
| return result; | |
| }; | |
| LocalStorageCache.prototype.ttl = function (options) { | |
| if (!this.config.permanent) { | |
| if (options && options.hasOwnProperty('ttl')) { | |
| return options.ttl; | |
| } | |
| if (this.config.hasOwnProperty('ttl')) { | |
| return this.config.ttl; | |
| } | |
| } | |
| return null; | |
| } | |
| // options: | |
| // ttl: time-to-live in milliseconds, null results in permanent storage | |
| LocalStorageCache.prototype.set = function set (key, json, options) { | |
| var wrapper = { | |
| data: json | |
| }; | |
| var ttl = this.ttl(options); | |
| if (ttl) { | |
| wrapper.ttl = ttl; | |
| wrapper.timestamp = _now(); | |
| } | |
| key = this.namespacedKey(key); | |
| var json_string = JSON.stringify(wrapper); | |
| return localStorage.setItem(key, json_string); // Note: localStorage.setItem has undefined return val | |
| }; | |
| LocalStorageCache.prototype.remove = function (key) { | |
| key = this.namespacedKey(key); | |
| return localStorage.removeItem(key); // note: localStorage.remove has undefined return val | |
| }; | |
| return LocalStorageCache; | |
| })(); |
This file contains hidden or 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
| <html> | |
| <head> | |
| <script src="./LocalStorageCache.js"></script> | |
| <script> | |
| (function () { | |
| var lsc = new LocalStorageCache({namespace: 'test'}); | |
| lsc.set('barf', 'hi'); | |
| if (lsc.get('barf') != 'hi') { | |
| throw "fail1"; | |
| } | |
| lsc.remove('barf'); | |
| if (lsc.get('barf') != null) { | |
| throw "fail2"; | |
| } | |
| var lsc2 = new LocalStorageCache({namespace: 'test2'}); | |
| lsc2.set('only here', 'hihihi'); | |
| if (lsc2.get('only here') != 'hihihi') { | |
| throw "fail3"; | |
| } | |
| if (lsc.get('only here') != null) { | |
| throw "fail4"; | |
| } | |
| lsc.set('timey', {just: 'for awhile'}, {ttl: 250}); | |
| if (lsc.get('timey').just != 'for awhile') { | |
| throw "fail5"; | |
| } | |
| setTimeout(function () { | |
| if (lsc.get('timey') != null) { | |
| throw "fail6"; | |
| } | |
| }, 300); | |
| console.log('pass'); | |
| })(); | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment