Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add LRU cache implementation for link middleware

Kiran K7c0140f562aa
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index af57dda3cd..2649b1ed7e 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -1,6 +1,7 @@
import { LinkProps, RedisLinkProps } from "@/lib/types";
import { formatRedisLink, redis, redisWithTimeout } from "@/lib/upstash";
import { decodeKey, isCaseSensitiveDomain } from "./case-sensitivity";
+import { lruCache } from "./lru-link-cache";
import { ExpandedLink } from "./utils/transform-link";
/*
@@ -51,19 +52,40 @@ class LinkCache {
}
async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
- // we're using the special redisWithTimeout client in case Redis times out
try {
+ const cacheKey = `linkcache:${domain}:${key}`;
+
+ let cachedLink = lruCache.get(cacheKey) || null;
+
+ if (cachedLink) {
+ console.log(`[LRU Cache HIT] ${cacheKey}`);
+ return cachedLink;
+ }
+
+ console.log(`[LRU Cache MISS] ${cacheKey} - Checking Redis...`);
+
+ // we're using the special redisWithTimeout client in case Redis times out
// here we use linkcache:${domain}:${key} instead of this._createKey({ domain, key })
// because the key can either be cached as case-sensitive or case-insensitive depending on the domain
// so we should get the original key from the cache
- return await redisWithTimeout.get<RedisLinkProps>(
+ cachedLink = await redisWithTimeout.get<RedisLinkProps>(
`linkcache:${domain}:${key}`,
);
+
+ if (cachedLink) {
+ console.log(`[Redis Cache HIT] ${cacheKey} - Populating LRU cache`);
+ lruCache.set(cacheKey, cachedLink);
+ } else {
+ console.log(`[Cache MISS] ${cacheKey} - Not found in LRU or Redis`);
+ }
+
+ return cachedLink;
} catch (error) {
console.error(
"[LinkCache]: Timeout getting cached link from Redis:",
error,
);
+
return null;
}
}
diff --git a/apps/web/lib/api/links/lru-link-cache.ts b/apps/web/lib/api/links/lru-link-cache.ts
new file mode 100644
index 0000000000..42e9e79b4f
--- /dev/null
+++ b/apps/web/lib/api/links/lru-link-cache.ts
@@ -0,0 +1,23 @@
+import { RedisLinkProps } from "@/lib/types";
+import { LRUCache } from "lru-cache";
+
+/*
+ * LRU cache for link middleware to reduce Redis load during traffic spikes.
+ * Max 5000 entries with 60-second TTL.
+ *
+ * In development, we store the cache in globalThis to prevent HMR from resetting it.
+ */
+const globalForLruCache = globalThis as unknown as {
+ lruCache: LRUCache<string, RedisLinkProps> | undefined;
+};
+
+export const lruCache =
+ globalForLruCache.lruCache ??
+ new LRUCache<string, RedisLinkProps>({
+ max: 5000,
+ ttl: 60000, // 60 seconds
+ });
+
+if (process.env.NODE_ENV !== "production") {
+ globalForLruCache.lruCache = lruCache;
+}
diff --git a/apps/web/package.json b/apps/web/package.json
index 52ba17b13e..c02f631e68 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -90,6 +90,7 @@
"json-2-csv": "^5.5.0",
"jszip": "^3.10.1",
"linkify-react": "4.3.2",
+ "lru-cache": "^11.2.2",
"lucide-react": "^0.462.0",
"luxon": "^3.5.0",
"motion": "^12.23.22",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3d06e1ea02..72c87cba8e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -257,6 +257,9 @@ importers:
linkify-react:
specifier: 4.3.2
+ lru-cache:
+ specifier: ^11.2.2
+ version: 11.2.2
lucide-react:
specifier: ^0.462.0
version: 0.462.0([email protected])
@@ -10026,10 +10029,6 @@ packages:
resolution: {integrity: sha512-ozCC6gdQ+glXOQsveKD0YsDy8DSQFjDTz4zyzEHNV5+JP5D62LmfDZ6o1cycFx9ouG940M5dE8C8CTewdj2YWQ==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
- resolution: {integrity: sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==}
- engines: {node: 14 || >=16.14}
-
resolution: {integrity: sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==}
@@ -25299,12 +25298,9 @@ snapshots:
-
- optional: true
dependencies:
@@ -26484,7 +26480,7 @@ snapshots:
dependencies:
- lru-cache: 10.2.0
+ lru-cache: 10.4.3
minipass: 7.0.4