| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- 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<string, CacheInfo> {
- const infoMap = new Map<string, CacheInfo>();
- 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<string, Object>[] = JSON.parse(jsonData) as Record<string, Object>[];
- 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;
|