Skip to content

Instantly share code, notes, and snippets.

@navneetkumar
Created September 14, 2013 12:49
Show Gist options
  • Save navneetkumar/6561768 to your computer and use it in GitHub Desktop.
Save navneetkumar/6561768 to your computer and use it in GitHub Desktop.
A sample client side cache implementation using local-storage
app = {}
app.cacheManager = function() {
var self = {};
var namedSpacedKey = function(key) {
if(app.release.version && app.release.version != "") return app.release.version + "." + key;
else return key;
};
self.cache = function(key,data){
localStorage.setObject(namedSpacedKey(key),data);
};
self.fetch = function(key){
return localStorage.getObject(namedSpacedKey(key));
};
self.cacheProperty = function(key,id,value){
var propertyStore = localStorage.getObject(namedSpacedKey(key)) || {};
propertyStore[id] = value;
localStorage.setObject(namedSpacedKey(key),propertyStore);
};
self.fetchProperty = function(key,id){
var propertyStore = localStorage.getObject(namedSpacedKey(key)) || {};
return propertyStore[id];
};
self.clear = function(key){
localStorage.removeItem(namedSpacedKey(key));
};
self.clearAll = function() {
var volatile_caches = _.pick(app.cacheKeys,'VOLATILE1','VOLATILE2');
_.each(volatile_caches,this.clear);
};
return self;
}();
app.cacheKeys = {
VOLATILE1 : 'volatile1',
VOLATILE2 : 'volatile2',
PERSISTENT1 : 'persistent1',
PERSISTENT2 : 'persistent2'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment