clear expired and invalid cache
This commit is contained in:
1 parent
0377f075be
commit
e8b6cd1588
1 file changed
+7
@@ -284,12 +284,14 @@ export class InvidiousApi {
|
|||||||
try {
|
try {
|
||||||
const cacheData = JSON.parse(cache);
|
const cacheData = JSON.parse(cache);
|
||||||
if (cacheData.timestamp + cacheSeconds * 1000 < Date.now()) {
|
if (cacheData.timestamp + cacheSeconds * 1000 < Date.now()) {
|
||||||
|
this.deleteCache(url);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
console.log(`Cache hit for ${url}`);
|
console.log(`Cache hit for ${url}`);
|
||||||
return cacheData.data;
|
return cacheData.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
this.deleteCache(url);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,6 +307,11 @@ export class InvidiousApi {
|
|||||||
localStorage.setItem(cacheKey, JSON.stringify(cacheData));
|
localStorage.setItem(cacheKey, JSON.stringify(cacheData));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private deleteCache(url: string) {
|
||||||
|
const cacheKey = this.getCacheKey(url);
|
||||||
|
localStorage.removeItem(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
private getCacheKey(url: string) {
|
private getCacheKey(url: string) {
|
||||||
return `v1:${url}`;
|
return `v1:${url}`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user