celo-poll/lib/cache.ts
2025-12-08 16:31:30 +03:00

49 lines
1.2 KiB
TypeScript

import { unstable_cache } from 'next/cache'
import { redis } from './redis'
export const getCachedLeaderboard = unstable_cache(
async (period: string, category: string) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/leaderboard?period=${period}&category=${category}`)
return response.json()
},
['leaderboard'],
{
revalidate: 300, // 5 minutes
tags: ['leaderboard']
}
)
export const getCachedUserStats = unstable_cache(
async (fid: string) => {
const response = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/user/${fid}/stats`)
return response.json()
},
['user-stats'],
{
revalidate: 60, // 1 minute
tags: ['user-stats']
}
)
export const getCachedPlatformStats = unstable_cache(
async () => {
const response = await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/analytics/platform`)
return response.json()
},
['platform-stats'],
{
revalidate: 300, // 5 minutes
tags: ['platform-stats']
}
)
export async function invalidateCache(tag: string) {
try {
await fetch(`${process.env.NEXT_PUBLIC_APP_URL}/api/revalidate?tag=${tag}`, {
method: 'POST'
})
} catch (error) {
console.error('Cache invalidation error:', error)
}
}