Skip to content

Commit 873c0ef

Browse files
authored
fix: use LRUMap for all in-memory caches (#734)
* fix: use LRUMap for all in-memory caches * fix import * undo test changes
1 parent ddf7bbb commit 873c0ef

File tree

8 files changed

+22
-16
lines changed

8 files changed

+22
-16
lines changed

src/server/middleware/cors/vary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FastifyReply } from "fastify";
1+
import type { FastifyReply } from "fastify";
22
import LRUCache from "mnemonist/lru-cache";
33

44
/**

src/utils/account.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { StatusCodes } from "http-status-codes";
2+
import LRUMap from "mnemonist/lru-map";
23
import type { Address } from "thirdweb";
34
import type { Account } from "thirdweb/wallets";
45
import { getWalletDetails } from "../db/wallets/getWalletDetails";
@@ -15,7 +16,7 @@ import { decrypt } from "./crypto";
1516
import { env } from "./env";
1617
import { thirdwebClient } from "./sdk";
1718

18-
export const _accountsCache = new Map<string, Account>();
19+
export const _accountsCache = new LRUMap<string, Account>(2048);
1920

2021
export const getAccount = async (args: {
2122
chainId: number;

src/utils/cache/accessToken.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Tokens } from "@prisma/client";
2+
import LRUMap from "mnemonist/lru-map";
23
import { getToken } from "../../db/tokens/getToken";
34

45
// Cache an access token JWT to the token object, or null if not found.
5-
export const accessTokenCache = new Map<string, Tokens | null>();
6+
export const accessTokenCache = new LRUMap<string, Tokens | null>(2048);
67

78
interface GetAccessTokenParams {
89
jwt: string;

src/utils/cache/getSdk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Type } from "@sinclair/typebox";
22
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
3+
import LRUMap from "mnemonist/lru-map";
34
import { getChainMetadata } from "thirdweb/chains";
45
import { badChainError } from "../../server/middleware/error";
56
import { getChain } from "../chain";
67
import { env } from "../env";
78
import { getWallet } from "./getWallet";
89

9-
export const sdkCache = new Map<string, ThirdwebSDK>();
10+
export const sdkCache = new LRUMap<string, ThirdwebSDK>(2048);
1011

1112
export const networkResponseSchema = Type.Object({
1213
name: Type.String({

src/utils/cache/getSmartWalletV5.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import LRUMap from "mnemonist/lru-map";
2+
import { getContract, readContract, type Address, type Chain } from "thirdweb";
13
import { smartWallet, type Account } from "thirdweb/wallets";
24
import { getAccount } from "../account";
35
import { thirdwebClient } from "../sdk";
46

5-
import { getContract, readContract, type Address, type Chain } from "thirdweb";
6-
7-
export const smartWalletsCache = new Map<string, Account>();
7+
export const smartWalletsCache = new LRUMap<string, Account>(2048);
88

99
interface SmartWalletParams {
1010
chain: Chain;

src/utils/cache/getWallet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { EVMWallet } from "@thirdweb-dev/wallets";
22
import { AwsKmsWallet } from "@thirdweb-dev/wallets/evm/wallets/aws-kms";
33
import { GcpKmsWallet } from "@thirdweb-dev/wallets/evm/wallets/gcp-kms";
44
import { StatusCodes } from "http-status-codes";
5+
import LRUMap from "mnemonist/lru-map";
56
import { getWalletDetails } from "../../db/wallets/getWalletDetails";
67
import type { PrismaTransaction } from "../../schema/prisma";
78
import { WalletType } from "../../schema/wallet";
@@ -14,7 +15,7 @@ import { decrypt } from "../crypto";
1415
import { env } from "../env";
1516
import { getConfig } from "./getConfig";
1617

17-
export const walletsCache = new Map<string, EVMWallet>();
18+
export const walletsCache = new LRUMap<string, EVMWallet>(2048);
1819

1920
interface GetWalletParams {
2021
pgtx?: PrismaTransaction;

src/utils/cache/getWebhook.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Webhooks } from "@prisma/client";
1+
import type { Webhooks } from "@prisma/client";
2+
import LRUMap from "mnemonist/lru-map";
23
import { getAllWebhooks } from "../../db/webhooks/getAllWebhooks";
3-
import { WebhooksEventTypes } from "../../schema/webhooks";
4+
import type { WebhooksEventTypes } from "../../schema/webhooks";
45

5-
export const webhookCache = new Map<string, Webhooks[]>();
6+
export const webhookCache = new LRUMap<string, Webhooks[]>(2048);
67

78
export const getWebhooksByEventType = async (
89
eventType: WebhooksEventTypes,

src/utils/cache/keypair.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { Keypairs } from "@prisma/client";
2+
import LRUMap from "mnemonist/lru-map";
23
import { getKeypairByHash, getKeypairByPublicKey } from "../../db/keypair/get";
34

45
// Cache a public key to the Keypair object, or null if not found.
5-
export const keypairCache = new Map<string, Keypairs | null>();
6+
export const keypairCache = new LRUMap<string, Keypairs | null>(2048);
67

78
/**
89
* Get a keypair by public key or hash.
@@ -16,8 +17,8 @@ export const getKeypair = async (args: {
1617
const key = publicKey
1718
? `public-key:${args.publicKey}`
1819
: publicKeyHash
19-
? `public-key-hash:${args.publicKeyHash}`
20-
: null;
20+
? `public-key-hash:${args.publicKeyHash}`
21+
: null;
2122

2223
if (!key) {
2324
throw new Error('Must provide "publicKey" or "publicKeyHash".');
@@ -31,8 +32,8 @@ export const getKeypair = async (args: {
3132
const keypair = publicKey
3233
? await getKeypairByPublicKey(publicKey)
3334
: publicKeyHash
34-
? await getKeypairByHash(publicKeyHash)
35-
: null;
35+
? await getKeypairByHash(publicKeyHash)
36+
: null;
3637

3738
keypairCache.set(key, keypair);
3839
return keypair;

0 commit comments

Comments
 (0)