mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-03-20 15:57:29 +08:00
使用 hash 优化用户信息获取速度
This commit is contained in:
@@ -48,9 +48,24 @@ export function generateStorageKey(source: string, id: string): string {
|
||||
// 导出便捷方法
|
||||
export class DbManager {
|
||||
private storage: IStorage;
|
||||
private migrationPromise: Promise<void> | null = null;
|
||||
|
||||
constructor() {
|
||||
this.storage = getStorage();
|
||||
// 启动时自动触发数据迁移(异步,不阻塞构造)
|
||||
if (this.storage && typeof this.storage.migrateData === 'function') {
|
||||
this.migrationPromise = this.storage.migrateData().catch((err) => {
|
||||
console.error('数据迁移异常:', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** 等待迁移完成(内部方法,首次调用后 migrationPromise 会被置空) */
|
||||
private async ensureMigrated(): Promise<void> {
|
||||
if (this.migrationPromise) {
|
||||
await this.migrationPromise;
|
||||
this.migrationPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 播放记录相关方法
|
||||
@@ -76,6 +91,7 @@ export class DbManager {
|
||||
async getAllPlayRecords(userName: string): Promise<{
|
||||
[key: string]: PlayRecord;
|
||||
}> {
|
||||
await this.ensureMigrated();
|
||||
return this.storage.getAllPlayRecords(userName);
|
||||
}
|
||||
|
||||
@@ -88,6 +104,10 @@ export class DbManager {
|
||||
await this.storage.deletePlayRecord(userName, key);
|
||||
}
|
||||
|
||||
async deleteAllPlayRecords(userName: string): Promise<void> {
|
||||
await this.storage.deleteAllPlayRecords(userName);
|
||||
}
|
||||
|
||||
// 收藏相关方法
|
||||
async getFavorite(
|
||||
userName: string,
|
||||
@@ -111,6 +131,7 @@ export class DbManager {
|
||||
async getAllFavorites(
|
||||
userName: string
|
||||
): Promise<{ [key: string]: Favorite }> {
|
||||
await this.ensureMigrated();
|
||||
return this.storage.getAllFavorites(userName);
|
||||
}
|
||||
|
||||
@@ -123,6 +144,10 @@ export class DbManager {
|
||||
await this.storage.deleteFavorite(userName, key);
|
||||
}
|
||||
|
||||
async deleteAllFavorites(userName: string): Promise<void> {
|
||||
await this.storage.deleteAllFavorites(userName);
|
||||
}
|
||||
|
||||
async isFavorited(
|
||||
userName: string,
|
||||
source: string,
|
||||
|
||||
Reference in New Issue
Block a user