Skip to content

Dev - #76

Merged
rekabytes merged 2 commits into
mainfrom
dev
Jan 8, 2026
Merged

Dev#76
rekabytes merged 2 commits into
mainfrom
dev

Conversation

@rekabytes

@rekabytes rekabytes commented Jan 8, 2026

Copy link
Copy Markdown
Owner

📝 Description

Brief description of what this PR does.

🔗 Related Issue

Fixes #(issue number)

🏷️ Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🧹 Code refactoring (no functional changes)
  • 🧪 Test update (adding or updating tests)

✅ Checklist

  • I have read the Contributing Guidelines
  • My branch is created from dev (not main)
  • I have run pnpm lint:fix
  • I have run pnpm typecheck
  • I have tested my changes locally
  • My code follows the project's coding standards
  • I have updated documentation (if applicable)

📸 Screenshots (if applicable)

Add screenshots to help explain your changes.

🧪 How to Test

Steps to test this PR:

  1. ...
  2. ...
  3. ...

📝 Additional Notes

Any additional information reviewers should know.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced Redis caching layer for significantly improved API response speeds
    • Session storage now backed by Redis for better reliability
    • Added Redis availability monitoring to system health checks
  • Performance

    • Food and halal data queries now cached for faster results

✏️ Tip: You can customize this high-level summary in your review settings.

Kai and others added 2 commits January 8, 2026 20:22
- Add Redis connection management with graceful degradation
- Add cache service with wrap(), get(), set(), del(), delPattern()
- Add centralized cache key generation and TTL configuration
- Add HTTP response caching middleware for REST API
- Add caching to tRPC food and halal procedures
- Add cache invalidation on mutations
- Migrate session storage to Redis
- Add Redis to docker-compose for local development
- Add health check endpoint with Redis status

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…-caching

Kairul/kal 36 feat add response caching
@coderabbitai

coderabbitai Bot commented Jan 8, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

This PR introduces comprehensive Redis caching infrastructure to the backend, adding Redis service configuration, client management utilities, centralized cache key generation, a high-level caching API, automatic response caching middleware, and cache integration throughout food and halal data routers with mutation-triggered invalidation.

Changes

Cohort / File(s) Summary
Infrastructure Setup
docker/docker-compose.yml, packages/kal-backend/package.json
Adds Redis service to Docker composition with health checks and persistent volume; adds connect-redis and ioredis npm dependencies to backend.
Core Redis Module
packages/kal-backend/src/lib/redis.ts
New module providing singleton Redis client management with connection initialization, health status reporting, graceful degradation on unavailability, and connection cleanup.
Cache Abstraction Layer
packages/kal-backend/src/lib/cache.ts
New high-level caching API wrapping Redis with get/set/del/delPattern operations, cache-aside pattern via wrap, and cache invalidation helpers for foods/halal/user entries/stats/all.
Cache Key Standardization
packages/kal-backend/src/lib/cache-keys.ts
New module defining centralized cache key factory functions for REST and tRPC endpoints, user-scoped data, invalidation patterns, and standardized TTL constants.
Logging Enhancement
packages/kal-backend/src/lib/logger.ts
Adds debug log level and corresponding logger.debug() method for non-production environments.
Caching Middleware
packages/kal-backend/src/middleware/cache-middleware.ts
New middleware enabling automatic GET response caching with route-specific configurations, cache hit/miss detection, and transparent response serialization.
Middleware Integration
packages/kal-backend/src/routers/api.ts
Wires apiCacheMiddleware into router after API key validation.
Server Initialization
packages/kal-backend/src/index.ts
Adds Redis connection on startup, Redis-backed session storage with memory fallback, Redis status in health endpoint, and graceful shutdown handler for Redis cleanup.
Food Router Caching
packages/kal-backend/src/routers/food.ts
Wraps search/all/paginated/categories/stats endpoints with caching; adds cache invalidation on create/delete mutations; normalizes responses with string IDs.
Halal Router Caching
packages/kal-backend/src/routers/halal.ts
Wraps search/all/paginated/brands/categories/byBrand procedures with caching; caches per-cursor/limit/filter combinations; maintains response field mapping.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Middleware as Cache Middleware
    participant Cache as Redis Cache
    participant DB as Database

    rect rgb(200, 220, 255)
    Note over Client,DB: Cache Hit Scenario
    Client->>Middleware: GET /api/foods
    Middleware->>Cache: Check cache key
    Cache-->>Middleware: Return cached data
    Middleware-->>Client: Send cached response
    end

    rect rgb(220, 200, 255)
    Note over Client,DB: Cache Miss Scenario
    Client->>Middleware: GET /api/foods
    Middleware->>Cache: Check cache key
    Cache-->>Middleware: Cache miss (null)
    Middleware->>DB: Query food data
    DB-->>Middleware: Return results
    Middleware->>Cache: Store in cache with TTL
    Middleware-->>Client: Send fresh response
    end
Loading
sequenceDiagram
    participant App as Application
    participant Redis as Redis Client
    participant Storage as Session Store
    participant Monitor as Health Check

    App->>Redis: connectRedis() on startup
    Redis->>Redis: Initialize client with retry
    Redis->>Redis: Ping test
    Redis-->>App: Connected (or null if failed)
    App->>Storage: Initialize Redis session store<br/>(or memory fallback)
    Storage-->>App: Ready

    App->>Monitor: GET /health
    Monitor->>Redis: getRedisHealth()
    Redis->>Redis: Ping for latency
    Redis-->>Monitor: Status + latency
    Monitor-->>App: Health report<br/>(Redis + MongoDB + timestamp)

    App->>App: Mutation (create/delete food)
    App->>Redis: invalidateCache.userFoodEntries(userId)
    Redis->>Redis: Delete matching patterns
    Redis-->>App: Invalidation complete
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly Related PRs

Poem

🐰 A hop through Redis fields so bright,
Caching queries left and right,
Sessions stored where data flows,
Health checks ping as timing goes,
Invalidation clears the way—
Faster meals for every day! 🥕

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c03c8b6 and 4f21251.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (11)
  • docker/docker-compose.yml
  • packages/kal-backend/package.json
  • packages/kal-backend/src/index.ts
  • packages/kal-backend/src/lib/cache-keys.ts
  • packages/kal-backend/src/lib/cache.ts
  • packages/kal-backend/src/lib/logger.ts
  • packages/kal-backend/src/lib/redis.ts
  • packages/kal-backend/src/middleware/cache-middleware.ts
  • packages/kal-backend/src/routers/api.ts
  • packages/kal-backend/src/routers/food.ts
  • packages/kal-backend/src/routers/halal.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@rekabytes
rekabytes merged commit c6a13d8 into main Jan 8, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant