// Shared chatbot config cache — used by admin/chatbot and chat routes let _cachedAt = 0; let _cachedConfig: Record | null = null; const CACHE_TTL = 5 * 60 * 1000; // 5 minutes export function invalidateChatbotCache() { _cachedAt = 0; _cachedConfig = null; } export function resetCacheTimestamp() { _cachedAt = 0; } export async function getChatbotConfig(): Promise> { const now = Date.now(); if (_cachedConfig && now - _cachedAt < CACHE_TTL) { return _cachedConfig; } // Dynamic import to avoid circular dependency const { db } = await import('@/lib/db'); const rows = await db.siteSetting.findMany({ where: { key: { startsWith: 'chatbot_' }, }, }); const config: Record = {}; for (const row of rows) { config[row.key] = row.value; } _cachedConfig = config; _cachedAt = now; return config; }