Skip to content

Commit 9d2b5ec

Browse files
authored
Merge pull request #121 from codegasms/fix/cache-not-invalidating
Fix cache not invalidated after a call to POST, PUT and DELETE routes
2 parents c503f6f + d4f4e1f commit 9d2b5ec

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

app/api/orgs/[orgId]/contests/[contestId]/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { z } from "zod";
33
import { NameIdSchema, updateContestSchema } from "@/lib/validations";
44
import { getOrgIdFromNameId } from "@/app/api/service";
55
import { deleteContest, getContestByNameId, updateContest } from "../service";
6+
import { invalidateCacheKey } from "@/lib/cache/utils";
67

78
export async function GET(
89
_request: NextRequest,
@@ -47,6 +48,7 @@ export async function PATCH(
4748
NameIdSchema.parse(params.contestId),
4849
data,
4950
);
51+
await invalidateCacheKey(`contest:${orgId}:${params.contestId}`);
5052
return NextResponse.json(contest);
5153
} catch (error) {
5254
if (error instanceof z.ZodError) {
@@ -77,6 +79,7 @@ export async function DELETE(
7779
orgId,
7880
NameIdSchema.parse(params.contestId),
7981
);
82+
await invalidateCacheKey(`contest:${orgId}:${params.contestId}`);
8083
return NextResponse.json(contest);
8184
} catch (error) {
8285
if (error instanceof z.ZodError) {

app/api/orgs/[orgId]/problems/[problemId]/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { z } from "zod";
33
import { NameIdSchema, updateProblemSchema } from "@/lib/validations";
44
import { getOrgIdFromNameId } from "@/app/api/service";
55
import * as problemService from "./service";
6+
import { invalidateCacheKey } from "@/lib/cache/utils";
67

78
export async function GET(
89
_req: NextRequest,
@@ -47,6 +48,7 @@ export async function PATCH(
4748
problemCode,
4849
data,
4950
);
51+
await invalidateCacheKey(`org:problem:${orgId}:${params.problemId}`);
5052
return NextResponse.json(problem);
5153
} catch (error) {
5254
if (error instanceof z.ZodError) {
@@ -76,6 +78,7 @@ export async function DELETE(
7678
const problemCode = NameIdSchema.parse(params.problemId);
7779

7880
await problemService.deleteProblem(orgId, problemCode);
81+
await invalidateCacheKey(`org:problem:${orgId}:${params.problemId}`);
7982
return new Response(null, { status: 204 });
8083
} catch (error) {
8184
if (error instanceof z.ZodError) {

app/api/orgs/[orgId]/problems/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { NextRequest, NextResponse } from "next/server";
55
import { NameIdSchema } from "@/app/api/types";
66
import { problemSchema } from "@/lib/validations";
77
import { z } from "zod";
8+
import { invalidateCacheKey } from "@/lib/cache/utils";
89

910
export async function GET(
1011
_req: NextRequest,
@@ -52,6 +53,8 @@ export async function POST(
5253
validatedTestCases,
5354
);
5455

56+
await invalidateCacheKey(`org:problems:${orgId}`);
57+
5558
return NextResponse.json(problem, { status: 201 });
5659
} catch (error) {
5760
if (error instanceof z.ZodError) {

app/api/orgs/[orgId]/route.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { eq } from "drizzle-orm";
55
import { z } from "zod";
66
import { getOrgIdFromNameId } from "../../service";
77
import { getOrgByOrgId } from "./service";
8+
import { invalidateCacheKey } from "@/lib/cache/utils";
89

910
const updateOrgSchema = z.object({
1011
name: z.string().optional(),
@@ -54,6 +55,8 @@ export async function PATCH(
5455
.where(eq(orgs.id, orgId))
5556
.returning();
5657

58+
await invalidateCacheKey(`org:${orgId}`);
59+
5760
return updatedOrg
5861
? NextResponse.json(updatedOrg)
5962
: NextResponse.json({ message: "Organization not found" }, { status: 404 });

lib/cache/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ export async function withDataCache<T>(
4343
throw error;
4444
}
4545
}
46+
47+
export async function invalidateCacheKey(key: string): Promise<number> {
48+
const redis = getRedis();
49+
return redis.del(key);
50+
}

0 commit comments

Comments
 (0)