The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
finalize leadCache
Steven Tey0a3da06ba9f4
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex 991e8bbe3c..0ca00349ab 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -160,17 +160,22 @@ export const POST = withWorkspace(customer = await upsertCustomer();const leadEventPayload = createLeadEventPayload(customer.id);+ const cacheLeadEventPayload = Array.isArray(leadEventPayload)+ ? leadEventPayload[0]+ : leadEventPayload;await Promise.all([// Use recordLeadSync which waits for the operation to completerecordLeadSync(leadEventPayload),// Cache the latest lead event for 5 minutes because the ingested event is not available immediately on Tinybird+ // we're setting two keys because we want to support the use case where the customer has multiple lead events+ redis.set(`leadCache:${customer.id}`, cacheLeadEventPayload, {+ ex: 60 * 5,+ }),redis.set(`leadCache:${customer.id}:${stringifiedEventName}`,- Array.isArray(leadEventPayload)- ? leadEventPayload[0]- : leadEventPayload,+ cacheLeadEventPayload,{ex: 60 * 5,},diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex d4006cac9a..74965cb20e 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -90,22 +90,16 @@ export const POST = withWorkspace(let leadEventData: LeadEvent | null = null;if (!leadEvent || leadEvent.data.length === 0) {- let cachedLeadEvent: LeadEvent | null = null;// Check cache to see if the lead event exists// if leadEventName is provided, we only check for that specific event// otherwise, we check for all cached lead events for that customer- if (leadEventName) {- cachedLeadEvent = await redis.get<LeadEvent>(- `leadCache:${customer.id}:${leadEventName.toLowerCase().replace(" ", "-")}`,- );- } else {- const cachedLeadEventKeys = await redis.scan(0, {- match: `leadCache:${customer.id}:*`,- });+ const cachedLeadEvents = await redis.mget<(LeadEvent | null)[]>([+ `leadCache:${customer.id}${leadEventName ? `:${leadEventName.toLowerCase().replace(" ", "-")}` : ""}`,+ `latestLeadEvent:${customer.id}`,+ ]);- cachedLeadEvent = await redis.get<LeadEvent>(cachedLeadEventKeys[1][0]);- }+ const cachedLeadEvent = cachedLeadEvents[0] ?? cachedLeadEvents[1];if (!cachedLeadEvent) {throw new DubApiError({