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

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
+12
View File
@@ -9,6 +9,7 @@ import { Suspense, useEffect, useRef, useState } from 'react';
import { getAuthInfoFromBrowserCookie } from '@/lib/auth';
import {
convertDanmakuFormat,
clearDanmakuCacheByTitle,
getDanmakuById,
getDanmakuFromCache,
getEpisodes,
@@ -4255,6 +4256,17 @@ function PlayPageClient() {
// 尝试跳转到当前正在播放的集数
let targetIndex = currentEpisodeIndex;
// 如果新源的集数跟旧源的集数不一致,清除当前剧集的所有弹幕缓存
const oldEpisodeCount = detail?.episodes?.length || 0;
const newEpisodeCount = newDetail.episodes?.length || 0;
if (oldEpisodeCount > 0 && newEpisodeCount > 0 && oldEpisodeCount !== newEpisodeCount) {
const titleForCache = detail?.title || videoTitle;
console.log(`换源集数不一致 (${oldEpisodeCount} -> ${newEpisodeCount}),清除弹幕缓存: ${titleForCache}`);
clearDanmakuCacheByTitle(titleForCache).catch((err) => {
console.error('清除弹幕缓存失败:', err);
});
}
// 如果当前集数超出新源的范围,则跳转到第一集
if (!newDetail.episodes || targetIndex >= newDetail.episodes.length) {
targetIndex = 0;
+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 {