From: Andrew Dolgov Date: Sun, 7 Nov 2010 09:54:43 +0000 (+0300) Subject: implement html5 localStorage support for client-side cache X-Git-Tag: 1.5.0~385 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=ff0010c182c1643cc5e0c516e95ed50e18004068;p=tt-rss.git implement html5 localStorage support for client-side cache --- diff --git a/functions.js b/functions.js index 2aa0dab4..8b4945ed 100644 --- a/functions.js +++ b/functions.js @@ -2184,3 +2184,10 @@ function backend_sanity_check_callback(transport) { } } +function has_local_storage() { + try { + return 'localStorage' in window && window['localStorage'] != null; + } catch (e) { + return false; + } +} diff --git a/tt-rss.js b/tt-rss.js index 51c1d3c5..6dd53c8b 100644 --- a/tt-rss.js +++ b/tt-rss.js @@ -421,6 +421,9 @@ function init_second_stage() { enable_offline_reading(); + if (has_local_storage()) + localStorage.clear(); + } catch (e) { exception_error("init_second_stage", e); } diff --git a/viewfeed.js b/viewfeed.js index a6065ecb..e2b20779 100644 --- a/viewfeed.js +++ b/viewfeed.js @@ -1696,13 +1696,19 @@ function cache_inject(id, article, param) { [id, article, param, ts]); } else { - var cache_obj = new Array(); + var cache_obj = {}; cache_obj["id"] = id; cache_obj["data"] = article; cache_obj["param"] = param; - - article_cache.push(cache_obj); + cache_obj["added"] = new Date(); + + if (param) id = id + ":" + param; + + if (has_local_storage()) + localStorage.setItem(id, JSON.stringify(cache_obj)); + else + article_cache.push(cache_obj); } } else { @@ -1728,9 +1734,22 @@ function cache_find(id) { return a; } else { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id) { - return article_cache[i]["data"]; + + if (has_local_storage()) { + var cache_obj = localStorage.getItem(id); + + if (cache_obj) { + cache_obj = JSON.parse(cache_obj); + + if (cache_obj) + return cache_obj['data']; + } + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id) { + return article_cache[i]["data"]; + } } } } @@ -1753,9 +1772,22 @@ function cache_find_param(id, param) { return a; } else { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { - return article_cache[i]["data"]; + + if (has_local_storage()) { + var cache_obj = localStorage.getItem(id + ":" + param); + + if (cache_obj) { + cache_obj = JSON.parse(cache_obj); + + if (cache_obj) + return cache_obj['data']; + } + + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { + return article_cache[i]["data"]; + } } } } @@ -1778,9 +1810,14 @@ function cache_check(id) { return a; } else { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id) { - return true; + if (has_local_storage) { + if (localStorage.getItem(id)) + return true; + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id) { + return true; + } } } } @@ -1803,9 +1840,15 @@ function cache_check_param(id, param) { return a; } else { - for (var i = 0; i < article_cache.length; i++) { - if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { - return true; + + if (has_local_storage) { + if (localStorage.getItem(id + ':' + param)) + return true; + } else { + for (var i = 0; i < article_cache.length; i++) { + if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { + return true; + } } } } @@ -1823,8 +1866,14 @@ function cache_expire() { } else { - while (article_cache.length > 25) { - article_cache.shift(); + if (has_local_storage()) { + while (localStorage.length > 25) { + localStorage.removeItem(localStorage.key(localStorage.length-1)); + } + } else { + while (article_cache.length > 25) { + article_cache.shift(); + } } } } @@ -1841,15 +1890,25 @@ function cache_invalidate(id) { return rs.rowsAffected != 0; } else { - var i = 0 + if (has_local_storage()) { + for (var i = 0; i < localStorage.length; i++) { + var key = localStorage.key(i); - while (i < article_cache.length) { - if (article_cache[i]["id"] == id) { - console.log("cache_invalidate: removed id " + id); - article_cache.splice(i, 1); - return true; + if (key == id || key.indexOf(id + ":") == 0) + localStorage.removeItem(key); + + } + } else { + var i = 0 + + while (i < article_cache.length) { + if (article_cache[i]["id"] == id) { + console.log("cache_invalidate: removed id " + id); + article_cache.splice(i, 1); + return true; + } + i++; } - i++; } }