forked from newthuhole/hole_thu_frontend
improve cache and update style
This commit is contained in:
@@ -39,11 +39,14 @@
|
|||||||
}
|
}
|
||||||
@media screen and (max-width: 900px) {
|
@media screen and (max-width: 900px) {
|
||||||
.control-btn {
|
.control-btn {
|
||||||
flex: 0 0 2em;
|
flex: 0 0 2.5em;
|
||||||
}
|
}
|
||||||
.control-btn-label {
|
.control-btn-label {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
.control-search {
|
||||||
|
padding: 0 .5em;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.root-dark-mode .control-btn {
|
.root-dark-mode .control-btn {
|
||||||
|
|||||||
55
src/cache.js
55
src/cache.js
@@ -1,12 +1,16 @@
|
|||||||
const HOLE_CACHE_DB_NAME='hole_cache_db';
|
const HOLE_CACHE_DB_NAME='hole_cache_db';
|
||||||
const CACHE_DB_VER=1;
|
const CACHE_DB_VER=1;
|
||||||
const MAINTENANCE_STEP=500;
|
const MAINTENANCE_STEP=200;
|
||||||
const MAINTENANCE_COUNT=5000;
|
const MAINTENANCE_COUNT=2500;
|
||||||
|
|
||||||
|
const ENC_KEY=42;
|
||||||
|
|
||||||
class Cache {
|
class Cache {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.db=null;
|
this.db=null;
|
||||||
this.added_items_since_maintenance=0;
|
this.added_items_since_maintenance=0;
|
||||||
|
this.encrypt=this.encrypt.bind(this);
|
||||||
|
this.decrypt=this.decrypt.bind(this);
|
||||||
const open_req=indexedDB.open(HOLE_CACHE_DB_NAME,CACHE_DB_VER);
|
const open_req=indexedDB.open(HOLE_CACHE_DB_NAME,CACHE_DB_VER);
|
||||||
open_req.onerror=console.error.bind(console);
|
open_req.onerror=console.error.bind(console);
|
||||||
open_req.onupgradeneeded=(event)=>{
|
open_req.onupgradeneeded=(event)=>{
|
||||||
@@ -24,6 +28,40 @@ class Cache {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// use window.hole_cache.encrypt() only after cache is loaded!
|
||||||
|
encrypt(pid,data) {
|
||||||
|
let s=JSON.stringify(data);
|
||||||
|
let o='';
|
||||||
|
for(let i=0,key=(ENC_KEY^pid)%128;i<s.length;i++) {
|
||||||
|
let c=s.charCodeAt(i);
|
||||||
|
let new_key=(key^(c/2))%128;
|
||||||
|
o+=String.fromCharCode(key^s.charCodeAt(i));
|
||||||
|
key=new_key;
|
||||||
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use window.hole_cache.decrypt() only after cache is loaded!
|
||||||
|
decrypt(pid,s) {
|
||||||
|
let o='';
|
||||||
|
if(typeof(s)!==typeof('str'))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
for(let i=0,key=(ENC_KEY^pid)%128;i<s.length;i++) {
|
||||||
|
let c=key^s.charCodeAt(i);
|
||||||
|
o+=String.fromCharCode(c);
|
||||||
|
key=(key^(c/2))%128;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(o);
|
||||||
|
} catch(e) {
|
||||||
|
console.error('decrypt failed');
|
||||||
|
console.trace(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get(pid,target_version) {
|
get(pid,target_version) {
|
||||||
pid=parseInt(pid);
|
pid=parseInt(pid);
|
||||||
return new Promise((resolve,reject)=>{
|
return new Promise((resolve,reject)=>{
|
||||||
@@ -34,14 +72,15 @@ class Cache {
|
|||||||
const get_req=store.get(pid);
|
const get_req=store.get(pid);
|
||||||
get_req.onsuccess=()=>{
|
get_req.onsuccess=()=>{
|
||||||
let res=get_req.result;
|
let res=get_req.result;
|
||||||
if(!res) {
|
if(!res || !res.data_str) {
|
||||||
//console.log('comment cache miss '+pid);
|
//console.log('comment cache miss '+pid);
|
||||||
resolve(null);
|
resolve(null);
|
||||||
} else if(target_version===res.version) { // hit
|
} else if(target_version===res.version) { // hit
|
||||||
console.log('comment cache hit',pid);
|
console.log('comment cache hit',pid);
|
||||||
res.last_access=(+new Date());
|
res.last_access=(+new Date());
|
||||||
store.put(res);
|
store.put(res);
|
||||||
resolve(res.data);
|
let data=this.decrypt(pid,res.data_str);
|
||||||
|
resolve(data); // obj or null
|
||||||
} else { // expired
|
} else { // expired
|
||||||
console.log('comment cache expired',pid,': ver',res.version,'target',target_version);
|
console.log('comment cache expired',pid,': ver',res.version,'target',target_version);
|
||||||
store.delete(pid);
|
store.delete(pid);
|
||||||
@@ -66,7 +105,7 @@ class Cache {
|
|||||||
store.put({
|
store.put({
|
||||||
pid: pid,
|
pid: pid,
|
||||||
version: target_version,
|
version: target_version,
|
||||||
data: data,
|
data_str: this.encrypt(pid,data),
|
||||||
last_access: +new Date(),
|
last_access: +new Date(),
|
||||||
});
|
});
|
||||||
if(++this.added_items_since_maintenance===MAINTENANCE_STEP)
|
if(++this.added_items_since_maintenance===MAINTENANCE_STEP)
|
||||||
@@ -127,7 +166,7 @@ class Cache {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function cache() {
|
export function cache() {
|
||||||
if(!window._cache)
|
if(!window.hole_cache)
|
||||||
window._cache=new Cache();
|
window.hole_cache=new Cache();
|
||||||
return window._cache;
|
return window.hole_cache;
|
||||||
}
|
}
|
||||||
Submodule src/infrastructure updated: 110f225b4e...4777c70b0c
Reference in New Issue
Block a user