implement html5 localStorage support for client-side cache

This commit is contained in:
Andrew Dolgov 2010-11-07 12:54:43 +03:00
parent 50eefedbfd
commit ff0010c182
3 changed files with 93 additions and 24 deletions

View File

@ -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;
}
}

View File

@ -421,6 +421,9 @@ function init_second_stage() {
enable_offline_reading(); enable_offline_reading();
if (has_local_storage())
localStorage.clear();
} catch (e) { } catch (e) {
exception_error("init_second_stage", e); exception_error("init_second_stage", e);
} }

View File

@ -1696,13 +1696,19 @@ function cache_inject(id, article, param) {
[id, article, param, ts]); [id, article, param, ts]);
} else { } else {
var cache_obj = new Array(); var cache_obj = {};
cache_obj["id"] = id; cache_obj["id"] = id;
cache_obj["data"] = article; cache_obj["data"] = article;
cache_obj["param"] = param; cache_obj["param"] = param;
cache_obj["added"] = new Date();
article_cache.push(cache_obj);
if (param) id = id + ":" + param;
if (has_local_storage())
localStorage.setItem(id, JSON.stringify(cache_obj));
else
article_cache.push(cache_obj);
} }
} else { } else {
@ -1728,9 +1734,22 @@ function cache_find(id) {
return a; return a;
} else { } else {
for (var i = 0; i < article_cache.length; i++) {
if (article_cache[i]["id"] == id) { if (has_local_storage()) {
return article_cache[i]["data"]; 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; return a;
} else { } else {
for (var i = 0; i < article_cache.length; i++) {
if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { if (has_local_storage()) {
return article_cache[i]["data"]; 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; return a;
} else { } else {
for (var i = 0; i < article_cache.length; i++) { if (has_local_storage) {
if (article_cache[i]["id"] == id) { if (localStorage.getItem(id))
return true; 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; return a;
} else { } else {
for (var i = 0; i < article_cache.length; i++) {
if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) { if (has_local_storage) {
return true; 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 { } else {
while (article_cache.length > 25) { if (has_local_storage()) {
article_cache.shift(); 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; return rs.rowsAffected != 0;
} else { } else {
var i = 0 if (has_local_storage()) {
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i);
if (key == id || key.indexOf(id + ":") == 0)
localStorage.removeItem(key);
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++; } 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++;
}
} }
} }