1
0
mirror of https://github.com/bitwarden/browser synced 2024-12-27 10:23:48 +01:00

Cache state service account blob from disk reads (#668)

* store account state in mem cache

* use const
This commit is contained in:
Kyle Spearrin 2022-02-11 00:20:50 -05:00 committed by GitHub
parent b7d9a532cb
commit cda649fa21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -69,13 +69,17 @@ export class StateService<
private hasBeenInited: boolean = false; private hasBeenInited: boolean = false;
private accountDiskCache: Map<string, TAccount>;
constructor( constructor(
protected storageService: StorageService, protected storageService: StorageService,
protected secureStorageService: StorageService, protected secureStorageService: StorageService,
protected logService: LogService, protected logService: LogService,
protected stateMigrationService: StateMigrationService, protected stateMigrationService: StateMigrationService,
protected stateFactory: StateFactory<TGlobalState, TAccount> protected stateFactory: StateFactory<TGlobalState, TAccount>
) {} ) {
this.accountDiskCache = new Map<string, TAccount>();
}
async init(): Promise<void> { async init(): Promise<void> {
if (this.hasBeenInited) { if (this.hasBeenInited) {
@ -2191,6 +2195,11 @@ export class StateService<
return null; return null;
} }
const cachedAccount = this.accountDiskCache.get(options.userId);
if (cachedAccount != null) {
return cachedAccount;
}
const account = options?.useSecureStorage const account = options?.useSecureStorage
? (await this.secureStorageService.get<TAccount>(options.userId, options)) ?? ? (await this.secureStorageService.get<TAccount>(options.userId, options)) ??
(await this.storageService.get<TAccount>( (await this.storageService.get<TAccount>(
@ -2199,6 +2208,7 @@ export class StateService<
)) ))
: await this.storageService.get<TAccount>(options.userId, options); : await this.storageService.get<TAccount>(options.userId, options);
this.accountDiskCache.set(options.userId, account);
return account; return account;
} }
@ -2228,6 +2238,7 @@ export class StateService<
: this.storageService; : this.storageService;
await storageLocation.save(`${options.userId}`, account, options); await storageLocation.save(`${options.userId}`, account, options);
this.accountDiskCache.delete(options.userId);
} }
protected async saveAccountToMemory(account: TAccount): Promise<void> { protected async saveAccountToMemory(account: TAccount): Promise<void> {