From cda649fa21ef7f77d63b7582aab4e8dcca448c6b Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 11 Feb 2022 00:20:50 -0500 Subject: [PATCH] Cache state service account blob from disk reads (#668) * store account state in mem cache * use const --- common/src/services/state.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/common/src/services/state.service.ts b/common/src/services/state.service.ts index 376f006336..3bb26db833 100644 --- a/common/src/services/state.service.ts +++ b/common/src/services/state.service.ts @@ -69,13 +69,17 @@ export class StateService< private hasBeenInited: boolean = false; + private accountDiskCache: Map; + constructor( protected storageService: StorageService, protected secureStorageService: StorageService, protected logService: LogService, protected stateMigrationService: StateMigrationService, protected stateFactory: StateFactory - ) {} + ) { + this.accountDiskCache = new Map(); + } async init(): Promise { if (this.hasBeenInited) { @@ -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(options.userId, options)) ?? (await this.storageService.get( @@ -2199,6 +2208,7 @@ export class StateService< )) : await this.storageService.get(options.userId, options); + this.accountDiskCache.set(options.userId, account); return account; } @@ -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 {