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

@@ -10,6 +10,7 @@ const {
const { upload, handleUploadError } = require("../middleware/upload");
const { uploadImage, deleteImage } = require("../config/cloudinary");
const { paginate, buildPaginatedResponse } = require("../middleware/pagination");
const { getCacheMiddleware, invalidateCacheByPattern } = require("../middleware/cache");
const router = express.Router();
@@ -17,6 +18,7 @@ const router = express.Router();
router.get(
"/",
paginate,
getCacheMiddleware(60), // Cache for 1 minute
asyncHandler(async (req, res) => {
const { skip, limit, page } = req.pagination;
@@ -58,6 +60,9 @@ router.post(
const post = await Post.create(postData);
// Invalidate posts cache
invalidateCacheByPattern('/api/posts');
res.json({
post,
pointsAwarded: 5, // Standard post creation points