import { PreferencesUtil } from '@pura/harmony-utils'; import Logger from './Logger'; import { NavidromeRestArtist, NavidromeRestAlbum, NavidromeRestPlaylist } from '../network/NavidromeRestApi'; import { VideoItem } from '../../viewmodel/VideoItem'; const TAG = 'heanup NavidromeListCache'; /** * 缓存信息接口 */ export interface CacheInfo { count: number; } /** * 所有缓存数据接口 */ export interface AllCacheData { songs: VideoItem[] | null; artists: NavidromeRestArtist[] | null; albums: NavidromeRestAlbum[] | null; playlists: NavidromeRestPlaylist[] | null; } /** * Navidrome列表缓存工具类 * 使用PreferencesUtil持久化缓存前80条数据 * 区分不同网盘账号的缓存 */ export class NavidromeListCache { private static instance: NavidromeListCache; private constructor() {} public static getInstance(): NavidromeListCache { if (!NavidromeListCache.instance) { NavidromeListCache.instance = new NavidromeListCache(); } return NavidromeListCache.instance; } /** * 生成缓存键名,包含账户ID * @param accountId 账户ID * @param dataType 数据类型 * @returns 缓存键名 */ private getCacheKey(accountId: string, dataType: string): string { return `navidrome_${accountId}_${dataType}`; } /** * 保存歌曲列表缓存(前80首) * @param accountId 账户ID * @param songs 歌曲列表 */ public saveSongsCache(accountId: string, songs: VideoItem[]): void { try { const limitedSongs = songs.slice(0, 80); const cacheKey = this.getCacheKey(accountId, 'songs'); PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedSongs)); Logger.info(TAG, `保存歌曲缓存成功: accountId=${accountId}, 数量=${limitedSongs.length}`); } catch (error) { Logger.error(TAG, `保存歌曲缓存失败: ${(error as Error).message}`); } } /** * 读取歌曲列表缓存 * @param accountId 账户ID * @returns 歌曲列表或null */ public getSongsCache(accountId: string): VideoItem[] | null { try { const cacheKey = this.getCacheKey(accountId, 'songs'); const jsonData = PreferencesUtil.getStringSync(cacheKey, ''); if (!jsonData || jsonData.length === 0) { return null; } const songs = JSON.parse(jsonData) as VideoItem[]; Logger.info(TAG, `读取歌曲缓存成功: accountId=${accountId}, 数量=${songs.length}`); return songs; } catch (error) { Logger.error(TAG, `读取歌曲缓存失败: ${(error as Error).message}`); return null; } } /** * 保存艺术家列表缓存(前80位) * @param accountId 账户ID * @param artists 艺术家列表 */ public saveArtistsCache(accountId: string, artists: NavidromeRestArtist[]): void { try { const limitedArtists = artists.slice(0, 80); const cacheKey = this.getCacheKey(accountId, 'artists'); PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedArtists)); Logger.info(TAG, `保存艺术家缓存成功: accountId=${accountId}, 数量=${limitedArtists.length}`); } catch (error) { Logger.error(TAG, `保存艺术家缓存失败: ${(error as Error).message}`); } } /** * 读取艺术家列表缓存 * @param accountId 账户ID * @returns 艺术家列表或null */ public getArtistsCache(accountId: string): NavidromeRestArtist[] | null { try { const cacheKey = this.getCacheKey(accountId, 'artists'); const jsonData = PreferencesUtil.getStringSync(cacheKey, ''); if (!jsonData || jsonData.length === 0) { return null; } const artists = JSON.parse(jsonData) as NavidromeRestArtist[]; Logger.info(TAG, `读取艺术家缓存成功: accountId=${accountId}, 数量=${artists.length}`); return artists; } catch (error) { Logger.error(TAG, `读取艺术家缓存失败: ${(error as Error).message}`); return null; } } /** * 保存专辑列表缓存(前80张) * @param accountId 账户ID * @param albums 专辑列表 */ public saveAlbumsCache(accountId: string, albums: NavidromeRestAlbum[]): void { try { const limitedAlbums = albums.slice(0, 80); const cacheKey = this.getCacheKey(accountId, 'albums'); PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedAlbums)); Logger.info(TAG, `保存专辑缓存成功: accountId=${accountId}, 数量=${limitedAlbums.length}`); } catch (error) { Logger.error(TAG, `保存专辑缓存失败: ${(error as Error).message}`); } } /** * 读取专辑列表缓存 * @param accountId 账户ID * @returns 专辑列表或null */ public getAlbumsCache(accountId: string): NavidromeRestAlbum[] | null { try { const cacheKey = this.getCacheKey(accountId, 'albums'); const jsonData = PreferencesUtil.getStringSync(cacheKey, ''); if (!jsonData || jsonData.length === 0) { return null; } const albums = JSON.parse(jsonData) as NavidromeRestAlbum[]; Logger.info(TAG, `读取专辑缓存成功: accountId=${accountId}, 数量=${albums.length}`); return albums; } catch (error) { Logger.error(TAG, `读取专辑缓存失败: ${(error as Error).message}`); return null; } } /** * 保存歌单列表缓存(前80个) * @param accountId 账户ID * @param playlists 歌单列表 */ public savePlaylistsCache(accountId: string, playlists: NavidromeRestPlaylist[]): void { try { const limitedPlaylists = playlists.slice(0, 80); const cacheKey = this.getCacheKey(accountId, 'playlists'); PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedPlaylists)); Logger.info(TAG, `保存歌单缓存成功: accountId=${accountId}, 数量=${limitedPlaylists.length}`); } catch (error) { Logger.error(TAG, `保存歌单缓存失败: ${(error as Error).message}`); } } /** * 读取歌单列表缓存 * @param accountId 账户ID * @returns 歌单列表或null */ public getPlaylistsCache(accountId: string): NavidromeRestPlaylist[] | null { try { const cacheKey = this.getCacheKey(accountId, 'playlists'); const jsonData = PreferencesUtil.getStringSync(cacheKey, ''); if (!jsonData || jsonData.length === 0) { return null; } const playlists = JSON.parse(jsonData) as NavidromeRestPlaylist[]; Logger.info(TAG, `读取歌单缓存成功: accountId=${accountId}, 数量=${playlists.length}`); return playlists; } catch (error) { Logger.error(TAG, `读取歌单缓存失败: ${(error as Error).message}`); return null; } } /** * 删除指定账户的所有缓存 * @param accountId 账户ID */ public clearAccountCache(accountId: string): void { try { const dataTypes = ['songs', 'artists', 'albums', 'playlists']; for (let i = 0; i < dataTypes.length; i++) { const dataType = dataTypes[i]; const cacheKey = this.getCacheKey(accountId, dataType); PreferencesUtil.deleteSync(cacheKey); } Logger.info(TAG, `清空账户缓存成功: accountId=${accountId}`); } catch (error) { Logger.error(TAG, `清空账户缓存失败: ${(error as Error).message}`); } } /** * 获取缓存信息 * @param accountId 账户ID * @returns 缓存信息Map */ public getCacheInfo(accountId: string): Map { const infoMap = new Map(); const dataTypes = ['songs', 'artists', 'albums', 'playlists']; for (let i = 0; i < dataTypes.length; i++) { const dataType = dataTypes[i]; const cacheKey = this.getCacheKey(accountId, dataType); try { const jsonData = PreferencesUtil.getStringSync(cacheKey, ''); if (jsonData && jsonData.length > 0) { const data: Record[] = JSON.parse(jsonData) as Record[]; const cacheInfo: CacheInfo = { count: data.length }; infoMap.set(dataType, cacheInfo); } } catch (error) { Logger.error(TAG, `获取缓存信息失败: ${dataType}, ${(error as Error).message}`); } } return infoMap; } /** * 批量保存所有缓存 * @param accountId 账户ID * @param songs 歌曲列表 * @param artists 艺术家列表 * @param albums 专辑列表 * @param playlists 歌单列表 */ public saveAllCache( accountId: string, songs: VideoItem[], artists: NavidromeRestArtist[], albums: NavidromeRestAlbum[], playlists: NavidromeRestPlaylist[] ): void { this.saveSongsCache(accountId, songs); this.saveArtistsCache(accountId, artists); this.saveAlbumsCache(accountId, albums); this.savePlaylistsCache(accountId, playlists); Logger.info(TAG, `批量保存缓存完成: accountId=${accountId}`); } /** * 批量读取所有缓存 * @param accountId 账户ID * @returns 缓存数据对象 */ public getAllCache(accountId: string): AllCacheData { return { songs: this.getSongsCache(accountId), artists: this.getArtistsCache(accountId), albums: this.getAlbumsCache(accountId), playlists: this.getPlaylistsCache(accountId) }; } } export default NavidromeListCache;