Added Redis for Caching and Started Database Query Caching Migration to Redis

This commit is contained in:
Ahmad 2025-01-18 19:14:19 -05:00
parent f99360d9b9
commit 0f899cb34f
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
12 changed files with 260 additions and 632 deletions

View file

@ -1,6 +1,7 @@
import { auth } from '@clerk/nextjs/server';
import { ENTITY_TYPE } from '@prisma/client';
import { NextResponse } from 'next/server';
import { unstable_cache } from 'next/cache';
import { db } from '@/lib/db';
@ -17,21 +18,28 @@ export async function GET(
status: 401,
});
const auditLogs = await db.auditLog.findMany({
where: {
orgId,
entityId: params.cardId,
entityType: ENTITY_TYPE.CARD,
const getAuditLogs = unstable_cache(
async () => {
return await db.auditLog.findMany({
where: {
orgId,
entityId: params.cardId,
entityType: ENTITY_TYPE.CARD,
},
orderBy: {
createdAt: 'desc',
},
take: 3,
});
},
orderBy: {
createdAt: 'desc',
},
take: 3,
cacheStrategy: {
ttl: 30,
swr: 60,
},
});
[`card-logs-${params.cardId}`],
{
tags: [`card-logs-${params.cardId}`],
revalidate: false,
}
);
const auditLogs = await getAuditLogs();
return new NextResponse(JSON.stringify(auditLogs), {
status: 200,

View file

@ -2,6 +2,7 @@ import { auth } from '@clerk/nextjs/server';
import { NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { unstable_cache } from 'next/cache';
export async function GET(
req: Request,
@ -16,27 +17,34 @@ export async function GET(
status: 401,
});
const card = await db.card.findUnique({
where: {
id: params.cardId,
list: {
board: {
orgId,
const getCard = unstable_cache(
async () => {
return await db.card.findUnique({
where: {
id: params.cardId,
list: {
board: {
orgId,
},
},
},
},
},
include: {
list: {
select: {
title: true,
include: {
list: {
select: {
title: true,
},
},
},
},
});
},
cacheStrategy: {
ttl: 30,
swr: 60,
},
});
[`card-${params.cardId}`],
{
tags: [`card-${params.cardId}`],
revalidate: false,
}
);
const card = await getCard();
return new NextResponse(JSON.stringify(card), {
status: 200,