fix comment cache not expired

This commit is contained in:
xmcp
2020-01-11 01:33:47 +08:00
parent f9ccbc46ae
commit a16d3e28f3
2 changed files with 28 additions and 6 deletions

View File

@@ -25,6 +25,7 @@ class Cache {
}
get(pid,target_version) {
pid=parseInt(pid);
return new Promise((resolve,reject)=>{
if(!this.db)
return resolve(null);
@@ -37,12 +38,12 @@ class Cache {
//console.log('comment cache miss '+pid);
resolve(null);
} else if(target_version===res.version) { // hit
console.log('comment cache hit '+pid);
console.log('comment cache hit',pid);
res.last_access=(+new Date());
store.put(res);
resolve(res.data);
} 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);
resolve(null);
}
@@ -52,6 +53,7 @@ class Cache {
}
put(pid,target_version,data) {
pid=parseInt(pid);
return new Promise((resolve,reject)=>{
if(!this.db)
return resolve();
@@ -68,6 +70,23 @@ class Cache {
});
}
delete(pid) {
pid=parseInt(pid);
return new Promise((resolve,reject)=>{
if(!this.db)
return resolve();
const tx=this.db.transaction(['comment'],'readwrite');
const store=tx.objectStore('comment');
let req=store.delete(pid);
//console.log('comment cache delete',pid);
req.onerror=()=>{
console.warn('comment cache delete failed ',pid);
return resolve();
};
req.onsuccess=()=>resolve();
});
}
maintenance() {
if(!this.db)
return;
@@ -88,7 +107,7 @@ class Cache {
}
};
} else {
console.log('comment cache db not full',count);
console.log('comment cache db no need to maintenance',count);
}
this.added_items_since_maintenance=0;
};

View File

@@ -31,7 +31,9 @@ export const API={
else throw new Error(JSON.stringify(json));
}
cache().delete(pid).then(()=>{
cache().put(pid,cache_version,json);
});
// also change load_replies_with_cache!
json.data=json.data
@@ -85,6 +87,7 @@ export const API={
})
.then(get_json)
.then((json)=>{
cache().delete(pid);
if(json.code!==0) {
if(json.msg && json.msg==='已经关注过了') {}
else {
@@ -143,7 +146,7 @@ export const API={
.then(get_json)
.then((json)=>{
if(json.code!==0) {
if(json.msg) alert(json.msg);
if(json.msg) throw new Error(json.msg);
throw new Error(JSON.stringify(json));
}
return json;
@@ -174,7 +177,7 @@ export const API={
.then(get_json)
.then((json)=>{
if(json.code!==0) {
if(json.msg) alert(json.msg);
if(json.msg) throw new Error(json.msg);
throw new Error(JSON.stringify(json));
}
return json;