feat: implement API response caching with node-cache

- Add in-memory cache middleware with configurable TTL
- Cache GET endpoints: streets (5min), events (2min), posts (1min), rewards (10min)
- Automatic cache invalidation on POST/PUT/DELETE operations
- Add cache statistics endpoint (GET /api/cache/stats)
- Add cache management endpoint (DELETE /api/cache)
- Cache hit rate tracking and monitoring
- Pattern-based cache invalidation
- Optimized for Raspberry Pi deployment (lightweight in-memory)

🤖 Generated with Claude

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
William Valentin
2025-11-03 13:25:50 -08:00
parent 43c2e76070
commit ae77e30ffb
10 changed files with 301 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ const {
rewardIdValidation,
} = require("../middleware/validators/rewardValidator");
const { paginate, buildPaginatedResponse } = require("../middleware/pagination");
const { getCacheMiddleware, invalidateCacheByPattern } = require("../middleware/cache");
const router = express.Router();
@@ -14,6 +15,7 @@ const router = express.Router();
router.get(
"/",
paginate,
getCacheMiddleware(600), // Cache for 10 minutes
asyncHandler(async (req, res) => {
const { page, limit } = req.pagination;
@@ -37,6 +39,9 @@ router.post(
isPremium,
});
// Invalidate rewards cache
invalidateCacheByPattern('/api/rewards');
res.json(reward);
}),
);