forked from newthuhole/hole_thu_frontend
fix comment cache not expired
This commit is contained in:
25
src/cache.js
25
src/cache.js
@@ -25,6 +25,7 @@ class Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(pid,target_version) {
|
get(pid,target_version) {
|
||||||
|
pid=parseInt(pid);
|
||||||
return new Promise((resolve,reject)=>{
|
return new Promise((resolve,reject)=>{
|
||||||
if(!this.db)
|
if(!this.db)
|
||||||
return resolve(null);
|
return resolve(null);
|
||||||
@@ -37,12 +38,12 @@ class Cache {
|
|||||||
//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);
|
resolve(res.data);
|
||||||
} 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);
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
@@ -52,6 +53,7 @@ class Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
put(pid,target_version,data) {
|
put(pid,target_version,data) {
|
||||||
|
pid=parseInt(pid);
|
||||||
return new Promise((resolve,reject)=>{
|
return new Promise((resolve,reject)=>{
|
||||||
if(!this.db)
|
if(!this.db)
|
||||||
return resolve();
|
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() {
|
maintenance() {
|
||||||
if(!this.db)
|
if(!this.db)
|
||||||
return;
|
return;
|
||||||
@@ -88,7 +107,7 @@ class Cache {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} 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;
|
this.added_items_since_maintenance=0;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ export const API={
|
|||||||
else throw new Error(JSON.stringify(json));
|
else throw new Error(JSON.stringify(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
cache().put(pid,cache_version,json);
|
cache().delete(pid).then(()=>{
|
||||||
|
cache().put(pid,cache_version,json);
|
||||||
|
});
|
||||||
|
|
||||||
// also change load_replies_with_cache!
|
// also change load_replies_with_cache!
|
||||||
json.data=json.data
|
json.data=json.data
|
||||||
@@ -85,6 +87,7 @@ export const API={
|
|||||||
})
|
})
|
||||||
.then(get_json)
|
.then(get_json)
|
||||||
.then((json)=>{
|
.then((json)=>{
|
||||||
|
cache().delete(pid);
|
||||||
if(json.code!==0) {
|
if(json.code!==0) {
|
||||||
if(json.msg && json.msg==='已经关注过了') {}
|
if(json.msg && json.msg==='已经关注过了') {}
|
||||||
else {
|
else {
|
||||||
@@ -143,7 +146,7 @@ export const API={
|
|||||||
.then(get_json)
|
.then(get_json)
|
||||||
.then((json)=>{
|
.then((json)=>{
|
||||||
if(json.code!==0) {
|
if(json.code!==0) {
|
||||||
if(json.msg) alert(json.msg);
|
if(json.msg) throw new Error(json.msg);
|
||||||
throw new Error(JSON.stringify(json));
|
throw new Error(JSON.stringify(json));
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
@@ -174,7 +177,7 @@ export const API={
|
|||||||
.then(get_json)
|
.then(get_json)
|
||||||
.then((json)=>{
|
.then((json)=>{
|
||||||
if(json.code!==0) {
|
if(json.code!==0) {
|
||||||
if(json.msg) alert(json.msg);
|
if(json.msg) throw new Error(json.msg);
|
||||||
throw new Error(JSON.stringify(json));
|
throw new Error(JSON.stringify(json));
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
|
|||||||
Reference in New Issue
Block a user