Add a base.js module for http time-based caching

This commit is contained in:
Niko 2022-02-18 11:42:10 +01:00
parent e9feb6d912
commit 6980381ad5
1 changed files with 27 additions and 0 deletions

View File

@ -177,6 +177,33 @@ function _get_func_params(func) {
}
const http = {
cache: {
_key: function(name) { return 'httpcache__'+name },
get: function(name) {
const key = http.cache._key(name);
try { return JSON.parse(localStorage[key]) }
catch (e) { }
return localStorage[key];
},
set: function(name, value) {
const key = http.cache._key(name);
localStorage[key] = value;
localStorage[key+'_ts'] = new Date().getTime();
},
del: function(name) { http.cache.expire(name) },
expire: function(name) {
const key = http.cache._key(name);
delete localStorage[key];
delete localStorage[key+'_ts'];
},
expired: function(name, secs) {
const key = http.cache._key(name);
const ts = new Date().getTime();
const ts2 = parseInt(localStorage[key+'_ts']);
if (isNaN(ts2)) return true;
return ((ts - ts2) / 1000) > secs;
},
},
request: function(method, path, payload, callbk) {
payload = payload || null;
callbk = callbk || null;