add cache to prisma

This commit is contained in:
2023-12-14 17:42:13 +01:00
parent 268157b249
commit 62f4215c0c
11 changed files with 36 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
export default defineEventHandler(async () => {
return await usePrisma().announcement.findFirst({
orderBy: {
ororderBy: {
createdAt: 'desc',
},
})

View File

@@ -5,6 +5,9 @@ const PostSchema = z.object({ slug: z.string() }).parse
export default defineEventHandler(async (event) => {
const { slug } = await readValidatedBody(event, PostSchema)
return await usePrisma().post.upsert({
cacheStrategy: {
swr: 60 * 5,
},
where: {
slug,
},

View File

@@ -26,6 +26,9 @@ export default defineEventHandler(async (event) => {
}
return await prisma.bookmark.findMany({
cacheStrategy: {
ttl: 60 * 3,
},
where: whereClause,
orderBy: {
name: 'asc',

View File

@@ -3,6 +3,9 @@ import type { CategoryType } from '@prisma/client'
export default defineEventHandler(async (event) => {
const { type } = getQuery<{ type: CategoryType }>(event)
return await usePrisma().category.findMany({
cacheStrategy: {
ttl: 60 * 3,
},
where: {
type,
},

View File

@@ -3,6 +3,9 @@ export default defineEventHandler(async () => {
orderBy: {
createdAt: 'desc',
},
cacheStrategy: {
ttl: 60 * 60 * 24,
},
})
let enabled = true
if (process.env.NODE_ENV === 'development') {

View File

@@ -1,5 +1,9 @@
export default defineEventHandler(async () => {
return await usePrisma().guestbookMessage.findMany({
cacheStrategy: {
ttl: 60 * 60,
swr: 60 * 5,
},
orderBy: {
updatedAt: 'desc',
},

View File

@@ -26,6 +26,9 @@ export default defineEventHandler(async (event) => {
}
return await prisma.talent.findMany({
cacheStrategy: {
ttl: 60 * 3,
},
where: whereClause,
orderBy: {
name: 'asc',

View File

@@ -1,12 +1,14 @@
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '@prisma/client/edge'
import { PrismaClientExtends } from '@prisma/client/scripts/default-index'
import { withAccelerate } from '@prisma/extension-accelerate'
let prisma: PrismaClient | undefined
let prisma: any
export function usePrisma() {
if (!prisma) {
prisma = new PrismaClient({
log: ['warn', 'info', 'error'],
})
}).$extends(withAccelerate())
}
return prisma