feat: add Redis caching for recipe queries with automatic invalidation
All checks were successful
CI / update (push) Successful in 13s

Implements Redis caching layer for recipe endpoints to reduce MongoDB load and improve response times:

- Install ioredis for Redis client with TypeScript support
- Create cache.ts with namespaced keys (homepage: prefix) to avoid conflicts with other Redis applications
- Add caching to recipe query endpoints (all_brief, by tag, in_season) with 1-hour TTL
- Implement automatic cache invalidation on recipe create/edit/delete operations
- Cache recipes before randomization to maximize cache reuse while maintaining random order per request
- Add graceful fallback to MongoDB if Redis is unavailable
- Update .env.example with Redis configuration (REDIS_HOST, REDIS_PORT)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-07 20:25:07 +01:00
parent bfba0870e3
commit 1628f8ba23
10 changed files with 457 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import { UserFavorites } from '../../../../models/UserFavorites';
import { dbConnect } from '../../../../utils/db';
import type {RecipeModelType} from '../../../../types/types';
import { error } from '@sveltejs/kit';
import { invalidateRecipeCaches } from '$lib/server/cache';
// header: use for bearer token for now
// recipe json in body
export const POST: RequestHandler = async ({request, locals}) => {
@@ -69,6 +70,9 @@ export const POST: RequestHandler = async ({request, locals}) => {
// Delete the recipe
await Recipe.findOneAndDelete({short_name: short_name});
// Invalidate recipe caches after successful deletion
await invalidateRecipeCaches();
return new Response(JSON.stringify({msg: "Deleted recipe successfully"}),{
status: 200,
});