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

expose cache size env var #858

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/shared/db/wallet-credentials/get-wallet-credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ParsedWalletCredential = z.infer<typeof walletCredentialsSchema>;
export const walletCredentialsCache = new LRUMap<
string,
ParsedWalletCredential
>(2048);
>(env.ACCOUNT_CAHCE_SIZE);

interface GetWalletCredentialParams {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/db/wallets/get-wallet-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export type SmartBackendWalletType = (typeof SmartBackendWalletTypes)[number];
export type BackendWalletType = (typeof BackendWalletTypes)[number];
export type ParsedWalletDetails = z.infer<typeof walletDetailsSchema>;

export const walletDetailsCache = new LRUMap<string, ParsedWalletDetails>(2048);
export const walletDetailsCache = new LRUMap<string, ParsedWalletDetails>(env.ACCOUNT_CAHCE_SIZE);
/**
* Return the wallet details for the given address.
*
Expand Down
5 changes: 3 additions & 2 deletions src/shared/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import { thirdwebClient } from "./sdk";
import { getWalletCredential } from "../db/wallet-credentials/get-wallet-credential";
import { getCircleAccount } from "../../server/utils/wallets/circle";
import { getConfig } from "./cache/get-config";
import { env } from "./env";

export const _accountsCache = new LRUMap<string, Account>(2048);
export const _accountsCache = new LRUMap<string, Account>(env.ACCOUNT_CAHCE_SIZE);

export const getAccount = async (args: {
chainId: number;
Expand Down Expand Up @@ -212,7 +213,7 @@ export const walletDetailsToAccount = async ({
}
};

export const _adminAccountsCache = new LRUMap<string, Account>(2048);
export const _adminAccountsCache = new LRUMap<string, Account>(env.ACCOUNT_CAHCE_SIZE);

/**
* Get the admin account for a smart backend wallet (cached)
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/cache/access-token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Tokens } from "@prisma/client";
import LRUMap from "mnemonist/lru-map";
import { getToken } from "../../db/tokens/get-token";
import { env } from "../env";

// Cache an access token JWT to the token object, or null if not found.
export const accessTokenCache = new LRUMap<string, Tokens | null>(2048);
export const accessTokenCache = new LRUMap<string, Tokens | null>(env.ACCOUNT_CAHCE_SIZE);

interface GetAccessTokenParams {
jwt: string;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils/cache/get-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getChain } from "../chain";
import { env } from "../env";
import { getWallet } from "./get-wallet";

export const sdkCache = new LRUMap<string, ThirdwebSDK>(2048);
export const sdkCache = new LRUMap<string, ThirdwebSDK>(env.ACCOUNT_CAHCE_SIZE);

export const networkResponseSchema = Type.Object({
name: Type.String({
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/cache/get-smart-wallet-v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ 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 { env } from "../env";

export const smartWalletsCache = new LRUMap<string, Account>(2048);
export const smartWalletsCache = new LRUMap<string, Account>(env.ACCOUNT_CAHCE_SIZE);

interface SmartWalletParams {
chain: Chain;
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/cache/get-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { splitAwsKmsArn } from "../../../server/utils/wallets/aws-kms-arn";
import { splitGcpKmsResourcePath } from "../../../server/utils/wallets/gcp-kms-resource-path";
import { getLocalWallet } from "../../../server/utils/wallets/get-local-wallet";
import { getSmartWallet } from "../../../server/utils/wallets/get-smart-wallet";
import { env } from "../env";

export const walletsCache = new LRUMap<string, EVMWallet>(2048);
export const walletsCache = new LRUMap<string, EVMWallet>(env.ACCOUNT_CAHCE_SIZE);

interface GetWalletParams {
pgtx?: PrismaTransaction;
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/cache/get-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { Webhooks } from "@prisma/client";
import LRUMap from "mnemonist/lru-map";
import { getAllWebhooks } from "../../db/webhooks/get-all-webhooks";
import type { WebhooksEventTypes } from "../../schemas/webhooks";
import { env } from "../env";

export const webhookCache = new LRUMap<string, Webhooks[]>(2048);
export const webhookCache = new LRUMap<string, Webhooks[]>(env.ACCOUNT_CAHCE_SIZE);

export const getWebhooksByEventType = async (
eventType: WebhooksEventTypes,
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/cache/keypair.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Keypairs } from "@prisma/client";
import LRUMap from "mnemonist/lru-map";
import { getKeypairByHash, getKeypairByPublicKey } from "../../db/keypair/get";
import { env } from "../env";

// Cache a public key to the Keypair object, or null if not found.
export const keypairCache = new LRUMap<string, Keypairs | null>(2048);
export const keypairCache = new LRUMap<string, Keypairs | null>(env.ACCOUNT_CAHCE_SIZE);

/**
* Get a keypair by public key or hash.
Expand Down
2 changes: 2 additions & 0 deletions src/shared/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const env = createEnv({
.enum(["default", "sandbox", "server_only", "worker_only"])
.default("default"),
GLOBAL_RATE_LIMIT_PER_MIN: z.coerce.number().default(400 * 60),
ACCOUNT_CAHCE_SIZE: z.coerce.number().default(2048),
DD_TRACER_ACTIVATED: boolEnvSchema(false),

// Prometheus
Expand Down Expand Up @@ -145,6 +146,7 @@ export const env = createEnv({
ENABLE_CUSTOM_HMAC_AUTH: process.env.ENABLE_CUSTOM_HMAC_AUTH,
CUSTOM_HMAC_AUTH_CLIENT_ID: process.env.CUSTOM_HMAC_AUTH_CLIENT_ID,
CUSTOM_HMAC_AUTH_CLIENT_SECRET: process.env.CUSTOM_HMAC_AUTH_CLIENT_SECRET,
ACCOUNT_CAHCE_SIZE: process.env.ACCOUNT_CAHCE_SIZE,
},
onValidationError: (error: ZodError) => {
console.error(
Expand Down
Loading