Skip to content

Instantly share code, notes, and snippets.

@k0pernikus
Forked from kpuputti/gist:1040118
Created October 8, 2012 15:14
Show Gist options
  • Select an option

  • Save k0pernikus/3853059 to your computer and use it in GitHub Desktop.

Select an option

Save k0pernikus/3853059 to your computer and use it in GitHub Desktop.
jQuery.getJSON abstraction to cache data to localStorage with invalidation option based time
var getCachedJSON = function (url, callback) {
var cachedData = window.localStorage[url];
if (cachedData) {
log('Data already cached, returning from cache:', url);
callback(JSON.parse(cachedData));
} else {
$.getJSON(url, function (data) {
log('Fetched data, saving to cache:', url);
window.localStorage[url] = JSON.stringify(data);
callback(data);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment