Skip to content

Commit 8699353

Browse files
authored
perf: Cache customer accounts [DEV-4973] (#674)
perf: Cache customer accounts
1 parent da044cc commit 8699353

6 files changed

Lines changed: 62 additions & 28 deletions

File tree

src/controllers/admin/organisation.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {
1212
} from '../../types/admin.js';
1313
import { PaymentAccountService } from '../../services/api/payment-account.js';
1414
import { CheqdNetwork } from '@cheqd/sdk';
15+
import { LocalStore } from '../../database/cache/store.js';
16+
import { PaymentAccountEntity } from '../../database/entities/payment.account.entity.js';
1517

1618
dotenv.config();
1719

@@ -73,16 +75,23 @@ export class OrganisationController {
7375
email,
7476
description,
7577
});
76-
const paymentAccount = await PaymentAccountService.instance.find({ customer: customer });
78+
79+
const cachedAccounts = LocalStore.instance.getCustomerAccounts(response.locals.customer.customerId);
80+
let paymentAccounts: PaymentAccountEntity[];
81+
if (cachedAccounts?.length == 2) {
82+
paymentAccounts = cachedAccounts;
83+
} else {
84+
paymentAccounts = await PaymentAccountService.instance.find({ customer: response.locals.customer });
85+
}
7786

78-
if (!customer || paymentAccount.length === 0) {
87+
if (!customer || paymentAccounts.length === 0) {
7988
response.status(StatusCodes.NOT_FOUND).json({
8089
error: 'Customer for updating not found',
8190
} satisfies AdminOrganisationUpdateUnsuccessfulResponseBody);
8291
}
8392

84-
const testnetAddress = paymentAccount.find((acc) => acc.namespace === CheqdNetwork.Testnet)?.address;
85-
const mainnetAddress = paymentAccount.find((acc) => acc.namespace === CheqdNetwork.Mainnet)?.address;
93+
const testnetAddress = paymentAccounts.find((acc) => acc.namespace === CheqdNetwork.Testnet)?.address;
94+
const mainnetAddress = paymentAccounts.find((acc) => acc.namespace === CheqdNetwork.Mainnet)?.address;
8695

8796
return response.status(StatusCodes.OK).json({
8897
name: customer.name,

src/controllers/api/account.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import { SubscriptionService } from '../../services/admin/subscription.js';
3232
import { RoleService } from '../../services/api/role.js';
3333
import { getStripeObjectKey } from '../../utils/index.js';
3434
import { KeyService } from '../../services/api/key.js';
35+
import { LocalStore } from '../../database/cache/store.js';
36+
3537
dotenv.config();
3638

3739
export class AccountController {
@@ -76,7 +78,14 @@ export class AccountController {
7678
error: 'Bad state cause there is no customer assigned to the user yet. Please contact administrator.',
7779
} satisfies UnsuccessfulQueryCustomerResponseBody);
7880
}
79-
const paymentAccounts = await PaymentAccountService.instance.find({ customer: response.locals.customer });
81+
82+
const cachedAccounts = LocalStore.instance.getCustomerAccounts(response.locals.customer.customerId);
83+
let paymentAccounts: PaymentAccountEntity[];
84+
if (cachedAccounts?.length == 2) {
85+
paymentAccounts = cachedAccounts;
86+
} else {
87+
paymentAccounts = await PaymentAccountService.instance.find({ customer: response.locals.customer });
88+
}
8089
const result: QueryCustomerResponseBody = {
8190
customer: {
8291
customerId: response.locals.customer.customerId,

src/database/cache/store.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import NodeCache from 'node-cache';
2+
import * as dotenv from 'dotenv';
3+
4+
import { PaymentAccountEntity } from '../entities/payment.account.entity.js';
5+
6+
dotenv.config();
7+
8+
let { LOCAL_STORE_TTL = 600 } = process.env;
9+
10+
export class LocalStore {
11+
private cache: NodeCache;
12+
13+
public static instance = new LocalStore();
14+
15+
constructor() {
16+
this.cache = new NodeCache();
17+
}
18+
19+
setCustomerAccounts(key: string, data: PaymentAccountEntity[]) {
20+
this.cache.set(key, data, +LOCAL_STORE_TTL);
21+
}
22+
23+
getCustomerAccounts(key: string) {
24+
return this.cache.get(key) as PaymentAccountEntity[] | undefined;
25+
}
26+
}

src/services/api/store.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/services/identity/postgres.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { toTPublicKeyEd25519 } from '../helpers.js';
5858
import type { APIServiceOptions } from '../../types/admin.js';
5959
import { SupportedKeyTypes } from '@veramo/utils';
6060
import { PaymentAccountEntity } from '../../database/entities/payment.account.entity.js';
61+
import { LocalStore } from '../../database/cache/store.js';
6162

6263
dotenv.config();
6364

@@ -130,7 +131,17 @@ export class PostgresIdentityService extends DefaultIdentityService {
130131
}
131132
const dbConnection = Connection.instance.dbConnection;
132133

133-
const paymentAccounts = await PaymentAccountService.instance.find({ customer }, ['key']);
134+
const cachedAccounts = LocalStore.instance.getCustomerAccounts(customer.customerId);
135+
let paymentAccounts: PaymentAccountEntity[];
136+
if (cachedAccounts?.length == 2) {
137+
paymentAccounts = cachedAccounts;
138+
} else {
139+
paymentAccounts = await PaymentAccountService.instance.find({ customer }, ['key']);
140+
141+
if (paymentAccounts.length > 0) {
142+
LocalStore.instance.setCustomerAccounts(customer.customerId, paymentAccounts);
143+
}
144+
}
134145

135146
// One customer may / may not have one Mainnet paymentAccount
136147
const providerMainnet = await this.createCheqdProvider(customer, CheqdNetwork.Mainnet, paymentAccounts);

src/types/environment.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare global {
88
PORT: string;
99
NPM_CONFIG_LOGLEVEL: string;
1010
LOG_LEVEL: string | 'info';
11+
LOCAL_STORE_TTL: string | undefined;
1112

1213
// Network API endpoints
1314
MAINNET_RPC_URL: string;

0 commit comments

Comments
 (0)