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.

Revisions

  1. k0pernikus revised this gist Oct 12, 2012. 1 changed file with 23 additions and 25 deletions.
    48 changes: 23 additions & 25 deletions getCachedJSON.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,31 @@
    (function ($) {
    $.extend({
    getCachedJSON: function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();
    jQuery.extend({
    getCachedJSON: function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();

    var cache = {
    data:null,
    timestamp:null
    };
    var cache = {
    data:null,
    timestamp:null
    };

    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);
    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);

    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;
    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;

    if (validCache) {
    callback(cache.data);
    return true;
    }
    if (validCache) {
    callback(cache.data);
    return true;
    }
    }

    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();
    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();

    window.localStorage[url] = JSON.stringify(cache);
    window.localStorage[url] = JSON.stringify(cache);

    callback(cache.data);
    });
    }
    });
    }(jQuery));
    callback(cache.data);
    });
    }
    });
  2. k0pernikus revised this gist Oct 12, 2012. 1 changed file with 25 additions and 21 deletions.
    46 changes: 25 additions & 21 deletions getCachedJSON.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,33 @@
    var getCachedJSON = function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();
    (function ($) {
    $.extend({
    getCachedJSON: function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();

    var cache = {
    data:null,
    timestamp:null
    };
    var cache = {
    data:null,
    timestamp:null
    };

    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);
    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);

    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;
    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;

    if (validCache) {
    callback(cache.data);
    return true;
    }
    }
    if (validCache) {
    callback(cache.data);
    return true;
    }
    }

    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();
    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();

    window.localStorage[url] = JSON.stringify(cache);
    window.localStorage[url] = JSON.stringify(cache);

    callback(data);
    callback(cache.data);
    });
    }
    });
    };
    }(jQuery));
  3. k0pernikus revised this gist Oct 8, 2012. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions getCachedJSON.js
    Original file line number Diff line number Diff line change
    @@ -22,10 +22,7 @@ var getCachedJSON = function (url, callback) {
    cache.data = data;
    cache.timestamp = new Date().getTime();

    console.log(cache);

    window.localStorage[url] = JSON.stringify(cache);
    console.log(window.localStorage[url]);

    callback(data);
    });
  4. k0pernikus revised this gist Oct 8, 2012. 1 changed file with 22 additions and 22 deletions.
    44 changes: 22 additions & 22 deletions getCachedJSON.js
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,32 @@
    var getCachedJSON = function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();

    var cache = {
    data:null,
    timestamp:null
    };
    var cache = {
    data:null,
    timestamp:null
    };

    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);
    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);

    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;
    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;

    if (validCache) {
    callback(cache.data);
    return true;
    }
    if (validCache) {
    callback(cache.data);
    return true;
    }
    }

    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();
    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();

    console.log(cache);
    console.log(cache);

    window.localStorage[url] = JSON.stringify(cache);
    console.log(window.localStorage[url]);
    window.localStorage[url] = JSON.stringify(cache);
    console.log(window.localStorage[url]);

    callback(data);
    });
    };
    callback(data);
    });
    };
  5. k0pernikus revised this gist Oct 8, 2012. 2 changed files with 32 additions and 13 deletions.
    32 changes: 32 additions & 0 deletions getCachedJSON.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    var getCachedJSON = function (url, callback) {
    var cacheTimeInMs = 3600000;
    var currentTimeInMs = new Date().getTime();

    var cache = {
    data:null,
    timestamp:null
    };

    if (typeof window.localStorage[url] !== "undefined") {
    cache = JSON.parse(window.localStorage[url]);

    var validCache = (currentTimeInMs - cache.timestamp) < cacheTimeInMs;

    if (validCache) {
    callback(cache.data);
    return true;
    }
    }

    $.getJSON(url, function (data) {
    cache.data = data;
    cache.timestamp = new Date().getTime();

    console.log(cache);

    window.localStorage[url] = JSON.stringify(cache);
    console.log(window.localStorage[url]);

    callback(data);
    });
    };
    13 changes: 0 additions & 13 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    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);
    });
    }
    };
  6. @kpuputti kpuputti created this gist Jun 22, 2011.
    13 changes: 13 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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);
    });
    }
    };