From 681fc1dfb01298ac17450c3416c53f0ac53b19a5 Mon Sep 17 00:00:00 2001 From: xmcp Date: Tue, 4 Feb 2020 23:43:41 +0800 Subject: [PATCH] improve cache and update style --- src/Title.css | 5 ++++- src/cache.js | 55 +++++++++++++++++++++++++++++++++++++++------- src/infrastructure | 2 +- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/Title.css b/src/Title.css index dee57edc..0ef4f2cf 100644 --- a/src/Title.css +++ b/src/Title.css @@ -39,11 +39,14 @@ } @media screen and (max-width: 900px) { .control-btn { - flex: 0 0 2em; + flex: 0 0 2.5em; } .control-btn-label { display: none; } + .control-search { + padding: 0 .5em; + } } .root-dark-mode .control-btn { diff --git a/src/cache.js b/src/cache.js index 5749c831..9c0e4009 100644 --- a/src/cache.js +++ b/src/cache.js @@ -1,12 +1,16 @@ const HOLE_CACHE_DB_NAME='hole_cache_db'; const CACHE_DB_VER=1; -const MAINTENANCE_STEP=500; -const MAINTENANCE_COUNT=5000; +const MAINTENANCE_STEP=200; +const MAINTENANCE_COUNT=2500; + +const ENC_KEY=42; class Cache { constructor() { this.db=null; 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); open_req.onerror=console.error.bind(console); 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{ @@ -34,14 +72,15 @@ class Cache { const get_req=store.get(pid); get_req.onsuccess=()=>{ let res=get_req.result; - if(!res) { + if(!res || !res.data_str) { //console.log('comment cache miss '+pid); resolve(null); } else if(target_version===res.version) { // hit console.log('comment cache hit',pid); res.last_access=(+new Date()); store.put(res); - resolve(res.data); + let data=this.decrypt(pid,res.data_str); + resolve(data); // obj or null } else { // expired console.log('comment cache expired',pid,': ver',res.version,'target',target_version); store.delete(pid); @@ -66,7 +105,7 @@ class Cache { store.put({ pid: pid, version: target_version, - data: data, + data_str: this.encrypt(pid,data), last_access: +new Date(), }); if(++this.added_items_since_maintenance===MAINTENANCE_STEP) @@ -127,7 +166,7 @@ class Cache { }; export function cache() { - if(!window._cache) - window._cache=new Cache(); - return window._cache; + if(!window.hole_cache) + window.hole_cache=new Cache(); + return window.hole_cache; } \ No newline at end of file diff --git a/src/infrastructure b/src/infrastructure index 110f225b..4777c70b 160000 --- a/src/infrastructure +++ b/src/infrastructure @@ -1 +1 @@ -Subproject commit 110f225b4ef614a52ce603ae1338b7fbecbda8ac +Subproject commit 4777c70b0c65b76f9b473954e966ea922e41740d