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.

use redis.mget to avoid roundtrip

Steven Teyb49b3d7ec523
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index 5daadd6da4..aab53cc917 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -6,7 +6,8 @@ import { parseRequestBody } from "@/lib/api/utils";
import { getWorkspaceViaEdge } from "@/lib/planetscale";
import { getLinkWithPartner } from "@/lib/planetscale/get-link-with-partner";
import { recordClick } from "@/lib/tinybird";
-import { formatRedisLink } from "@/lib/upstash";
+import { RedisLinkProps } from "@/lib/types";
+import { formatRedisLink, redis } from "@/lib/upstash";
import { DiscountSchema } from "@/lib/zod/schemas/discount";
import { PartnerSchema } from "@/lib/zod/schemas/partners";
import { isValidUrl, LOCALHOST_IP, nanoid } from "@dub/utils";
@@ -54,16 +55,11 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- let [cachedClickId, cachedLink] = await Promise.all([
- clickCache.get({
- domain,
- key,
- ip,
- }),
- linkCache.get({
- domain,
- key,
- }),
+ let [cachedClickId, cachedLink] = await redis.mget<
+ [string, RedisLinkProps]
+ >([
+ clickCache._createKey({ domain, key, ip }),
+ linkCache._createKey({ domain, key }),
]);
// assign a new clickId if there's no cached clickId
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 2e581d3c8c..422eff1937 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -51,7 +51,7 @@ class LinkCache {
}
async get({ domain, key }: Pick<LinkProps, "domain" | "key">) {
- return await redis.get<RedisLinkProps>(`linkcache:${domain}:${key}`);
+ return await redis.get<RedisLinkProps>(this._createKey({ domain, key }));
}
async delete({ domain, key }: Pick<LinkProps, "domain" | "key">) {