Files
adopt-a-street/backend/routes/cache.js
William Valentin ae77e30ffb 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>
2025-11-03 13:25:50 -08:00

44 lines
920 B
JavaScript

const express = require("express");
const auth = require("../middleware/auth");
const { asyncHandler } = require("../middleware/errorHandler");
const { getCacheStats, clearCache } = require("../middleware/cache");
const router = express.Router();
/**
* GET /api/cache/stats
* Get cache statistics
* @access Private (authenticated users)
*/
router.get(
"/stats",
auth,
asyncHandler(async (req, res) => {
const stats = getCacheStats();
res.json({
status: "operational",
...stats,
timestamp: new Date().toISOString()
});
})
);
/**
* DELETE /api/cache
* Clear all cache
* @access Private (authenticated users - in production should be admin only)
*/
router.delete(
"/",
auth,
asyncHandler(async (req, res) => {
clearCache();
res.json({
msg: "Cache cleared successfully",
timestamp: new Date().toISOString()
});
})
);
module.exports = router;