feat: 去除歌曲缓存 优化播放下一首

This commit is contained in:
alger
2025-01-17 22:35:42 +08:00
parent dfa175b8b2
commit 914e043502
3 changed files with 7 additions and 90 deletions

View File

@@ -1,5 +1,4 @@
import match from '@unblockneteasemusic/server';
import Store from 'electron-store';
type Platform = 'qq' | 'migu' | 'kugou' | 'pyncmd' | 'joox' | 'kuwo' | 'bilibili' | 'youtube';
@@ -28,73 +27,6 @@ interface UnblockResult {
};
}
interface CacheData extends UnblockResult {
timestamp: number;
}
interface CacheStore {
[key: string]: CacheData;
}
// 初始化缓存存储
const store = new Store<CacheStore>({
name: 'unblock-cache'
});
// 缓存过期时间24小时
const CACHE_EXPIRY = 24 * 60 * 60 * 1000;
/**
* 检查缓存是否有效
* @param cacheData 缓存数据
* @returns boolean
*/
const isCacheValid = (cacheData: CacheData | null): boolean => {
if (!cacheData) return false;
const now = Date.now();
return now - cacheData.timestamp < CACHE_EXPIRY;
};
/**
* 从缓存中获取数据
* @param id 歌曲ID
* @returns CacheData | null
*/
const getFromCache = (id: string | number): CacheData | null => {
const cacheData = store.get(String(id)) as CacheData | null;
if (isCacheValid(cacheData)) {
return cacheData;
}
// 清除过期缓存
store.delete(String(id));
return null;
};
/**
* 将数据存入缓存
* @param id 歌曲ID
* @param data 解析结果
*/
const saveToCache = (id: string | number, data: UnblockResult): void => {
const cacheData: CacheData = {
...data,
timestamp: Date.now()
};
store.set(String(id), cacheData);
};
/**
* 清理过期缓存
*/
const cleanExpiredCache = (): void => {
const allData = store.store;
Object.entries(allData).forEach(([id, data]) => {
if (!isCacheValid(data)) {
store.delete(id);
}
});
};
/**
* 音乐解析函数
* @param id 歌曲ID
@@ -107,12 +39,6 @@ const unblockMusic = async (
songData: SongData,
retryCount = 3
): Promise<UnblockResult> => {
// 检查缓存
const cachedData = getFromCache(id);
if (cachedData) {
return cachedData;
}
// 所有可用平台
const platforms: Platform[] = ['migu', 'kugou', 'pyncmd', 'joox', 'kuwo', 'bilibili', 'youtube'];
@@ -128,9 +54,6 @@ const unblockMusic = async (
}
}
};
// 保存到缓存
saveToCache(id, result);
return result;
} catch (err) {
if (attempt < retryCount) {
@@ -149,14 +72,4 @@ const unblockMusic = async (
return retry(1);
};
// 定期清理过期缓存(每小时执行一次)
setInterval(cleanExpiredCache, 60 * 60 * 1000);
export {
cleanExpiredCache, // 导出清理缓存函数,以便手动调用
type Platform,
type ResponseData,
type SongData,
unblockMusic,
type UnblockResult
};
export { type Platform, type ResponseData, type SongData, unblockMusic, type UnblockResult };

View File

@@ -6,7 +6,7 @@ import store from '@/store';
import type { Artist, ILyricText, SongResult } from '@/type/music';
import { isElectron } from '@/utils';
import { getTextColors } from '@/utils/linearColor';
import { createDiscreteApi } from 'naive-ui';
const windowData = window as any;
export const lrcArray = ref<ILyricText[]>([]); // 歌词数组
@@ -51,6 +51,8 @@ document.onkeyup = (e) => {
}
};
const { message } = createDiscreteApi(['message']);
watch(
() => store.state.playMusicUrl,
async (newVal) => {
@@ -60,8 +62,10 @@ watch(
sound.value = newSound as Howl;
setupAudioListeners();
} catch (error) {
console.error('播放音频失败:', error);
store.commit('setPlayMusic', false);
message.error('当前歌曲播放失败,播放下一首');
// 下一首
store.commit('nextPlay');
}

View File

@@ -128,7 +128,7 @@ class AudioService {
}
return new Promise((resolve, reject) => {
let retryCount = 0;
const maxRetries = 3;
const maxRetries = 1;
const tryPlay = () => {
if (this.currentSound) {