- 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>
83 lines
2.9 KiB
JavaScript
83 lines
2.9 KiB
JavaScript
/**
|
|
* Manual test script for cache middleware
|
|
* Run with: node backend/test-cache.js
|
|
*/
|
|
|
|
const { getCacheMiddleware, invalidateCacheByPattern, getCacheStats, clearCache } = require('./middleware/cache');
|
|
|
|
console.log('Testing Cache Middleware\n');
|
|
console.log('=' .repeat(50));
|
|
|
|
// Test 1: Create mock request/response objects
|
|
console.log('\n1. Testing getCacheMiddleware...');
|
|
const mockReq = { originalUrl: '/api/streets?page=1' };
|
|
let responseSent = null;
|
|
const mockRes = {
|
|
json: (data) => {
|
|
responseSent = data;
|
|
console.log(' Response sent:', JSON.stringify(data).substring(0, 50) + '...');
|
|
return mockRes;
|
|
}
|
|
};
|
|
const mockNext = () => console.log(' Next middleware called');
|
|
|
|
// First request - cache miss
|
|
console.log('\n First request (should be cache miss):');
|
|
const middleware = getCacheMiddleware(60); // 60 second TTL
|
|
middleware(mockReq, mockRes, mockNext);
|
|
|
|
// Simulate response
|
|
mockRes.json({ streets: ['Main St', 'Elm St'], count: 2 });
|
|
|
|
// Test 2: Second request - should hit cache
|
|
console.log('\n Second request (should be cache hit):');
|
|
responseSent = null;
|
|
const mockReq2 = { originalUrl: '/api/streets?page=1' };
|
|
const mockRes2 = {
|
|
json: (data) => {
|
|
responseSent = data;
|
|
console.log(' Cached response returned:', JSON.stringify(data).substring(0, 50) + '...');
|
|
return mockRes2;
|
|
}
|
|
};
|
|
middleware(mockReq2, mockRes2, () => console.log(' Should NOT call next!'));
|
|
|
|
// Test 3: Cache statistics
|
|
console.log('\n2. Testing getCacheStats...');
|
|
const stats = getCacheStats();
|
|
console.log(' Cache stats:', JSON.stringify(stats, null, 2));
|
|
|
|
// Test 4: Pattern invalidation
|
|
console.log('\n3. Testing invalidateCacheByPattern...');
|
|
console.log(' Invalidating all /api/streets cache...');
|
|
invalidateCacheByPattern('/api/streets');
|
|
const statsAfterInvalidation = getCacheStats();
|
|
console.log(' Cache stats after invalidation:', JSON.stringify(statsAfterInvalidation, null, 2));
|
|
|
|
// Test 5: Different URL should not be in cache
|
|
console.log('\n4. Testing cache miss for different URL...');
|
|
const mockReq3 = { originalUrl: '/api/events?page=1' };
|
|
const mockRes3 = {
|
|
json: (data) => {
|
|
console.log(' New URL response sent');
|
|
return mockRes3;
|
|
}
|
|
};
|
|
middleware(mockReq3, mockRes3, () => console.log(' Next middleware called (cache miss)'));
|
|
|
|
// Test 6: Clear all cache
|
|
console.log('\n5. Testing clearCache...');
|
|
clearCache();
|
|
const statsAfterClear = getCacheStats();
|
|
console.log(' Cache stats after clear:', JSON.stringify(statsAfterClear, null, 2));
|
|
|
|
console.log('\n' + '='.repeat(50));
|
|
console.log('Cache middleware tests completed!');
|
|
console.log('\nIntegration points:');
|
|
console.log(' - GET /api/streets (5 min cache)');
|
|
console.log(' - GET /api/events (2 min cache)');
|
|
console.log(' - GET /api/posts (1 min cache)');
|
|
console.log(' - GET /api/rewards (10 min cache)');
|
|
console.log(' - GET /api/cache/stats (cache statistics)');
|
|
console.log(' - DELETE /api/cache (clear all cache)');
|