From a84b65976a0eff0b5e5169572b7561d35eaf2351 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Mon, 1 Jan 2024 16:59:38 +0530 Subject: [PATCH] feat: cache interface add inmemory cache in getTransactions --- src/common/lib/cache.ts | 17 +++++ .../background-script/connectors/alby.ts | 68 ++++++++++++------- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/common/lib/cache.ts b/src/common/lib/cache.ts index d2e5aafe42..031159108e 100644 --- a/src/common/lib/cache.ts +++ b/src/common/lib/cache.ts @@ -24,3 +24,20 @@ export const removeAccountFromCache = async (id: string) => { } return accountsCache; }; + +export interface CacheProvider { + get(key: string): unknown | undefined; + set(key: string, value: object): void; +} + +export class InMemoryCache implements CacheProvider { + private _cache = new Map(); + + get(key: string): unknown | undefined { + return this._cache.get(key); + } + + set(key: string, value: object): void { + this._cache.set(key, value); + } +} diff --git a/src/extension/background-script/connectors/alby.ts b/src/extension/background-script/connectors/alby.ts index fc6c5335d5..1c2cd05633 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -3,12 +3,12 @@ import { CreateSwapParams, CreateSwapResponse, GetAccountInformationResponse, - Invoice, RequestOptions, SwapInfoResponse, Token, } from "@getalby/sdk/dist/types"; import browser from "webextension-polyfill"; +import { InMemoryCache } from "~/common/lib/cache"; import { decryptData, encryptData } from "~/common/lib/crypto"; import { Account, OAuthToken } from "~/types"; import state from "../state"; @@ -42,11 +42,12 @@ export default class Alby implements Connector { private config: Config; private _client: Client | undefined; private _authUser: auth.OAuth2User | undefined; - private _cache = new Map(); + private _cache: InMemoryCache; constructor(account: Account, config: Config) { this.account = account; this.config = config; + this._cache = new InMemoryCache(); } async init() { @@ -90,31 +91,48 @@ export default class Alby implements Connector { } async getTransactions(): Promise { - const invoicesResponse = (await this._request((client) => - client.invoices({}) - )) as Invoice[]; - - const transactions: ConnectorTransaction[] = invoicesResponse.map( - (invoice, index): ConnectorTransaction => ({ - custom_records: invoice.custom_records, - id: `${invoice.payment_request}-${index}`, - memo: invoice.comment || invoice.memo, - preimage: invoice.preimage ?? "", - payment_hash: invoice.payment_hash, - settled: invoice.settled, - settleDate: new Date(invoice.settled_at).getTime(), - totalAmount: invoice.amount, - type: invoice.type == "incoming" ? "received" : "sent", - }) - ); + const cacheKey = "getTransactions"; + const cacheValue = this._cache.get( + "getTransactions" + ) as GetTransactionsResponse; - return { - data: { - transactions, - }, - }; - } + if (cacheValue) { + return cacheValue; + } + + try { + const invoicesResponse = await this._request((client) => + client.invoices({}) + ); + const transactions: ConnectorTransaction[] = invoicesResponse.map( + (invoice, index): ConnectorTransaction => ({ + custom_records: invoice.custom_records, + id: `${invoice.payment_request}-${index}`, + memo: invoice.comment || invoice.memo, + preimage: invoice.preimage ?? "", + payment_hash: invoice.payment_hash, + settled: invoice.settled, + settleDate: new Date(invoice.settled_at).getTime(), + totalAmount: invoice.amount, + type: invoice.type == "incoming" ? "received" : "sent", + }) + ); + + const result: GetTransactionsResponse = { + data: { + transactions, + }, + }; + + this._cache.set(cacheKey, result); + + return result; + } catch (error) { + console.error(error); + throw error; + } + } async getInfo(): Promise< GetInfoResponse > {