From d10b46bffcef1599ba59538c69cc11499a852b4d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 6 Mar 2024 10:31:52 +0100 Subject: wallet-core: actually cache denominations --- packages/taler-wallet-core/src/wallet.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'packages/taler-wallet-core') diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index b9d277cf0..8c9eee009 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -354,13 +354,15 @@ export async function getDenomInfo( denomPubHash: string, ): Promise { const key = `${exchangeBaseUrl}:${denomPubHash}`; - const cached = wex.ws.denomCache[key]; + const cached = wex.ws.lookupDenomCache(key); if (cached) { return cached; } const d = await tx.denominations.get([exchangeBaseUrl, denomPubHash]); if (d) { - return DenominationRecord.toDenomInfo(d); + const denomInfo = DenominationRecord.toDenomInfo(d); + wex.ws.putDenomCache(key, denomInfo); + return denomInfo; } return undefined; } @@ -1647,8 +1649,7 @@ export class InternalWalletState { initCalled = false; - // FIXME: Use an LRU cache here. - denomCache: Record = {}; + private denomCache: Map = new Map(); /** * Promises that are waiting for a particular resource. @@ -1675,6 +1676,17 @@ export class InternalWalletState { return this._db; } + lookupDenomCache(denomCacheKey: string): DenominationInfo | undefined { + return this.denomCache.get(denomCacheKey); + } + + putDenomCache(denomCacheKey: string, denomInfo: DenominationInfo): void { + if (this.denomCache.size > 1000) { + this.denomCache.clear(); + } + this.denomCache.set(denomCacheKey, denomInfo); + } + initWithConfig(newConfig: WalletRunConfig): void { if (this._config) { throw Error("config already set"); -- cgit v1.2.3