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:
@@ -8,12 +8,14 @@ const {
|
||||
createStreetValidation,
|
||||
streetIdValidation,
|
||||
} = require("../middleware/validators/streetValidator");
|
||||
const { getCacheMiddleware, invalidateCacheByPattern } = require("../middleware/cache");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Get all streets (with pagination and filtering)
|
||||
router.get(
|
||||
"/",
|
||||
getCacheMiddleware(300), // Cache for 5 minutes
|
||||
asyncHandler(async (req, res) => {
|
||||
const { buildPaginatedResponse } = require("../middleware/pagination");
|
||||
|
||||
@@ -81,6 +83,7 @@ router.get(
|
||||
router.get(
|
||||
"/:id",
|
||||
streetIdValidation,
|
||||
getCacheMiddleware(300), // Cache for 5 minutes
|
||||
asyncHandler(async (req, res) => {
|
||||
const street = await Street.findById(req.params.id);
|
||||
if (!street) {
|
||||
@@ -109,6 +112,9 @@ router.post(
|
||||
location,
|
||||
});
|
||||
|
||||
// Invalidate streets cache
|
||||
invalidateCacheByPattern('/api/streets');
|
||||
|
||||
res.json(street);
|
||||
}),
|
||||
);
|
||||
@@ -156,6 +162,9 @@ router.put(
|
||||
user.stats.streetsAdopted = user.adoptedStreets.length;
|
||||
await user.save();
|
||||
|
||||
// Invalidate streets cache
|
||||
invalidateCacheByPattern('/api/streets');
|
||||
|
||||
// Award points for street adoption using CouchDB service
|
||||
const updatedUser = await couchdbService.updateUserPoints(
|
||||
req.user.id,
|
||||
|
||||
Reference in New Issue
Block a user