换源集数不一致时清空弹幕缓存

This commit is contained in:
mtvpls
2026-04-14 23:28:03 +08:00
parent 5fd8b4d595
commit 91d3fe7da0
3 changed files with 59 additions and 0 deletions
+2
View File
@@ -2,6 +2,7 @@
import {
clearAllDanmakuCache,
clearDanmakuCache,
clearDanmakuCacheByTitle,
clearExpiredDanmakuCache,
generateCacheKey,
getDanmakuCacheStats,
@@ -43,6 +44,7 @@ export function initDanmakuModule(): void {
export {
clearAllDanmakuCache,
clearDanmakuCache,
clearDanmakuCacheByTitle,
clearExpiredDanmakuCache,
generateCacheKey,
getDanmakuCacheStats,
+45
View File
@@ -276,6 +276,51 @@ export async function clearDanmakuCache(title: string, episodeIndex: number): Pr
}
}
// 清除指定标题的所有弹幕缓存(所有集数)
export async function clearDanmakuCacheByTitle(title: string): Promise<number> {
try {
const db = await openDB();
const transaction = db.transaction([STORE_NAME], 'readwrite');
const objectStore = transaction.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
const request = objectStore.openCursor();
let deletedCount = 0;
request.onsuccess = (event) => {
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
if (cursor) {
const data = cursor.value as DanmakuCacheData;
if (data.title === title) {
cursor.delete();
deletedCount++;
}
cursor.continue();
} else {
if (deletedCount > 0) {
console.log(`已清除标题"${title}"的 ${deletedCount} 个弹幕缓存`);
}
resolve(deletedCount);
}
};
request.onerror = () => {
reject(new Error('清除弹幕缓存失败'));
};
transaction.oncomplete = () => {
db.close();
};
});
} catch (error) {
console.error('清除弹幕缓存失败:', error);
return 0;
}
}
// 清除所有过期缓存
export async function clearExpiredDanmakuCache(): Promise<number> {
try {