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.

linkCache.expireMany

Steven Tey8b13323b69a9
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 93c69c9697..e27555faac 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -95,6 +95,21 @@ class LinkCache {
return await pipeline.exec();
}
+ async expireMany(links: Pick<LinkProps, "domain" | "key">[]) {
+ if (links.length === 0) {
+ return;
+ }
+
+ const pipeline = redis.pipeline();
+
+ links.forEach(({ domain, key }) => {
+ // expire the link cache key immediately
+ pipeline.expire(this._createKey({ domain, key }), 1);
+ });
+
+ return await pipeline.exec();
+ }
+
_createKey({ domain, key }: Pick<LinkProps, "domain" | "key">) {
return `linkcache:${domain}:${key}`.toLowerCase();
}
diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.ts
index 920cd56f9c..71de693888 100644
--- a/apps/web/lib/api/workspaces.ts
+++ b/apps/web/lib/api/workspaces.ts
@@ -12,6 +12,7 @@ import { waitUntil } from "@vercel/functions";
import { qstash } from "../cron";
import { cancelSubscription } from "../stripe/cancel-subscription";
import { markDomainAsDeleted } from "./domains";
+import { linkCache } from "./links/cache";
export async function deleteWorkspace(
workspace: Pick<WorkspaceProps, "id" | "slug" | "logo" | "stripeId">,
@@ -63,28 +64,44 @@ export async function deleteWorkspace(
export async function deleteWorkspaceAdmin(
workspace: Pick<WorkspaceProps, "id" | "slug" | "logo" | "stripeId">,
) {
- // update all default domain links to the legal workspace
- const updateLinkPrismaResponse = await prisma.link.updateMany({
- where: {
- projectId: workspace.id,
- domain: {
- in: DUB_DOMAINS_ARRAY,
+ const [defaultDomainLinks, customDomains] = await Promise.all([
+ prisma.link.findMany({
+ where: {
+ domain: {
+ in: DUB_DOMAINS_ARRAY,
+ },
+ projectId: workspace.id,
},
- },
- data: {
- userId: LEGAL_USER_ID,
- projectId: LEGAL_WORKSPACE_ID,
- },
- });
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ },
+ }),
+ prisma.domain.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ select: {
+ slug: true,
+ },
+ }),
+ ]);
- const customDomains = await prisma.domain.findMany({
- where: {
- projectId: workspace.id,
- },
- select: {
- slug: true,
- },
- });
+ const [redisRes, prismaRes] = await Promise.allSettled([
+ linkCache.expireMany(defaultDomainLinks),
+ prisma.link.updateMany({
+ where: {
+ id: {
+ in: defaultDomainLinks.map((link) => link.id),
+ },
+ },
+ data: {
+ projectId: LEGAL_WORKSPACE_ID,
+ userId: LEGAL_USER_ID,
+ },
+ }),
+ ]);
// delete all domains, links, and uploaded images associated with the workspace
const deleteDomainsLinksResponse = await Promise.allSettled(
@@ -112,7 +129,8 @@ export async function deleteWorkspaceAdmin(
]);
return {
- updateLinkPrismaResponse,
+ redisRes,
+ prismaRes,
deleteDomainsLinksResponse,
deleteWorkspaceResponse,
};
diff --git a/apps/web/scripts/ban-links.ts b/apps/web/scripts/ban-links.ts
index fa2a273764..838d42a146 100644
--- a/apps/web/scripts/ban-links.ts
+++ b/apps/web/scripts/ban-links.ts
@@ -1,27 +1,44 @@
import { prisma } from "@dub/prisma";
import { LEGAL_USER_ID, LEGAL_WORKSPACE_ID } from "@dub/utils";
import "dotenv-flow/config";
+import { linkCache } from "../lib/api/links/cache";
async function main() {
- const links = await prisma.link.updateMany({
+ const links = await prisma.link.findMany({
where: {
domain: "dub.sh",
url: {
- contains: "secure329.inmotionhosting.com",
+ contains: "kurtdavisjr.com",
+ },
+ projectId: {
+ not: LEGAL_WORKSPACE_ID,
+ },
+ },
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ },
+ take: 1000,
+ });
+
+ const redisRes = await linkCache.expireMany(links);
+
+ const prismaRes = await prisma.link.updateMany({
+ where: {
+ id: {
+ in: links.map((link) => link.id),
},
},
- // select: {
- // shortLink: true,
- // url: true,
- // projectId: true,
- // userId: true,
- // },
data: {
projectId: LEGAL_WORKSPACE_ID,
userId: LEGAL_USER_ID,
},
});
+
console.table(links);
+ console.table(redisRes);
+ console.table(prismaRes);
}
main();