The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update LRUCache implementation
Steven Teyf551756bab39
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.tsindex 2649b1ed7e..0cdb640ecd 100644--- a/apps/web/lib/api/links/cache.ts+++ b/apps/web/lib/api/links/cache.ts@@ -1,9 +1,30 @@import { LinkProps, RedisLinkProps } from "@/lib/types";import { formatRedisLink, redis, redisWithTimeout } from "@/lib/upstash";+import { LRUCache } from "lru-cache";import { decodeKey, isCaseSensitiveDomain } from "./case-sensitivity";-import { lruCache } from "./lru-link-cache";import { ExpandedLink } from "./utils/transform-link";+/*+ * Link LRU cache to reduce Redis load during traffic spikes.+ * Max 5000 entries with 3-second TTL.+ *+ * In development, we store the cache in globalThis to prevent HMR from resetting it.+ */+const globalForLinkLRUCache = globalThis as unknown as {+ linkLRUCache: LRUCache<string, RedisLinkProps> | undefined;+};++const linkLRUCache =+ globalForLinkLRUCache.linkLRUCache ??+ new LRUCache<string, RedisLinkProps>({+ max: 5000,+ ttl: 3000, // 3 seconds+ });++if (process.env.NODE_ENV !== "production") {+ globalForLinkLRUCache.linkLRUCache = linkLRUCache;+}+/** Link cache expiration is set to 24 hours by default for all links.* Caveat: we don't set expiration for links with webhooks since it's expensive@@ -43,9 +64,13 @@ class LinkCache {async set(link: ExpandedLink) {const redisLink = formatRedisLink(link);const hasWebhooks = redisLink.webhookIds && redisLink.webhookIds.length > 0;+ const cacheKey = this._createKey({ domain: link.domain, key: link.key });++ // Update LRU cache immediately to prevent stale reads+ linkLRUCache.set(cacheKey, redisLink);return await redis.set(- this._createKey({ domain: link.domain, key: link.key }),+ cacheKey,redisLink,hasWebhooks ? undefined : { ex: CACHE_EXPIRATION },);@@ -55,7 +80,8 @@ class LinkCache {try {const cacheKey = `linkcache:${domain}:${key}`;- let cachedLink = lruCache.get(cacheKey) || null;+ // Check LRU cache first before hitting Redis+ let cachedLink = linkLRUCache.get(cacheKey) || null;if (cachedLink) {console.log(`[LRU Cache HIT] ${cacheKey}`);@@ -74,7 +100,7 @@ class LinkCache {if (cachedLink) {console.log(`[Redis Cache HIT] ${cacheKey} - Populating LRU cache`);- lruCache.set(cacheKey, cachedLink);+ linkLRUCache.set(cacheKey, cachedLink);} else {console.log(`[Cache MISS] ${cacheKey} - Not found in LRU or Redis`);}diff --git a/apps/web/lib/api/links/lru-link-cache.ts b/apps/web/lib/api/links/lru-link-cache.tsdeleted file mode 100644index 42e9e79b4f..0000000000--- a/apps/web/lib/api/links/lru-link-cache.ts+++ /dev/null@@ -1,23 +0,0 @@-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;-}