mirror of
https://github.com/spliit-app/spliit.git
synced 2026-03-06 12:36:11 +01:00
Use React’s cache to avoid some queries to the database
This commit is contained in:
17
src/app/cached-functions.ts
Normal file
17
src/app/cached-functions.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { getGroup } from '@/lib/api'
|
||||||
|
import { cache } from 'react'
|
||||||
|
|
||||||
|
function logAndCache<P extends any[], R>(fn: (...args: P) => R) {
|
||||||
|
const cached = cache((...args: P) => {
|
||||||
|
// console.log(`Not cached: ${fn.name}…`)
|
||||||
|
return fn(...args)
|
||||||
|
})
|
||||||
|
return (...args: P) => {
|
||||||
|
// console.log(`Calling cached ${fn.name}…`)
|
||||||
|
return cached(...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const cached = {
|
||||||
|
getGroup: logAndCache(getGroup),
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { BalancesList } from '@/app/groups/[groupId]/balances-list'
|
import { BalancesList } from '@/app/groups/[groupId]/balances-list'
|
||||||
import { ReimbursementList } from '@/app/groups/[groupId]/reimbursement-list'
|
import { ReimbursementList } from '@/app/groups/[groupId]/reimbursement-list'
|
||||||
import {
|
import {
|
||||||
@@ -7,7 +8,7 @@ import {
|
|||||||
CardHeader,
|
CardHeader,
|
||||||
CardTitle,
|
CardTitle,
|
||||||
} from '@/components/ui/card'
|
} from '@/components/ui/card'
|
||||||
import { getGroup, getGroupExpenses } from '@/lib/api'
|
import { getGroupExpenses } from '@/lib/api'
|
||||||
import { getBalances, getSuggestedReimbursements } from '@/lib/balances'
|
import { getBalances, getSuggestedReimbursements } from '@/lib/balances'
|
||||||
import { Metadata } from 'next'
|
import { Metadata } from 'next'
|
||||||
import { notFound } from 'next/navigation'
|
import { notFound } from 'next/navigation'
|
||||||
@@ -21,7 +22,7 @@ export default async function GroupPage({
|
|||||||
}: {
|
}: {
|
||||||
params: { groupId: string }
|
params: { groupId: string }
|
||||||
}) {
|
}) {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
|
|
||||||
const expenses = await getGroupExpenses(groupId)
|
const expenses = await getGroupExpenses(groupId)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { GroupForm } from '@/components/group-form'
|
import { GroupForm } from '@/components/group-form'
|
||||||
import { getGroup, getGroupExpensesParticipants, updateGroup } from '@/lib/api'
|
import { getGroupExpensesParticipants, updateGroup } from '@/lib/api'
|
||||||
import { groupFormSchema } from '@/lib/schemas'
|
import { groupFormSchema } from '@/lib/schemas'
|
||||||
import { Metadata } from 'next'
|
import { Metadata } from 'next'
|
||||||
import { notFound, redirect } from 'next/navigation'
|
import { notFound, redirect } from 'next/navigation'
|
||||||
@@ -13,7 +14,7 @@ export default async function EditGroupPage({
|
|||||||
}: {
|
}: {
|
||||||
params: { groupId: string }
|
params: { groupId: string }
|
||||||
}) {
|
}) {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
|
|
||||||
async function updateGroupAction(values: unknown) {
|
async function updateGroupAction(values: unknown) {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { ExpenseForm } from '@/components/expense-form'
|
import { ExpenseForm } from '@/components/expense-form'
|
||||||
import {
|
import {
|
||||||
deleteExpense,
|
deleteExpense,
|
||||||
getCategories,
|
getCategories,
|
||||||
getExpense,
|
getExpense,
|
||||||
getGroup,
|
|
||||||
updateExpense,
|
updateExpense,
|
||||||
} from '@/lib/api'
|
} from '@/lib/api'
|
||||||
import { expenseFormSchema } from '@/lib/schemas'
|
import { expenseFormSchema } from '@/lib/schemas'
|
||||||
@@ -21,7 +21,7 @@ export default async function EditExpensePage({
|
|||||||
params: { groupId: string; expenseId: string }
|
params: { groupId: string; expenseId: string }
|
||||||
}) {
|
}) {
|
||||||
const categories = await getCategories()
|
const categories = await getCategories()
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
const expense = await getExpense(groupId, expenseId)
|
const expense = await getExpense(groupId, expenseId)
|
||||||
if (!expense) notFound()
|
if (!expense) notFound()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { ExpenseForm } from '@/components/expense-form'
|
import { ExpenseForm } from '@/components/expense-form'
|
||||||
import { createExpense, getCategories, getGroup } from '@/lib/api'
|
import { createExpense, getCategories } from '@/lib/api'
|
||||||
import { expenseFormSchema } from '@/lib/schemas'
|
import { expenseFormSchema } from '@/lib/schemas'
|
||||||
import { Metadata } from 'next'
|
import { Metadata } from 'next'
|
||||||
import { notFound, redirect } from 'next/navigation'
|
import { notFound, redirect } from 'next/navigation'
|
||||||
@@ -15,7 +16,7 @@ export default async function ExpensePage({
|
|||||||
params: { groupId: string }
|
params: { groupId: string }
|
||||||
}) {
|
}) {
|
||||||
const categories = await getCategories()
|
const categories = await getCategories()
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
|
|
||||||
async function createExpenseAction(values: unknown) {
|
async function createExpenseAction(values: unknown) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { ActiveUserModal } from '@/app/groups/[groupId]/expenses/active-user-modal'
|
import { ActiveUserModal } from '@/app/groups/[groupId]/expenses/active-user-modal'
|
||||||
import { ExpenseList } from '@/app/groups/[groupId]/expenses/expense-list'
|
import { ExpenseList } from '@/app/groups/[groupId]/expenses/expense-list'
|
||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
@@ -9,13 +10,15 @@ import {
|
|||||||
CardTitle,
|
CardTitle,
|
||||||
} from '@/components/ui/card'
|
} from '@/components/ui/card'
|
||||||
import { Skeleton } from '@/components/ui/skeleton'
|
import { Skeleton } from '@/components/ui/skeleton'
|
||||||
import { getGroup, getGroupExpenses } from '@/lib/api'
|
import { getGroupExpenses } from '@/lib/api'
|
||||||
import { Download, Plus } from 'lucide-react'
|
import { Download, Plus } from 'lucide-react'
|
||||||
import { Metadata } from 'next'
|
import { Metadata } from 'next'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { notFound } from 'next/navigation'
|
import { notFound } from 'next/navigation'
|
||||||
import { Suspense } from 'react'
|
import { Suspense } from 'react'
|
||||||
|
|
||||||
|
export const revalidate = 3600
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: 'Expenses',
|
title: 'Expenses',
|
||||||
}
|
}
|
||||||
@@ -25,7 +28,7 @@ export default async function GroupExpensesPage({
|
|||||||
}: {
|
}: {
|
||||||
params: { groupId: string }
|
params: { groupId: string }
|
||||||
}) {
|
}) {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -84,7 +87,7 @@ export default async function GroupExpensesPage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function Expenses({ groupId }: { groupId: string }) {
|
async function Expenses({ groupId }: { groupId: string }) {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
const expenses = await getGroupExpenses(group.id)
|
const expenses = await getGroupExpenses(group.id)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
|
import { cached } from '@/app/cached-functions'
|
||||||
import { GroupTabs } from '@/app/groups/[groupId]/group-tabs'
|
import { GroupTabs } from '@/app/groups/[groupId]/group-tabs'
|
||||||
import { SaveGroupLocally } from '@/app/groups/[groupId]/save-recent-group'
|
import { SaveGroupLocally } from '@/app/groups/[groupId]/save-recent-group'
|
||||||
import { ShareButton } from '@/app/groups/[groupId]/share-button'
|
import { ShareButton } from '@/app/groups/[groupId]/share-button'
|
||||||
import { getGroup } from '@/lib/api'
|
|
||||||
import { Metadata } from 'next'
|
import { Metadata } from 'next'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { notFound } from 'next/navigation'
|
import { notFound } from 'next/navigation'
|
||||||
@@ -16,7 +16,7 @@ type Props = {
|
|||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params: { groupId },
|
params: { groupId },
|
||||||
}: Props): Promise<Metadata> {
|
}: Props): Promise<Metadata> {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: {
|
title: {
|
||||||
@@ -30,7 +30,7 @@ export default async function GroupLayout({
|
|||||||
children,
|
children,
|
||||||
params: { groupId },
|
params: { groupId },
|
||||||
}: PropsWithChildren<Props>) {
|
}: PropsWithChildren<Props>) {
|
||||||
const group = await getGroup(groupId)
|
const group = await cached.getGroup(groupId)
|
||||||
if (!group) notFound()
|
if (!group) notFound()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -9,7 +9,9 @@ export async function getPrisma() {
|
|||||||
prisma = new PrismaClient()
|
prisma = new PrismaClient()
|
||||||
} else {
|
} else {
|
||||||
if (!(global as any).prisma) {
|
if (!(global as any).prisma) {
|
||||||
;(global as any).prisma = new PrismaClient()
|
;(global as any).prisma = new PrismaClient({
|
||||||
|
// log: [{ emit: 'stdout', level: 'query' }],
|
||||||
|
})
|
||||||
}
|
}
|
||||||
prisma = (global as any).prisma
|
prisma = (global as any).prisma
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user