@@ -9,6 +9,8 @@ import { urlFor } from '../sanity/imageUrl';
|
||||
import { theaterHomepageQuery } from '../sanity/queries';
|
||||
import { MapPin } from '@lucide/astro';
|
||||
|
||||
export const prerender = false
|
||||
|
||||
const content = await sanityClient.fetch(theaterHomepageQuery) ?? {};
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
interface CacheEntry {
|
||||
data: unknown;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
const store = new Map<string, CacheEntry>();
|
||||
const DEFAULT_TTL = 60 * 1000; // 1 minute
|
||||
|
||||
function cacheKey(query: string, params: Record<string, unknown>): string {
|
||||
const p = Object.keys(params).sort().map(k => `${k}=${JSON.stringify(params[k])}`).join('&');
|
||||
return `${query}${p ? `?${p}` : ''}`;
|
||||
}
|
||||
|
||||
export function get(key: string): unknown | null {
|
||||
const entry = store.get(key);
|
||||
if (!entry) return null;
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
store.delete(key);
|
||||
return null;
|
||||
}
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
export function set(key: string, data: unknown, ms?: number): void {
|
||||
store.set(key, { data, expiresAt: Date.now() + (ms ?? DEFAULT_TTL) });
|
||||
}
|
||||
+14
-1
@@ -1 +1,14 @@
|
||||
export { sanityClient } from 'sanity:client'
|
||||
import { sanityClient } from 'sanity:client';
|
||||
import { get, set } from './cache';
|
||||
|
||||
export async function fetchWithCache<T>(query: string, params?: Record<string, unknown>): Promise<T | null> {
|
||||
const key = `sanity:${query}:${JSON.stringify(params ?? {})}`;
|
||||
const cached = get(key) as T | null;
|
||||
if (cached) return cached;
|
||||
|
||||
const data = await sanityClient.fetch(query, params);
|
||||
if (data != null) set(key, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
export { sanityClient };
|
||||
|
||||
Reference in New Issue
Block a user