diff --git a/src/server/middleware/cors/vary.ts b/src/server/middleware/cors/vary.ts index 8d4ae4c84..2a4423138 100644 --- a/src/server/middleware/cors/vary.ts +++ b/src/server/middleware/cors/vary.ts @@ -1,4 +1,4 @@ -import { FastifyReply } from "fastify"; +import type { FastifyReply } from "fastify"; import LRUCache from "mnemonist/lru-cache"; /** diff --git a/src/utils/account.ts b/src/utils/account.ts index 8880a553d..8eb5b55bc 100644 --- a/src/utils/account.ts +++ b/src/utils/account.ts @@ -1,4 +1,5 @@ import { StatusCodes } from "http-status-codes"; +import LRUMap from "mnemonist/lru-map"; import type { Address } from "thirdweb"; import type { Account } from "thirdweb/wallets"; import { getWalletDetails } from "../db/wallets/getWalletDetails"; @@ -15,7 +16,7 @@ import { decrypt } from "./crypto"; import { env } from "./env"; import { thirdwebClient } from "./sdk"; -export const _accountsCache = new Map(); +export const _accountsCache = new LRUMap(2048); export const getAccount = async (args: { chainId: number; diff --git a/src/utils/cache/accessToken.ts b/src/utils/cache/accessToken.ts index 894a51dfb..992e25ee9 100644 --- a/src/utils/cache/accessToken.ts +++ b/src/utils/cache/accessToken.ts @@ -1,8 +1,9 @@ import type { Tokens } from "@prisma/client"; +import LRUMap from "mnemonist/lru-map"; import { getToken } from "../../db/tokens/getToken"; // Cache an access token JWT to the token object, or null if not found. -export const accessTokenCache = new Map(); +export const accessTokenCache = new LRUMap(2048); interface GetAccessTokenParams { jwt: string; diff --git a/src/utils/cache/getSdk.ts b/src/utils/cache/getSdk.ts index e43a9ef2f..80e965492 100644 --- a/src/utils/cache/getSdk.ts +++ b/src/utils/cache/getSdk.ts @@ -1,12 +1,13 @@ import { Type } from "@sinclair/typebox"; import { ThirdwebSDK } from "@thirdweb-dev/sdk"; +import LRUMap from "mnemonist/lru-map"; import { getChainMetadata } from "thirdweb/chains"; import { badChainError } from "../../server/middleware/error"; import { getChain } from "../chain"; import { env } from "../env"; import { getWallet } from "./getWallet"; -export const sdkCache = new Map(); +export const sdkCache = new LRUMap(2048); export const networkResponseSchema = Type.Object({ name: Type.String({ diff --git a/src/utils/cache/getSmartWalletV5.ts b/src/utils/cache/getSmartWalletV5.ts index 5361c0d46..7ab01f275 100644 --- a/src/utils/cache/getSmartWalletV5.ts +++ b/src/utils/cache/getSmartWalletV5.ts @@ -1,10 +1,10 @@ +import LRUMap from "mnemonist/lru-map"; +import { getContract, readContract, type Address, type Chain } from "thirdweb"; import { smartWallet, type Account } from "thirdweb/wallets"; import { getAccount } from "../account"; import { thirdwebClient } from "../sdk"; -import { getContract, readContract, type Address, type Chain } from "thirdweb"; - -export const smartWalletsCache = new Map(); +export const smartWalletsCache = new LRUMap(2048); interface SmartWalletParams { chain: Chain; diff --git a/src/utils/cache/getWallet.ts b/src/utils/cache/getWallet.ts index 9b559092e..ae0397375 100644 --- a/src/utils/cache/getWallet.ts +++ b/src/utils/cache/getWallet.ts @@ -2,6 +2,7 @@ import type { EVMWallet } from "@thirdweb-dev/wallets"; import { AwsKmsWallet } from "@thirdweb-dev/wallets/evm/wallets/aws-kms"; import { GcpKmsWallet } from "@thirdweb-dev/wallets/evm/wallets/gcp-kms"; import { StatusCodes } from "http-status-codes"; +import LRUMap from "mnemonist/lru-map"; import { getWalletDetails } from "../../db/wallets/getWalletDetails"; import type { PrismaTransaction } from "../../schema/prisma"; import { WalletType } from "../../schema/wallet"; @@ -14,7 +15,7 @@ import { decrypt } from "../crypto"; import { env } from "../env"; import { getConfig } from "./getConfig"; -export const walletsCache = new Map(); +export const walletsCache = new LRUMap(2048); interface GetWalletParams { pgtx?: PrismaTransaction; diff --git a/src/utils/cache/getWebhook.ts b/src/utils/cache/getWebhook.ts index 423fcfddf..ded5543df 100644 --- a/src/utils/cache/getWebhook.ts +++ b/src/utils/cache/getWebhook.ts @@ -1,8 +1,9 @@ -import { Webhooks } from "@prisma/client"; +import type { Webhooks } from "@prisma/client"; +import LRUMap from "mnemonist/lru-map"; import { getAllWebhooks } from "../../db/webhooks/getAllWebhooks"; -import { WebhooksEventTypes } from "../../schema/webhooks"; +import type { WebhooksEventTypes } from "../../schema/webhooks"; -export const webhookCache = new Map(); +export const webhookCache = new LRUMap(2048); export const getWebhooksByEventType = async ( eventType: WebhooksEventTypes, diff --git a/src/utils/cache/keypair.ts b/src/utils/cache/keypair.ts index 06bb95213..17165ed5f 100644 --- a/src/utils/cache/keypair.ts +++ b/src/utils/cache/keypair.ts @@ -1,8 +1,9 @@ import type { Keypairs } from "@prisma/client"; +import LRUMap from "mnemonist/lru-map"; import { getKeypairByHash, getKeypairByPublicKey } from "../../db/keypair/get"; // Cache a public key to the Keypair object, or null if not found. -export const keypairCache = new Map(); +export const keypairCache = new LRUMap(2048); /** * Get a keypair by public key or hash. @@ -16,8 +17,8 @@ export const getKeypair = async (args: { const key = publicKey ? `public-key:${args.publicKey}` : publicKeyHash - ? `public-key-hash:${args.publicKeyHash}` - : null; + ? `public-key-hash:${args.publicKeyHash}` + : null; if (!key) { throw new Error('Must provide "publicKey" or "publicKeyHash".'); @@ -31,8 +32,8 @@ export const getKeypair = async (args: { const keypair = publicKey ? await getKeypairByPublicKey(publicKey) : publicKeyHash - ? await getKeypairByHash(publicKeyHash) - : null; + ? await getKeypairByHash(publicKeyHash) + : null; keypairCache.set(key, keypair); return keypair;