Created
June 12, 2015 08:53
-
-
Save GoldenEra/f841535bdf870ee3aafe to your computer and use it in GitHub Desktop.
javascript cache
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
//reference:http://www.dustindiaz.com/javascript-cache-provider | |
function CacheProvider() { | |
// values will be stored here | |
this._cache = {}; | |
} | |
if (CacheProvider.hasLocalStorage) { | |
Storage.prototype.setObject = function(key, value) { | |
this.setItem(key, JSON.stringify(value)); | |
}; | |
Storage.prototype.getObject = function(key) { | |
return JSON.parse(this.getItem(key)); | |
}; | |
} | |
CacheProvider.prototype = { | |
/** | |
* {String} k - the key | |
* {Boolean} local - get this from local storage? | |
* {Boolean} o - is the value you put in local storage an object? | |
*/ | |
get: function(k, local, o) { | |
if (local && CacheProvider.hasLocalStorage) { | |
var action = o ? 'getObject' : 'getItem'; | |
return localStorage[action](k) || undefined; | |
} else { | |
return this._cache[k] || undefined; | |
} | |
}, | |
/** | |
* {String} k - the key | |
* {Object} v - any kind of value you want to store | |
* however only objects and strings are allowed in local storage | |
* {Boolean} local - put this in local storage | |
*/ | |
set: function(k, v, local) { | |
if (local && CacheProvider.hasLocalStorage) { | |
if (typeof v !== 'string') { | |
// make assumption if it's not a string, then we're storing an object | |
localStorage.setObject(k, v); | |
} else { | |
try { | |
localStorage.setItem(k, v); | |
} catch (ex) { | |
if (ex.name == 'QUOTA_EXCEEDED_ERR') { | |
// developer needs to figure out what to start invalidating | |
throw new Exception(v); | |
return; | |
} | |
} | |
} | |
} else { | |
// put in our local object | |
this._cache[k] = v; | |
} | |
// return our newly cached item | |
return v; | |
}, | |
/** | |
* {String} k - the key | |
* {Boolean} local - put this in local storage | |
* {Boolean} o - is this an object you want to put in local storage? | |
*/ | |
clear: function(k, local, o) { | |
if (local && CacheProvider.hasLocalStorage) { | |
localStorage.removeItem(k); | |
} | |
// delete in both caches - doesn't hurt. | |
delete this._cache[k]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment