import { prisma } from '@/lib/db' export class CacheManager { static async get(key: string): Promise { try { const result = await prisma.$queryRaw<{ value: string }[]>` SELECT value FROM verse_cache WHERE key = ${key} AND expires_at > NOW() LIMIT 1 ` return result[0]?.value || null } catch (error) { console.error('Cache get error:', error) return null } } static async set(key: string, value: string, ttl: number = 3600): Promise { try { const expiresAt = new Date(Date.now() + ttl * 1000) await prisma.$executeRaw` INSERT INTO verse_cache (key, value, expires_at) VALUES (${key}, ${value}, ${expiresAt}) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, expires_at = EXCLUDED.expires_at ` } catch (error) { console.error('Cache set error:', error) } } static async invalidate(pattern: string): Promise { try { await prisma.$executeRaw` DELETE FROM verse_cache WHERE key LIKE ${pattern} ` } catch (error) { console.error('Cache invalidate error:', error) } } static async clear(): Promise { try { await prisma.$executeRaw`DELETE FROM verse_cache` } catch (error) { console.error('Cache clear error:', error) } } static async cleanup(): Promise { try { await prisma.$executeRaw`DELETE FROM verse_cache WHERE expires_at < NOW()` } catch (error) { console.error('Cache cleanup error:', error) } } // Helper methods for specific cache patterns static getChapterKey(bookId: string, chapterNum: number): string { return `chapter:${bookId}:${chapterNum}` } static getSearchKey(query: string, limit: number): string { return `search:${query.toLowerCase()}:${limit}` } static getUserBookmarksKey(userId: string): string { return `bookmarks:${userId}` } }