diff --git a/app/api/orgs/[orgId]/contests/[contestId]/route.ts b/app/api/orgs/[orgId]/contests/[contestId]/route.ts index 728b0ff..9dd0aca 100644 --- a/app/api/orgs/[orgId]/contests/[contestId]/route.ts +++ b/app/api/orgs/[orgId]/contests/[contestId]/route.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { NameIdSchema, updateContestSchema } from "@/lib/validations"; import { getOrgIdFromNameId } from "@/app/api/service"; import { deleteContest, getContestByNameId, updateContest } from "../service"; +import { invalidateCacheKey } from "@/lib/cache/utils"; export async function GET( _request: NextRequest, @@ -47,6 +48,7 @@ export async function PATCH( NameIdSchema.parse(params.contestId), data, ); + await invalidateCacheKey(`contest:${orgId}:${params.contestId}`); return NextResponse.json(contest); } catch (error) { if (error instanceof z.ZodError) { @@ -77,6 +79,7 @@ export async function DELETE( orgId, NameIdSchema.parse(params.contestId), ); + await invalidateCacheKey(`contest:${orgId}:${params.contestId}`); return NextResponse.json(contest); } catch (error) { if (error instanceof z.ZodError) { diff --git a/app/api/orgs/[orgId]/problems/[problemId]/route.ts b/app/api/orgs/[orgId]/problems/[problemId]/route.ts index 528c04f..f6cff2c 100644 --- a/app/api/orgs/[orgId]/problems/[problemId]/route.ts +++ b/app/api/orgs/[orgId]/problems/[problemId]/route.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { NameIdSchema, updateProblemSchema } from "@/lib/validations"; import { getOrgIdFromNameId } from "@/app/api/service"; import * as problemService from "./service"; +import { invalidateCacheKey } from "@/lib/cache/utils"; export async function GET( _req: NextRequest, @@ -47,6 +48,7 @@ export async function PATCH( problemCode, data, ); + await invalidateCacheKey(`org:problem:${orgId}:${params.problemId}`); return NextResponse.json(problem); } catch (error) { if (error instanceof z.ZodError) { @@ -76,6 +78,7 @@ export async function DELETE( const problemCode = NameIdSchema.parse(params.problemId); await problemService.deleteProblem(orgId, problemCode); + await invalidateCacheKey(`org:problem:${orgId}:${params.problemId}`); return new Response(null, { status: 204 }); } catch (error) { if (error instanceof z.ZodError) { diff --git a/app/api/orgs/[orgId]/problems/route.ts b/app/api/orgs/[orgId]/problems/route.ts index f00ee33..2dfa4f4 100644 --- a/app/api/orgs/[orgId]/problems/route.ts +++ b/app/api/orgs/[orgId]/problems/route.ts @@ -5,6 +5,7 @@ import { NextRequest, NextResponse } from "next/server"; import { NameIdSchema } from "@/app/api/types"; import { problemSchema } from "@/lib/validations"; import { z } from "zod"; +import { invalidateCacheKey } from "@/lib/cache/utils"; export async function GET( _req: NextRequest, @@ -52,6 +53,8 @@ export async function POST( validatedTestCases, ); + await invalidateCacheKey(`org:problems:${orgId}`); + return NextResponse.json(problem, { status: 201 }); } catch (error) { if (error instanceof z.ZodError) { diff --git a/app/api/orgs/[orgId]/route.ts b/app/api/orgs/[orgId]/route.ts index 38d27d8..092dc5f 100644 --- a/app/api/orgs/[orgId]/route.ts +++ b/app/api/orgs/[orgId]/route.ts @@ -5,6 +5,7 @@ import { eq } from "drizzle-orm"; import { z } from "zod"; import { getOrgIdFromNameId } from "../../service"; import { getOrgByOrgId } from "./service"; +import { invalidateCacheKey } from "@/lib/cache/utils"; const updateOrgSchema = z.object({ name: z.string().optional(), @@ -54,6 +55,8 @@ export async function PATCH( .where(eq(orgs.id, orgId)) .returning(); + await invalidateCacheKey(`org:${orgId}`); + return updatedOrg ? NextResponse.json(updatedOrg) : NextResponse.json({ message: "Organization not found" }, { status: 404 }); diff --git a/lib/cache/utils.ts b/lib/cache/utils.ts index 95e9216..8fb0cc2 100644 --- a/lib/cache/utils.ts +++ b/lib/cache/utils.ts @@ -43,3 +43,8 @@ export async function withDataCache( throw error; } } + +export async function invalidateCacheKey(key: string): Promise { + const redis = getRedis(); + return redis.del(key); +}