使用 hash 优化用户信息获取速度

This commit is contained in:
shinya
2026-02-27 19:57:14 +08:00
parent 3a201c7546
commit 13f1fb7166
6 changed files with 252 additions and 105 deletions

View File

@@ -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,