Skip to content

Commit 2383e75

Browse files
committed
cache reset
1 parent 5a0212b commit 2383e75

File tree

5 files changed

+113
-2
lines changed

5 files changed

+113
-2
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,32 @@ Returns a JSON object with the following schema:
520520
}
521521
```
522522
523+
### `POST /v1/cache-reset`
524+
525+
Resets all caches in the API. This endpoint requires a POST request.
526+
527+
```bash
528+
curl --request POST \
529+
--url 'https://{{HOST}}/v1/cache-reset'
530+
```
531+
532+
Returns a JSON object with the following schema:
533+
534+
```json
535+
{
536+
"success": true,
537+
"message": "All caches have been reset",
538+
"before": {
539+
"queryCache": 150,
540+
"dateCache": 12
541+
},
542+
"after": {
543+
"queryCache": 0,
544+
"dateCache": 0
545+
}
546+
}
547+
```
548+
523549
## Testing
524550
525551
```bash

src/__tests__/routes.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,35 @@ describe('API Routes', () => {
362362
expect(res.headers['timing-allow-origin']).toEqual('*');
363363
});
364364
});
365+
366+
describe('Cache Management', () => {
367+
it('should provide cache stats', async () => {
368+
const res = await request(app)
369+
.get('/v1/cache-stats')
370+
.expect(200);
371+
372+
expect(res.body).toHaveProperty('queryCache');
373+
expect(res.body).toHaveProperty('dateCache');
374+
expect(res.body).toHaveProperty('config');
375+
});
376+
377+
it('should reset cache on POST request', async () => {
378+
const res = await request(app)
379+
.post('/v1/cache-reset')
380+
.expect(200);
381+
382+
expect(res.body).toHaveProperty('success', true);
383+
expect(res.body).toHaveProperty('message');
384+
expect(res.body).toHaveProperty('before');
385+
expect(res.body).toHaveProperty('after');
386+
});
387+
388+
it('should handle cache reset OPTIONS request', async () => {
389+
const res = await request(app)
390+
.options('/v1/cache-reset')
391+
.expect(204);
392+
393+
expect(res.headers['access-control-allow-methods']).toContain('POST');
394+
});
395+
});
365396
});

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const getController = async (name) => {
5151
// Helper function to set CORS headers
5252
const setCORSHeaders = (res) => {
5353
res.setHeader('Access-Control-Allow-Origin', '*');
54-
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
54+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
5555
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Timing-Allow-Origin');
5656
res.setHeader('Access-Control-Max-Age', '86400');
5757
};
@@ -163,6 +163,11 @@ const handleRequest = async (req, res) => {
163163
const { getCacheStats } = await import('./utils/controllerHelpers.js');
164164
const stats = getCacheStats();
165165
sendJSONResponse(res, stats);
166+
} else if (pathname === '/v1/cache-reset' && req.method === 'POST') {
167+
// Cache reset endpoint
168+
const { resetCache } = await import('./utils/controllerHelpers.js');
169+
const result = resetCache();
170+
sendJSONResponse(res, result);
166171
} else {
167172
// 404 Not Found
168173
res.statusCode = 404;

src/utils/controllerHelpers.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ const validateTechnologyArray = (technologyParam) => {
318318
}
319319
};
320320

321+
/**
322+
* Reset all caches
323+
* @returns {Object} Reset operation result
324+
*/
325+
const resetCache = () => {
326+
const beforeStats = {
327+
queryCache: queryResultCache.size,
328+
dateCache: latestDateCache.size
329+
};
330+
331+
// Clear both caches
332+
queryResultCache.clear();
333+
latestDateCache.clear();
334+
335+
return {
336+
success: true,
337+
message: 'All caches have been reset',
338+
before: beforeStats,
339+
after: {
340+
queryCache: queryResultCache.size,
341+
dateCache: latestDateCache.size
342+
}
343+
};
344+
};
345+
321346
export {
322347
REQUIRED_PARAMS,
323348
FIRESTORE_IN_LIMIT,
@@ -331,5 +356,6 @@ export {
331356
setCachedQueryResult,
332357
getCacheStats,
333358
executeQuery,
334-
validateTechnologyArray
359+
validateTechnologyArray,
360+
resetCache
335361
};

test-api.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,27 @@ test_endpoint "/v1/page-weight" "?technology=WordPress&geo=ALL&rank=ALL&start=la
116116
# Test audits endpoint
117117
test_endpoint "/v1/audits" "?technology=WordPress&geo=ALL&rank=ALL&start=latest"
118118

119+
# Test cache stats endpoint
120+
echo "Testing cache stats endpoint..."
121+
test_endpoint "/v1/cache-stats" ""
122+
123+
# Test cache reset endpoint
124+
echo "Testing cache reset endpoint..."
125+
echo "Checking cache reset: http://localhost:3000/v1/cache-reset"
126+
response=$(curl -s -w "\n%{http_code}" -X POST "http://localhost:3000/v1/cache-reset")
127+
http_code=$(echo "$response" | tail -n1)
128+
body=$(echo "$response" | sed '$d')
129+
130+
echo "$body" | jq .
131+
echo "Status code: $http_code"
132+
133+
if [[ $http_code -ne 200 ]]; then
134+
echo "Error: Cache reset endpoint returned non-200 status code"
135+
exit 1
136+
fi
137+
138+
echo ""
139+
echo "----------------------"
140+
echo ""
141+
119142
echo "API tests complete! All endpoints returned 200 status code and CORS is properly configured."

0 commit comments

Comments
 (0)