Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Prisma Accelerate caching to some of our queries #22

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 },
Copy link
Contributor Author

@stefl stefl Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern is to add a TTL and SWR setting to each request that looks cacheable

});

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 },
stefl marked this conversation as resolved.
Show resolved Hide resolved
});
}

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
2 changes: 1 addition & 1 deletion packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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_*****
Expand Down
Loading