From b9bcde54e2b744eed66176598386c910a0a9c6d0 Mon Sep 17 00:00:00 2001 From: Stef Lewandowski Date: Wed, 28 Aug 2024 16:49:43 +0100 Subject: [PATCH 1/4] feat: add Prisma Accelerate caching to some of our tables --- packages/db/prisma/schema.prisma | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 012ac0d15..eec24c374 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -37,6 +37,7 @@ model App { statistics Statistics[] @@map("apps") + @@cacheStrategy(ttl: 300) } model Prompt { @@ -61,6 +62,7 @@ model Prompt { AnswersAndDistractorsForJudgement AnswersAndDistractorsForJudgement[] @@map("prompts") + @@cacheStrategy(ttl: 300) } // Clerk users @@ -68,7 +70,7 @@ model Prompt { // but we're storing the derived data as joining against clerk's // API makes analysis and filtering harder // -// n.b. user_id colummns are not currently set as relations +// n.b. user_id columns are not currently set as relations // but will be in future once this is complete model User { id String @id // Defined by clerk, aka user_***** @@ -100,6 +102,7 @@ model KeyStage { LessonPlan LessonPlan[] @@map("key_stages") + @@cacheStrategy(ttl: 300) } model KeyStageSubject { @@ -115,6 +118,7 @@ model KeyStageSubject { @@unique([keyStageId, subjectId]) @@map("key_stage_subjects") + @@cacheStrategy(ttl: 300) } model AilaUserFlag { @@ -175,6 +179,7 @@ model Subject { LessonPlans LessonPlan[] @@map("subjects") + @@cacheStrategy(ttl: 300) } model Lesson { @@ -249,6 +254,7 @@ model LessonSummary { subject Subject? @relation(fields: [subjectId], references: [id], onDelete: Cascade) @@map("lesson_summaries") + @@cacheStrategy(ttl: 300) } model LessonPlan { @@ -744,6 +750,7 @@ model Moderation { lessonSnapshot LessonSnapshot? @relation(fields: [lessonSnapshotId], references: [id], onDelete: Cascade) @@map("moderations") + @@cacheStrategy(ttl: 300) } model LessonSchema { From 2a53c89c10d1fd4cb74caec895f90fc789c49e27 Mon Sep 17 00:00:00 2001 From: Stef Lewandowski Date: Wed, 28 Aug 2024 19:02:49 +0100 Subject: [PATCH 2/4] Add 5 minute cache and 2 minute SWR to common Prisma find calls --- .vscode/settings.json | 3 +++ apps/nextjs/src/app/api/aila-download/route.ts | 1 + apps/nextjs/src/app/api/qd-download/route.ts | 1 + .../src/utils/lessonPlan/fetchLessonPlanContentById.ts | 1 + packages/api/src/router/app.ts | 3 +++ packages/api/src/router/exports.ts | 1 + packages/api/src/router/generations.ts | 2 ++ packages/api/src/router/lesson.ts | 1 + packages/core/src/models/apps.ts | 2 ++ packages/core/src/models/promptVariants.tsx | 2 ++ packages/core/src/models/prompts.ts | 1 + packages/core/src/rag/index.ts | 4 ++++ packages/db/prisma/schema.prisma | 7 ------- 13 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fae165739..e22e8c317 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,6 +26,7 @@ "cloudinary", "compat", "contrib", + "cuid", "dashify", "dialog", "Dialogs", @@ -39,6 +40,7 @@ "estruyf", "fkey", "fontsource", + "gdrive", "Geist", "gleap", "Hardman", @@ -74,6 +76,7 @@ "NDJSON", "nextjs", "nocheck", + "Nullability", "oakai", "oaknational", "oaknationalacademy", diff --git a/apps/nextjs/src/app/api/aila-download/route.ts b/apps/nextjs/src/app/api/aila-download/route.ts index 0ed600103..d39ca7de4 100644 --- a/apps/nextjs/src/app/api/aila-download/route.ts +++ b/apps/nextjs/src/app/api/aila-download/route.ts @@ -115,6 +115,7 @@ async function getHandler(req: Request): Promise { gdriveFileId: fileId, userId, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!lessonExport) { diff --git a/apps/nextjs/src/app/api/qd-download/route.ts b/apps/nextjs/src/app/api/qd-download/route.ts index dafddfb81..bae256c49 100644 --- a/apps/nextjs/src/app/api/qd-download/route.ts +++ b/apps/nextjs/src/app/api/qd-download/route.ts @@ -112,6 +112,7 @@ async function getHandler(req: Request) { gdriveFileId: fileId, userId, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!qdExport) { diff --git a/packages/aila/src/utils/lessonPlan/fetchLessonPlanContentById.ts b/packages/aila/src/utils/lessonPlan/fetchLessonPlanContentById.ts index d407c81ad..830749c07 100644 --- a/packages/aila/src/utils/lessonPlan/fetchLessonPlanContentById.ts +++ b/packages/aila/src/utils/lessonPlan/fetchLessonPlanContentById.ts @@ -14,6 +14,7 @@ export async function fetchLessonPlanContentById( where: { id, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!lessonPlanRecord) { diff --git a/packages/api/src/router/app.ts b/packages/api/src/router/app.ts index 92d98e5bc..70095f11c 100644 --- a/packages/api/src/router/app.ts +++ b/packages/api/src/router/app.ts @@ -47,6 +47,7 @@ export const appRouter = router({ slug: input.appSlug, }, select: { id: true }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!app) { throw new TRPCError({ @@ -161,6 +162,7 @@ export const appRouter = router({ current: true, }, select: { id: true, appId: true }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); const avgGenerationTimeResult = await ctx.prisma.statistics.findMany({ @@ -169,6 +171,7 @@ export const appRouter = router({ promptId, }, select: { name: true, value: true }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); const timingsKeyedByName = Object.fromEntries( diff --git a/packages/api/src/router/exports.ts b/packages/api/src/router/exports.ts index 2548b15e0..8f1a21fe9 100644 --- a/packages/api/src/router/exports.ts +++ b/packages/api/src/router/exports.ts @@ -70,6 +70,7 @@ async function ailaGetOrSaveSnapshot({ orderBy: { createdAt: "desc", }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!lessonSchema) { diff --git a/packages/api/src/router/generations.ts b/packages/api/src/router/generations.ts index dc8a7e1d4..2fa3c65f8 100644 --- a/packages/api/src/router/generations.ts +++ b/packages/api/src/router/generations.ts @@ -119,6 +119,7 @@ export const generationRouter = router({ current: true, }, select: { id: true, appId: true }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!promptAndAppId) { @@ -311,6 +312,7 @@ export const generationRouter = router({ where: { id: flaggedItem.lastGenerationId, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); await sendQuizFeedbackEmail({ diff --git a/packages/api/src/router/lesson.ts b/packages/api/src/router/lesson.ts index 96801e3a0..26c4154a8 100644 --- a/packages/api/src/router/lesson.ts +++ b/packages/api/src/router/lesson.ts @@ -215,6 +215,7 @@ export const lessonRouter = router({ } const transcript = await ctx.prisma.transcript.findFirst({ where: { lessonId: lesson.id, variant: "ORIGINAL" }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!transcript || !transcript.content) { throw new TRPCError({ diff --git a/packages/core/src/models/apps.ts b/packages/core/src/models/apps.ts index a8f566d8d..af1cb2615 100644 --- a/packages/core/src/models/apps.ts +++ b/packages/core/src/models/apps.ts @@ -20,6 +20,7 @@ export class Apps { }, }, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); } @@ -37,6 +38,7 @@ export class Apps { async getSharedContent(shareId: string) { return this.prisma.sharedContent.findFirst({ where: { id: shareId }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); } async getSingleSessionOutput(sessionId: string, userId: string) { diff --git a/packages/core/src/models/promptVariants.tsx b/packages/core/src/models/promptVariants.tsx index d9ad02b29..6ae7c9c11 100644 --- a/packages/core/src/models/promptVariants.tsx +++ b/packages/core/src/models/promptVariants.tsx @@ -31,6 +31,7 @@ export class PromptVariants { where: { hash, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (existing) { return; @@ -45,6 +46,7 @@ export class PromptVariants { const app = await this.prisma.app.findFirstOrThrow({ where: { slug: appSlug }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); const maxVersionRows = (await this.prisma .$queryRaw`select max(version) as max_version from prompts where slug = ${slug}`) as { diff --git a/packages/core/src/models/prompts.ts b/packages/core/src/models/prompts.ts index 1b0a3176c..ec7b7d4b0 100644 --- a/packages/core/src/models/prompts.ts +++ b/packages/core/src/models/prompts.ts @@ -44,6 +44,7 @@ export class Prompts { current: true, }, include: { app: true }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); } diff --git a/packages/core/src/rag/index.ts b/packages/core/src/rag/index.ts index 4bbbc4c76..73c87b127 100644 --- a/packages/core/src/rag/index.ts +++ b/packages/core/src/rag/index.ts @@ -603,6 +603,7 @@ Thank you and happy classifying!`; { slug: { equals: keyStage.toLowerCase(), mode: "insensitive" } }, ], }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!foundKeyStage) { const categorisation = await this.categoriseKeyStageAndSubject(keyStage); @@ -611,6 +612,7 @@ Thank you and happy classifying!`; where: { slug: categorisation.keyStage, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); } } @@ -654,6 +656,7 @@ Thank you and happy classifying!`; { title: { equals: subject.toLowerCase(), mode: "insensitive" } }, ], }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); // If none of that works, fall back to categorising the subject based on free text @@ -667,6 +670,7 @@ Thank you and happy classifying!`; where: { slug: categorisation.subject, }, + cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); } } diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index eec24c374..10e76571d 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -37,7 +37,6 @@ model App { statistics Statistics[] @@map("apps") - @@cacheStrategy(ttl: 300) } model Prompt { @@ -62,7 +61,6 @@ model Prompt { AnswersAndDistractorsForJudgement AnswersAndDistractorsForJudgement[] @@map("prompts") - @@cacheStrategy(ttl: 300) } // Clerk users @@ -102,7 +100,6 @@ model KeyStage { LessonPlan LessonPlan[] @@map("key_stages") - @@cacheStrategy(ttl: 300) } model KeyStageSubject { @@ -118,7 +115,6 @@ model KeyStageSubject { @@unique([keyStageId, subjectId]) @@map("key_stage_subjects") - @@cacheStrategy(ttl: 300) } model AilaUserFlag { @@ -179,7 +175,6 @@ model Subject { LessonPlans LessonPlan[] @@map("subjects") - @@cacheStrategy(ttl: 300) } model Lesson { @@ -254,7 +249,6 @@ model LessonSummary { subject Subject? @relation(fields: [subjectId], references: [id], onDelete: Cascade) @@map("lesson_summaries") - @@cacheStrategy(ttl: 300) } model LessonPlan { @@ -750,7 +744,6 @@ model Moderation { lessonSnapshot LessonSnapshot? @relation(fields: [lessonSnapshotId], references: [id], onDelete: Cascade) @@map("moderations") - @@cacheStrategy(ttl: 300) } model LessonSchema { From e34227ae5cc4df9a74f145960551ab6debd8a120 Mon Sep 17 00:00:00 2001 From: Stef Lewandowski Date: Wed, 28 Aug 2024 19:10:22 +0100 Subject: [PATCH 3/4] Remove a couple of caches that could be dangerous --- packages/api/src/router/generations.ts | 1 - packages/core/src/models/promptVariants.tsx | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/api/src/router/generations.ts b/packages/api/src/router/generations.ts index 2fa3c65f8..1b0a0adbe 100644 --- a/packages/api/src/router/generations.ts +++ b/packages/api/src/router/generations.ts @@ -119,7 +119,6 @@ export const generationRouter = router({ current: true, }, select: { id: true, appId: true }, - cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); if (!promptAndAppId) { diff --git a/packages/core/src/models/promptVariants.tsx b/packages/core/src/models/promptVariants.tsx index 6ae7c9c11..e10d5698f 100644 --- a/packages/core/src/models/promptVariants.tsx +++ b/packages/core/src/models/promptVariants.tsx @@ -46,7 +46,6 @@ export class PromptVariants { const app = await this.prisma.app.findFirstOrThrow({ where: { slug: appSlug }, - cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); const maxVersionRows = (await this.prisma .$queryRaw`select max(version) as max_version from prompts where slug = ${slug}`) as { From e1ae855b3881be6dc4fbcc68c81acb7cff3e002d Mon Sep 17 00:00:00 2001 From: Stef Lewandowski Date: Thu, 29 Aug 2024 10:40:56 +0100 Subject: [PATCH 4/4] Remove caching for Apps.byId --- packages/core/src/models/apps.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/models/apps.ts b/packages/core/src/models/apps.ts index af1cb2615..6b3f552e0 100644 --- a/packages/core/src/models/apps.ts +++ b/packages/core/src/models/apps.ts @@ -20,7 +20,6 @@ export class Apps { }, }, }, - cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 }, }); }