NavidromeListCache.ets 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { PreferencesUtil } from '@pura/harmony-utils';
  2. import Logger from './Logger';
  3. import { NavidromeRestArtist, NavidromeRestAlbum, NavidromeRestPlaylist } from '../network/NavidromeRestApi';
  4. import { VideoItem } from '../../viewmodel/VideoItem';
  5. const TAG = 'heanup NavidromeListCache';
  6. /**
  7. * 缓存信息接口
  8. */
  9. export interface CacheInfo {
  10. count: number;
  11. }
  12. /**
  13. * 所有缓存数据接口
  14. */
  15. export interface AllCacheData {
  16. songs: VideoItem[] | null;
  17. artists: NavidromeRestArtist[] | null;
  18. albums: NavidromeRestAlbum[] | null;
  19. playlists: NavidromeRestPlaylist[] | null;
  20. }
  21. /**
  22. * Navidrome列表缓存工具类
  23. * 使用PreferencesUtil持久化缓存前80条数据
  24. * 区分不同网盘账号的缓存
  25. */
  26. export class NavidromeListCache {
  27. private static instance: NavidromeListCache;
  28. private constructor() {}
  29. public static getInstance(): NavidromeListCache {
  30. if (!NavidromeListCache.instance) {
  31. NavidromeListCache.instance = new NavidromeListCache();
  32. }
  33. return NavidromeListCache.instance;
  34. }
  35. /**
  36. * 生成缓存键名,包含账户ID
  37. * @param accountId 账户ID
  38. * @param dataType 数据类型
  39. * @returns 缓存键名
  40. */
  41. private getCacheKey(accountId: string, dataType: string): string {
  42. return `navidrome_${accountId}_${dataType}`;
  43. }
  44. /**
  45. * 保存歌曲列表缓存(前80首)
  46. * @param accountId 账户ID
  47. * @param songs 歌曲列表
  48. */
  49. public saveSongsCache(accountId: string, songs: VideoItem[]): void {
  50. try {
  51. const limitedSongs = songs.slice(0, 80);
  52. const cacheKey = this.getCacheKey(accountId, 'songs');
  53. PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedSongs));
  54. Logger.info(TAG, `保存歌曲缓存成功: accountId=${accountId}, 数量=${limitedSongs.length}`);
  55. } catch (error) {
  56. Logger.error(TAG, `保存歌曲缓存失败: ${(error as Error).message}`);
  57. }
  58. }
  59. /**
  60. * 读取歌曲列表缓存
  61. * @param accountId 账户ID
  62. * @returns 歌曲列表或null
  63. */
  64. public getSongsCache(accountId: string): VideoItem[] | null {
  65. try {
  66. const cacheKey = this.getCacheKey(accountId, 'songs');
  67. const jsonData = PreferencesUtil.getStringSync(cacheKey, '');
  68. if (!jsonData || jsonData.length === 0) {
  69. return null;
  70. }
  71. const songs = JSON.parse(jsonData) as VideoItem[];
  72. Logger.info(TAG, `读取歌曲缓存成功: accountId=${accountId}, 数量=${songs.length}`);
  73. return songs;
  74. } catch (error) {
  75. Logger.error(TAG, `读取歌曲缓存失败: ${(error as Error).message}`);
  76. return null;
  77. }
  78. }
  79. /**
  80. * 保存艺术家列表缓存(前80位)
  81. * @param accountId 账户ID
  82. * @param artists 艺术家列表
  83. */
  84. public saveArtistsCache(accountId: string, artists: NavidromeRestArtist[]): void {
  85. try {
  86. const limitedArtists = artists.slice(0, 80);
  87. const cacheKey = this.getCacheKey(accountId, 'artists');
  88. PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedArtists));
  89. Logger.info(TAG, `保存艺术家缓存成功: accountId=${accountId}, 数量=${limitedArtists.length}`);
  90. } catch (error) {
  91. Logger.error(TAG, `保存艺术家缓存失败: ${(error as Error).message}`);
  92. }
  93. }
  94. /**
  95. * 读取艺术家列表缓存
  96. * @param accountId 账户ID
  97. * @returns 艺术家列表或null
  98. */
  99. public getArtistsCache(accountId: string): NavidromeRestArtist[] | null {
  100. try {
  101. const cacheKey = this.getCacheKey(accountId, 'artists');
  102. const jsonData = PreferencesUtil.getStringSync(cacheKey, '');
  103. if (!jsonData || jsonData.length === 0) {
  104. return null;
  105. }
  106. const artists = JSON.parse(jsonData) as NavidromeRestArtist[];
  107. Logger.info(TAG, `读取艺术家缓存成功: accountId=${accountId}, 数量=${artists.length}`);
  108. return artists;
  109. } catch (error) {
  110. Logger.error(TAG, `读取艺术家缓存失败: ${(error as Error).message}`);
  111. return null;
  112. }
  113. }
  114. /**
  115. * 保存专辑列表缓存(前80张)
  116. * @param accountId 账户ID
  117. * @param albums 专辑列表
  118. */
  119. public saveAlbumsCache(accountId: string, albums: NavidromeRestAlbum[]): void {
  120. try {
  121. const limitedAlbums = albums.slice(0, 80);
  122. const cacheKey = this.getCacheKey(accountId, 'albums');
  123. PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedAlbums));
  124. Logger.info(TAG, `保存专辑缓存成功: accountId=${accountId}, 数量=${limitedAlbums.length}`);
  125. } catch (error) {
  126. Logger.error(TAG, `保存专辑缓存失败: ${(error as Error).message}`);
  127. }
  128. }
  129. /**
  130. * 读取专辑列表缓存
  131. * @param accountId 账户ID
  132. * @returns 专辑列表或null
  133. */
  134. public getAlbumsCache(accountId: string): NavidromeRestAlbum[] | null {
  135. try {
  136. const cacheKey = this.getCacheKey(accountId, 'albums');
  137. const jsonData = PreferencesUtil.getStringSync(cacheKey, '');
  138. if (!jsonData || jsonData.length === 0) {
  139. return null;
  140. }
  141. const albums = JSON.parse(jsonData) as NavidromeRestAlbum[];
  142. Logger.info(TAG, `读取专辑缓存成功: accountId=${accountId}, 数量=${albums.length}`);
  143. return albums;
  144. } catch (error) {
  145. Logger.error(TAG, `读取专辑缓存失败: ${(error as Error).message}`);
  146. return null;
  147. }
  148. }
  149. /**
  150. * 保存歌单列表缓存(前80个)
  151. * @param accountId 账户ID
  152. * @param playlists 歌单列表
  153. */
  154. public savePlaylistsCache(accountId: string, playlists: NavidromeRestPlaylist[]): void {
  155. try {
  156. const limitedPlaylists = playlists.slice(0, 80);
  157. const cacheKey = this.getCacheKey(accountId, 'playlists');
  158. PreferencesUtil.putSync(cacheKey, JSON.stringify(limitedPlaylists));
  159. Logger.info(TAG, `保存歌单缓存成功: accountId=${accountId}, 数量=${limitedPlaylists.length}`);
  160. } catch (error) {
  161. Logger.error(TAG, `保存歌单缓存失败: ${(error as Error).message}`);
  162. }
  163. }
  164. /**
  165. * 读取歌单列表缓存
  166. * @param accountId 账户ID
  167. * @returns 歌单列表或null
  168. */
  169. public getPlaylistsCache(accountId: string): NavidromeRestPlaylist[] | null {
  170. try {
  171. const cacheKey = this.getCacheKey(accountId, 'playlists');
  172. const jsonData = PreferencesUtil.getStringSync(cacheKey, '');
  173. if (!jsonData || jsonData.length === 0) {
  174. return null;
  175. }
  176. const playlists = JSON.parse(jsonData) as NavidromeRestPlaylist[];
  177. Logger.info(TAG, `读取歌单缓存成功: accountId=${accountId}, 数量=${playlists.length}`);
  178. return playlists;
  179. } catch (error) {
  180. Logger.error(TAG, `读取歌单缓存失败: ${(error as Error).message}`);
  181. return null;
  182. }
  183. }
  184. /**
  185. * 删除指定账户的所有缓存
  186. * @param accountId 账户ID
  187. */
  188. public clearAccountCache(accountId: string): void {
  189. try {
  190. const dataTypes = ['songs', 'artists', 'albums', 'playlists'];
  191. for (let i = 0; i < dataTypes.length; i++) {
  192. const dataType = dataTypes[i];
  193. const cacheKey = this.getCacheKey(accountId, dataType);
  194. PreferencesUtil.deleteSync(cacheKey);
  195. }
  196. Logger.info(TAG, `清空账户缓存成功: accountId=${accountId}`);
  197. } catch (error) {
  198. Logger.error(TAG, `清空账户缓存失败: ${(error as Error).message}`);
  199. }
  200. }
  201. /**
  202. * 获取缓存信息
  203. * @param accountId 账户ID
  204. * @returns 缓存信息Map
  205. */
  206. public getCacheInfo(accountId: string): Map<string, CacheInfo> {
  207. const infoMap = new Map<string, CacheInfo>();
  208. const dataTypes = ['songs', 'artists', 'albums', 'playlists'];
  209. for (let i = 0; i < dataTypes.length; i++) {
  210. const dataType = dataTypes[i];
  211. const cacheKey = this.getCacheKey(accountId, dataType);
  212. try {
  213. const jsonData = PreferencesUtil.getStringSync(cacheKey, '');
  214. if (jsonData && jsonData.length > 0) {
  215. const data: Record<string, Object>[] = JSON.parse(jsonData) as Record<string, Object>[];
  216. const cacheInfo: CacheInfo = {
  217. count: data.length
  218. };
  219. infoMap.set(dataType, cacheInfo);
  220. }
  221. } catch (error) {
  222. Logger.error(TAG, `获取缓存信息失败: ${dataType}, ${(error as Error).message}`);
  223. }
  224. }
  225. return infoMap;
  226. }
  227. /**
  228. * 批量保存所有缓存
  229. * @param accountId 账户ID
  230. * @param songs 歌曲列表
  231. * @param artists 艺术家列表
  232. * @param albums 专辑列表
  233. * @param playlists 歌单列表
  234. */
  235. public saveAllCache(
  236. accountId: string,
  237. songs: VideoItem[],
  238. artists: NavidromeRestArtist[],
  239. albums: NavidromeRestAlbum[],
  240. playlists: NavidromeRestPlaylist[]
  241. ): void {
  242. this.saveSongsCache(accountId, songs);
  243. this.saveArtistsCache(accountId, artists);
  244. this.saveAlbumsCache(accountId, albums);
  245. this.savePlaylistsCache(accountId, playlists);
  246. Logger.info(TAG, `批量保存缓存完成: accountId=${accountId}`);
  247. }
  248. /**
  249. * 批量读取所有缓存
  250. * @param accountId 账户ID
  251. * @returns 缓存数据对象
  252. */
  253. public getAllCache(accountId: string): AllCacheData {
  254. return {
  255. songs: this.getSongsCache(accountId),
  256. artists: this.getArtistsCache(accountId),
  257. albums: this.getAlbumsCache(accountId),
  258. playlists: this.getPlaylistsCache(accountId)
  259. };
  260. }
  261. }
  262. export default NavidromeListCache;