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.

Refactor click caching with dedicated ClickCache class

Kiran Kee7465071c4b
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index 2f09158d9b..c2486f8340 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -1,9 +1,9 @@
import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { clickCache } from "@/lib/api/links/click-cache";
import { parseRequestBody } from "@/lib/api/utils";
import { getLinkWithAllowedHostnames } from "@/lib/planetscale/get-link-with-allowed-hostnames";
import { recordClick } from "@/lib/tinybird";
-import { redis } from "@/lib/upstash";
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { AxiomRequest, withAxiom } from "next-axiom";
@@ -32,8 +32,7 @@ export const POST = withAxiom(
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- const cacheKey = `recordClick:${domain}:${key}:${ip}`;
- let clickId = await redis.get<string>(cacheKey);
+ let clickId = await clickCache.get({ domain, key, ip });
// only generate + record a new click ID if it's not already cached in Redis
if (!clickId) {
diff --git a/apps/web/app/api/track/visit/route.ts b/apps/web/app/api/track/visit/route.ts
index 4d751d06ef..7a731c68d8 100644
--- a/apps/web/app/api/track/visit/route.ts
+++ b/apps/web/app/api/track/visit/route.ts
@@ -1,9 +1,9 @@
import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { clickCache } from "@/lib/api/links/click-cache";
import { parseRequestBody } from "@/lib/api/utils";
import { getLinkWithAllowedHostnames } from "@/lib/planetscale/get-link-with-allowed-hostnames";
import { recordClick } from "@/lib/tinybird";
-import { redis } from "@/lib/upstash";
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress, waitUntil } from "@vercel/functions";
import { AxiomRequest, withAxiom } from "next-axiom";
@@ -38,9 +38,8 @@ export const POST = withAxiom(
}
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- const cacheKey = `recordClick:${domain}:${key}:${ip}`;
- let clickId = await redis.get<string>(cacheKey);
+ let clickId = await clickCache.get({ domain, key, ip });
// only generate + record a new click ID if it's not already cached in Redis
if (!clickId) {
diff --git a/apps/web/lib/api/links/click-cache.ts b/apps/web/lib/api/links/click-cache.ts
new file mode 100644
index 0000000000..42a52ae56a
--- /dev/null
+++ b/apps/web/lib/api/links/click-cache.ts
@@ -0,0 +1,28 @@
+import { redis } from "@/lib/upstash";
+
+// Cache the click ID in Redis for 1 hour
+const CACHE_EXPIRATION = 60 * 60;
+
+interface KeyProps {
+ domain: string;
+ key: string;
+ ip: string | undefined;
+}
+
+class ClickCache {
+ async set({ domain, key, ip, clickId }: KeyProps & { clickId: string }) {
+ return await redis.set(this._createKey({ domain, key, ip }), clickId, {
+ ex: CACHE_EXPIRATION,
+ });
+ }
+
+ async get({ domain, key, ip }: KeyProps) {
+ return await redis.get<string>(this._createKey({ domain, key, ip }));
+ }
+
+ _createKey({ domain, key, ip }: KeyProps) {
+ return `recordClick:${domain}:${key}:${ip}`;
+ }
+}
+
+export const clickCache = new ClickCache();
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 5f7fc86e9c..21b0073088 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -6,7 +6,7 @@ import {
parse,
} from "@/lib/middleware/utils";
import { recordClick } from "@/lib/tinybird";
-import { formatRedisLink, redis } from "@/lib/upstash";
+import { formatRedisLink } from "@/lib/upstash";
import {
DUB_HEADERS,
LEGAL_WORKSPACE_ID,
@@ -27,6 +27,7 @@ import {
} from "next/server";
import { linkCache } from "../api/links/cache";
import { isCaseSensitiveDomain } from "../api/links/case-sensitivity";
+import { clickCache } from "../api/links/click-cache";
import { getLinkViaEdge } from "../planetscale";
import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";
import { hasEmptySearchParams } from "./utils/has-empty-search-params";
@@ -227,8 +228,8 @@ export default async function LinkMiddleware(
// if trackConversion is enabled, check if clickId is cached in Redis
if (trackConversion) {
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- const cacheKey = `recordClick:${domain}:${key}:${ip}`;
- clickId = (await redis.get<string>(cacheKey)) || undefined;
+
+ clickId = (await clickCache.get({ domain, key, ip })) || undefined;
}
// if there's still no clickId, generate a new one
if (!clickId) {
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index afb400910e..e339f2be70 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -7,6 +7,7 @@ import {
import { EU_COUNTRY_CODES } from "@dub/utils/src/constants/countries";
import { geolocation, ipAddress } from "@vercel/functions";
import { userAgent } from "next/server";
+import { clickCache } from "../api/links/click-cache";
import { ExpandedLink, transformLink } from "../api/links/utils/transform-link";
import {
detectBot,
@@ -16,7 +17,6 @@ import {
} from "../middleware/utils";
import { conn } from "../planetscale";
import { WorkspaceProps } from "../types";
-import { redis } from "../upstash";
import { webhookCache } from "../webhook/cache";
import { sendWebhooks } from "../webhook/qstash";
import { transformClickEventData } from "../webhook/transform";
@@ -65,13 +65,11 @@ export async function recordClick({
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- const cacheKey = `recordClick:${domain}:${key}:${ip}`;
-
// by default, we deduplicate clicks for a domain + key pair from the same IP address – only record 1 click per hour
// we only need to do these if skipRatelimit is not true (we skip it in /api/track/:path endpoints)
if (!skipRatelimit) {
// here, we check if the clickId is cached in Redis within the last hour
- const cachedClickId = await redis.get<string>(cacheKey);
+ const cachedClickId = await clickCache.get({ domain, key, ip });
if (cachedClickId) {
return null;
}
@@ -151,9 +149,7 @@ export async function recordClick({
).then((res) => res.json()),
// cache the click ID in Redis for 1 hour
- redis.set(cacheKey, clickId, {
- ex: 60 * 60,
- }),
+ clickCache.set({ domain, key, ip, clickId }),
// increment the click count for the link (based on their ID)
// we have to use planetscale connection directly (not prismaEdge) because of connection pooling