Skip to content

Commit

Permalink
Add 5 minute cache and 2 minute SWR to common Prisma find calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Aug 28, 2024
1 parent b9bcde5 commit 2a53c89
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cloudinary",
"compat",
"contrib",
"cuid",
"dashify",
"dialog",
"Dialogs",
Expand All @@ -39,6 +40,7 @@
"estruyf",
"fkey",
"fontsource",
"gdrive",
"Geist",
"gleap",
"Hardman",
Expand Down Expand Up @@ -74,6 +76,7 @@
"NDJSON",
"nextjs",
"nocheck",
"Nullability",
"oakai",
"oaknational",
"oaknationalacademy",
Expand Down
1 change: 1 addition & 0 deletions apps/nextjs/src/app/api/aila-download/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async function getHandler(req: Request): Promise<Response> {
gdriveFileId: fileId,
userId,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

if (!lessonExport) {
Expand Down
1 change: 1 addition & 0 deletions apps/nextjs/src/app/api/qd-download/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async function getHandler(req: Request) {
gdriveFileId: fileId,
userId,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

if (!qdExport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function fetchLessonPlanContentById(
where: {
id,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

if (!lessonPlanRecord) {
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/router/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/router/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async function ailaGetOrSaveSnapshot({
orderBy: {
createdAt: "desc",
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

if (!lessonSchema) {
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/router/generations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const generationRouter = router({
current: true,
},
select: { id: true, appId: true },
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

if (!promptAndAppId) {
Expand Down Expand Up @@ -311,6 +312,7 @@ export const generationRouter = router({
where: {
id: flaggedItem.lastGenerationId,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});

await sendQuizFeedbackEmail({
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/router/lesson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/models/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Apps {
},
},
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});
}

Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/models/promptVariants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class PromptVariants {
where: {
hash,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});
if (existing) {
return;
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/models/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Prompts {
current: true,
},
include: { app: true },
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});
}

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/rag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -611,6 +612,7 @@ Thank you and happy classifying!`;
where: {
slug: categorisation.keyStage,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});
}
}
Expand Down Expand Up @@ -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
Expand All @@ -667,6 +670,7 @@ Thank you and happy classifying!`;
where: {
slug: categorisation.subject,
},
cacheStrategy: { ttl: 60 * 5, swr: 60 * 2 },
});
}
}
Expand Down
7 changes: 0 additions & 7 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ model App {
statistics Statistics[]
@@map("apps")
@@cacheStrategy(ttl: 300)
}

model Prompt {
Expand All @@ -62,7 +61,6 @@ model Prompt {
AnswersAndDistractorsForJudgement AnswersAndDistractorsForJudgement[]
@@map("prompts")
@@cacheStrategy(ttl: 300)
}

// Clerk users
Expand Down Expand Up @@ -102,7 +100,6 @@ model KeyStage {
LessonPlan LessonPlan[]
@@map("key_stages")
@@cacheStrategy(ttl: 300)
}

model KeyStageSubject {
Expand All @@ -118,7 +115,6 @@ model KeyStageSubject {
@@unique([keyStageId, subjectId])
@@map("key_stage_subjects")
@@cacheStrategy(ttl: 300)
}

model AilaUserFlag {
Expand Down Expand Up @@ -179,7 +175,6 @@ model Subject {
LessonPlans LessonPlan[]
@@map("subjects")
@@cacheStrategy(ttl: 300)
}

model Lesson {
Expand Down Expand Up @@ -254,7 +249,6 @@ model LessonSummary {
subject Subject? @relation(fields: [subjectId], references: [id], onDelete: Cascade)
@@map("lesson_summaries")
@@cacheStrategy(ttl: 300)
}

model LessonPlan {
Expand Down Expand Up @@ -750,7 +744,6 @@ model Moderation {
lessonSnapshot LessonSnapshot? @relation(fields: [lessonSnapshotId], references: [id], onDelete: Cascade)
@@map("moderations")
@@cacheStrategy(ttl: 300)
}

model LessonSchema {
Expand Down

0 comments on commit 2a53c89

Please sign in to comment.