How vishutx/smart-caching-api-system works
Project Type and Purpose
• This is a RESTful microservice (Smart Caching API System) built with Node.js and Express.
• Its primary function is to serve product data from MongoDB, while employing Redis for high-speed caching to reduce latency and database load.
• Key use cases: frequent GET requests for product data, with automatic cache invalidation on writes.
File Structure Analysis
• Top‐level files:
• index.js / server.js: application entry points, bootstrapping Express and middleware.
• .env, config/: environment and connection settings for MongoDB and Redis.
• controllers/: business logic for handling requests (productController.js).
• routes/: routing layer mapping HTTP endpoints to controllers (productRoutes.js).
• middleware/: caching logic (cacheMiddleware.js) inserted into the request pipeline.
• models/: Mongoose schema for Product (Product.js).
• utils/: error handling and input validation.
• __tests__/: unit tests for core functionality.
• package.json / package-lock.json: dependencies (express, mongoose, redis, etc.).
README Insights
• Describes a “read-through” caching strategy:
• GET → check Redis → cache hit or miss.
• On miss: fetch from MongoDB → respond → write to Redis.
• On data mutations (POST/PUT/DELETE), invalidate or update Redis keys.
• RESTful endpoints for CRUD operations on products.
a. Clients / Consumers
• External clients (web apps, mobile apps, other services) issuing HTTP requests.
b. API Server
• Express.js application with routing → controllers → models.
• Middleware layer: cacheMiddleware intercepting GET requests.
• Utils layer for error handling and validation.
c. Cache Layer
• Redis instance storing serialized product data keyed by product ID or query parameters.
d. Database
• MongoDB instance for persistent product storage.
e. Configuration & Infrastructure
• .env/config modules for environment variables and connection setup.
Relationships and Data Flow
1. Client → Express Route (HTTP GET /products/:id).
2. Route → cacheMiddleware.
3. cacheMiddleware → Redis (GET key).
– If cache hit: Redis → middleware → Route → Client.
– If cache miss: middleware → Controller → Model → MongoDB (find).
→ MongoDB → Controller → middleware (to cache) → Redis (SET key) → Response → Client.
4. For POST/PUT/DELETE:
– Client → Route → Controller → Model → MongoDB.
– Controller → Redis (DEL or update key) → Response → Client.
Architectural Patterns and Principles
• Read-through caching pattern.
• Separation of concerns: routing, controllers, middleware, data models.
• Single-responsibility modules: config, utils, tests.
Technologies and Frameworks
• Node.js, Express.js: HTTP server framework.
• Mongoose: ODM for MongoDB.
• Redis client (e.g., ioredis or node-redis).
• Jest or similar for testing (__tests__).
• Environment configuration with dotenv.
• Routes layer → Middleware layer → Controllers → Models.
4. Two paths:
• Cache Hit: Client → Routes → Middleware → Redis → Response → Client.
• Cache Miss: Client → Routes → Middleware → Controller → Model → MongoDB → Controller → Middleware → Redis → Response.
6. Configuration sources and test suite separately.