Skip to content

Commit

Permalink
feat: adding cache to activities api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bensigo committed Oct 4, 2023
1 parent cb37171 commit 602ceba
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/server/api/routers/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { queryOpenAi } from "@/utils/openai";
import { DailyQuestActivity, Level } from "@prisma/client";
import { getLevel } from "../utils";
import { createChat } from "./chat";
import { kv } from "@vercel/kv";

interface Activity {
title: string;
Expand All @@ -27,6 +28,17 @@ export const activityRouter = createTRPCRouter({
try {
const prisma = ctx.prisma;

// check for cache
const cacheKey = `${ctx.session.user.id}_affirmation`
const cacheData = await kv.get<string>(cacheKey);

console.log({ cacheData }) // log for texting

if(cacheData) {
const data = (JSON.parse(cacheData) )as Activity[];
return data
}

// const quest = await ctx.prisma.quest.findUnique({ where: { id: input.questId }, include: { goal: true }})
const quest = await prisma.quest.findFirst({
where: { id : input.questId },
Expand Down Expand Up @@ -56,6 +68,12 @@ export const activityRouter = createTRPCRouter({
}
const activities = parseActivities(rawActivitiyResponse);
console.log({ activities })

// set cache data
const currentCache = JSON.stringify(activities)
await kv.set(cacheKey, currentCache, {
ex: 86400
})

return activities;
}
Expand Down Expand Up @@ -245,6 +263,8 @@ export const activityRouter = createTRPCRouter({
type: z.enum(dailyActivityEnum)
})).query(async ({ ctx, input }) => {



const activity = ctx.prisma.dailyActivity.findFirst({
where: {
type: input.type as DailyQuestActivity,
Expand Down

0 comments on commit 602ceba

Please sign in to comment.