Skip to content

Commit

Permalink
fix: use LRUMap for all in-memory caches (#734)
Browse files Browse the repository at this point in the history
* fix: use LRUMap for all in-memory caches

* fix import

* undo test changes
arcoraven authored Oct 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ddf7bbb commit 873c0ef
Showing 8 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/server/middleware/cors/vary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FastifyReply } from "fastify";
import type { FastifyReply } from "fastify";
import LRUCache from "mnemonist/lru-cache";

/**
3 changes: 2 additions & 1 deletion src/utils/account.ts
Original file line number Diff line number Diff line change
@@ -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<string, Account>();
export const _accountsCache = new LRUMap<string, Account>(2048);

export const getAccount = async (args: {
chainId: number;
3 changes: 2 additions & 1 deletion src/utils/cache/accessToken.ts
Original file line number Diff line number Diff line change
@@ -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<string, Tokens | null>();
export const accessTokenCache = new LRUMap<string, Tokens | null>(2048);

interface GetAccessTokenParams {
jwt: string;
3 changes: 2 additions & 1 deletion src/utils/cache/getSdk.ts
Original file line number Diff line number Diff line change
@@ -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<string, ThirdwebSDK>();
export const sdkCache = new LRUMap<string, ThirdwebSDK>(2048);

export const networkResponseSchema = Type.Object({
name: Type.String({
6 changes: 3 additions & 3 deletions src/utils/cache/getSmartWalletV5.ts
Original file line number Diff line number Diff line change
@@ -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<string, Account>();
export const smartWalletsCache = new LRUMap<string, Account>(2048);

interface SmartWalletParams {
chain: Chain;
3 changes: 2 additions & 1 deletion src/utils/cache/getWallet.ts
Original file line number Diff line number Diff line change
@@ -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<string, EVMWallet>();
export const walletsCache = new LRUMap<string, EVMWallet>(2048);

interface GetWalletParams {
pgtx?: PrismaTransaction;
7 changes: 4 additions & 3 deletions src/utils/cache/getWebhook.ts
Original file line number Diff line number Diff line change
@@ -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<string, Webhooks[]>();
export const webhookCache = new LRUMap<string, Webhooks[]>(2048);

export const getWebhooksByEventType = async (
eventType: WebhooksEventTypes,
11 changes: 6 additions & 5 deletions src/utils/cache/keypair.ts
Original file line number Diff line number Diff line change
@@ -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<string, Keypairs | null>();
export const keypairCache = new LRUMap<string, Keypairs | null>(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;

0 comments on commit 873c0ef

Please sign in to comment.