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();
if (has_local_storage())
localStorage.clear();
} catch (e) {
exception_error("init_second_stage", e);
}

View File

@ -1696,12 +1696,18 @@ 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;
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);
}
@ -1727,6 +1733,18 @@ function cache_find(id) {
return a;
} else {
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) {
@ -1734,6 +1752,7 @@ function cache_find(id) {
}
}
}
}
return false;
}
@ -1752,6 +1771,18 @@ function cache_find_param(id, param) {
return a;
} else {
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) {
@ -1759,6 +1790,7 @@ function cache_find_param(id, param) {
}
}
}
}
return false;
}
@ -1777,6 +1809,10 @@ function cache_check(id) {
return a;
} else {
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) {
@ -1784,6 +1820,7 @@ function cache_check(id) {
}
}
}
}
return false;
}
@ -1802,6 +1839,11 @@ function cache_check_param(id, param) {
return a;
} else {
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) {
@ -1809,6 +1851,7 @@ function cache_check_param(id, param) {
}
}
}
}
return false;
}
@ -1822,12 +1865,18 @@ function cache_expire() {
db.execute("DELETE FROM cache WHERE added < ? - 86400", [ts]);
} else {
if (has_local_storage()) {
while (localStorage.length > 25) {
localStorage.removeItem(localStorage.key(localStorage.length-1));
}
} else {
while (article_cache.length > 25) {
article_cache.shift();
}
}
}
}
function cache_flush() {
article_cache = new Array();
@ -1841,6 +1890,15 @@ function cache_invalidate(id) {
return rs.rowsAffected != 0;
} else {
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);
}
} else {
var i = 0
while (i < article_cache.length) {
@ -1852,6 +1910,7 @@ function cache_invalidate(id) {
i++;
}
}
}
console.log("cache_invalidate: id not found: " + id);
return false;