Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Cache state service account blob from disk reads (#668)
Browse files Browse the repository at this point in the history
* store account state in mem cache

* use const
  • Loading branch information
kspearrin committed Feb 11, 2022
1 parent 0109b7b commit b094fa7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion common/src/services/state.service.ts
Expand Up @@ -69,13 +69,17 @@ export class StateService<

private hasBeenInited: boolean = false;

private accountDiskCache: Map<string, TAccount>;

constructor(
protected storageService: StorageService,
protected secureStorageService: StorageService,
protected logService: LogService,
protected stateMigrationService: StateMigrationService,
protected stateFactory: StateFactory<TGlobalState, TAccount>
) {}
) {
this.accountDiskCache = new Map<string, TAccount>();
}

async init(): Promise<void> {
if (this.hasBeenInited) {
Expand Down Expand Up @@ -2191,6 +2195,11 @@ export class StateService<
return null;
}

const cachedAccount = this.accountDiskCache.get(options.userId);
if (cachedAccount != null) {
return cachedAccount;
}

const account = options?.useSecureStorage
? (await this.secureStorageService.get<TAccount>(options.userId, options)) ??
(await this.storageService.get<TAccount>(
Expand All @@ -2199,6 +2208,7 @@ export class StateService<
))
: await this.storageService.get<TAccount>(options.userId, options);

this.accountDiskCache.set(options.userId, account);
return account;
}

Expand Down Expand Up @@ -2228,6 +2238,7 @@ export class StateService<
: this.storageService;

await storageLocation.save(`${options.userId}`, account, options);
this.accountDiskCache.delete(options.userId);
}

protected async saveAccountToMemory(account: TAccount): Promise<void> {
Expand Down

0 comments on commit b094fa7

Please sign in to comment.