Files
biblical-guide.com/lib/cache/index.ts
andupetcu 3b375c869b Add complete Biblical Guide web application with Material UI
Implemented comprehensive Romanian Biblical Guide web app:
- Next.js 15 with App Router and TypeScript
- Material UI 7.3.2 for modern, responsive design
- PostgreSQL database with Prisma ORM
- Complete Bible reader with book/chapter navigation
- AI-powered biblical chat with Romanian responses
- Prayer wall for community prayer requests
- Advanced Bible search with filters and highlighting
- Sample Bible data imported from API.Bible
- All API endpoints created and working
- Professional Material UI components throughout
- Responsive layout with navigation and theme

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-20 14:10:28 +03:00

71 lines
1.9 KiB
TypeScript

import { prisma } from '@/lib/db'
export class CacheManager {
static async get(key: string): Promise<string | null> {
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<void> {
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<void> {
try {
await prisma.$executeRaw`
DELETE FROM verse_cache WHERE key LIKE ${pattern}
`
} catch (error) {
console.error('Cache invalidate error:', error)
}
}
static async clear(): Promise<void> {
try {
await prisma.$executeRaw`DELETE FROM verse_cache`
} catch (error) {
console.error('Cache clear error:', error)
}
}
static async cleanup(): Promise<void> {
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: number, 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}`
}
}