Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cache interface #2977

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/common/lib/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>();

get(key: string): unknown | undefined {
return this._cache.get(key);
}

set(key: string, value: object): void {
this._cache.set(key, value);
}
}
68 changes: 43 additions & 25 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string, object>();
private _cache: InMemoryCache;

constructor(account: Account, config: Config) {
this.account = account;
this.config = config;
this._cache = new InMemoryCache();
}

async init() {
Expand Down Expand Up @@ -90,31 +91,48 @@ export default class Alby implements Connector {
}

async getTransactions(): Promise<GetTransactionsResponse> {
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<WebLNNode & GetAccountInformationResponse>
> {
Expand Down
Loading