Skip to content

Instantly share code, notes, and snippets.

@cmaher
Last active August 29, 2015 14:08
Show Gist options
  • Save cmaher/88d7c9c003b6c1d42071 to your computer and use it in GitHub Desktop.
Save cmaher/88d7c9c003b6c1d42071 to your computer and use it in GitHub Desktop.
Basic in-memory localStorage API for use with cmaher/backbone.hoard
// around 5MB, matching common localStorage limit
var STORAGE_SIZE_DEFAULT_LIMIT = 5000000;
var InMemoryStorage = function () {
this.storage = {};
this.size = 0;
};
_.extend(InMemoryStorage.prototype, {
setItem: function (key, value) {
this.storage[key] = value;
this.size += value.length;
if (this.size > STORAGE_SIZE_DEFAULT_LIMIT) {
// Notify Hoard that the cache is full.
// This will trigger a cache invalidation.
throw new Error("On-Page Cache size exceeded");
}
},
getItem: function (key) {
var value = this.storage[key];
if (_.isUndefined(value)) {
value = null;
}
return value;
},
removeItem: function (key) {
var value = this.getItem(key);
delete this.storage[key];
if (value != null) {
this.size -= value.length;
}
}
});
Backbone.Hoard.backend = new InMemoryStorage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment