Skip to content

Instantly share code, notes, and snippets.

@juanbzpy
Forked from pamelafox/localstore.js
Last active August 29, 2015 14:07
Show Gist options
  • Save juanbzpy/66b5a02f351d93274fc8 to your computer and use it in GitHub Desktop.
Save juanbzpy/66b5a02f351d93274fc8 to your computer and use it in GitHub Desktop.
/**
* LocalStore is a *super* simple abstraction around localStorage for easy
* get/set/delete. We may end up wanting something more powerful like
* BankersBox, but for now this is much lighter weight.
*
* If you ever need to completely wipe LocalStore for *all* users when,
* say, changing the format of data being cached, just bump up the "version"
* property below.
*/
window.LocalStore = {
// Bump up "version" any time you want to completely wipe LocalStore results.
// This lets us expire values on all users' LocalStores when deploying
// a new version, if necessary.
version: 4,
keyPrefix: "ka",
cacheKey: function(key) {
if (!key) {
throw new Error("Attempting to use LocalStore without a key");
}
return [this.keyPrefix, this.version, key].join(":");
},
/**
* Get whatever data was associated with key. Returns null if no data is
* associated with the key, regardless of key's value (null, undefined, "monkey").
*/
get: function(key) {
if (!this.isEnabled()) {
return undefined;
}
try {
var data = window.localStorage[LocalStore.cacheKey(key)];
if (data) {
return JSON.parse(data);
}
} catch(e) {
// If we had trouble retrieving, like FF's NS_FILE_CORRUPTED
}
return null;
},
/**
* Store data associated with key in localStorage
*/
set: function(key, data) {
if (!this.isEnabled()) {
throw new Error("LocalStore is not enabled");
}
var stringified = JSON.stringify(data),
cacheKey = LocalStore.cacheKey(key);
try {
window.localStorage[cacheKey] = stringified;
} catch (e) {
// If we had trouble storing in localStorage, we may've run over
// the browser's 5MB limit. This should be rare, but when hit, clear
// everything out.
LocalStore.clearAll();
}
},
/**
* Delete whatever data was associated with key
*/
del: function(key) {
if (!this.isEnabled()) {
return;
}
var cacheKey = this.cacheKey(key);
if (cacheKey in window.localStorage) {
// IE throws when deleting a key that's not currently in
// localStorage.
delete window.localStorage[cacheKey];
}
},
isEnabled: function() {
var enabled, uid = String(+(new Date));
try {
window.sessionStorage[uid] = uid;
enabled = (window.sessionStorage[uid] === uid);
window.sessionStorage.removeItem(uid);
return enabled;
} catch (e) {
return false;
}
},
/**
* Delete all cached objects from localStorage
*/
clearAll: function() {
if (!this.isEnabled()) {
return;
}
try {
var i = 0;
while (i < localStorage.length) {
var key = localStorage.key(i);
if (key.indexOf(LocalStore.keyPrefix + ":") === 0) {
delete localStorage[key];
} else {
i++;
}
}
} catch(e) {
// If we had trouble accessing .length, like FF's NS_FILE_CORRUPTED
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment