diff options
author | Florian Dold <florian@dold.me> | 2024-03-06 10:31:52 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-03-06 10:31:52 +0100 |
commit | d10b46bffcef1599ba59538c69cc11499a852b4d (patch) | |
tree | e88a05e8363d8ddd9c81c838f2b0a25bde88f547 /packages | |
parent | 794a9dc01185ffa5f6d3cf22d1041b4e8f468f48 (diff) | |
download | wallet-core-d10b46bffcef1599ba59538c69cc11499a852b4d.tar.xz |
wallet-core: actually cache denominations
Diffstat (limited to 'packages')
-rw-r--r-- | packages/taler-wallet-core/src/wallet.ts | 20 |
1 files changed, 16 insertions, 4 deletions
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<DenominationInfo | undefined> { 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<string, DenominationInfo> = {}; + private denomCache: Map<string, DenominationInfo> = 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"); |