File tree Expand file tree Collapse file tree 5 files changed +113
-2
lines changed Expand file tree Collapse file tree 5 files changed +113
-2
lines changed Original file line number Diff line number Diff line change @@ -520,6 +520,32 @@ Returns a JSON object with the following schema:
520
520
}
521
521
```
522
522
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
+
523
549
## Testing
524
550
525
551
```bash
Original file line number Diff line number Diff line change @@ -362,4 +362,35 @@ describe('API Routes', () => {
362
362
expect ( res . headers [ 'timing-allow-origin' ] ) . toEqual ( '*' ) ;
363
363
} ) ;
364
364
} ) ;
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
+ } ) ;
365
396
} ) ;
Original file line number Diff line number Diff line change @@ -51,7 +51,7 @@ const getController = async (name) => {
51
51
// Helper function to set CORS headers
52
52
const setCORSHeaders = ( res ) => {
53
53
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' ) ;
55
55
res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type, Timing-Allow-Origin' ) ;
56
56
res . setHeader ( 'Access-Control-Max-Age' , '86400' ) ;
57
57
} ;
@@ -163,6 +163,11 @@ const handleRequest = async (req, res) => {
163
163
const { getCacheStats } = await import ( './utils/controllerHelpers.js' ) ;
164
164
const stats = getCacheStats ( ) ;
165
165
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 ) ;
166
171
} else {
167
172
// 404 Not Found
168
173
res . statusCode = 404 ;
Original file line number Diff line number Diff line change @@ -318,6 +318,31 @@ const validateTechnologyArray = (technologyParam) => {
318
318
}
319
319
} ;
320
320
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
+
321
346
export {
322
347
REQUIRED_PARAMS ,
323
348
FIRESTORE_IN_LIMIT ,
@@ -331,5 +356,6 @@ export {
331
356
setCachedQueryResult ,
332
357
getCacheStats ,
333
358
executeQuery ,
334
- validateTechnologyArray
359
+ validateTechnologyArray ,
360
+ resetCache
335
361
} ;
Original file line number Diff line number Diff line change @@ -116,4 +116,27 @@ test_endpoint "/v1/page-weight" "?technology=WordPress&geo=ALL&rank=ALL&start=la
116
116
# Test audits endpoint
117
117
test_endpoint " /v1/audits" " ?technology=WordPress&geo=ALL&rank=ALL&start=latest"
118
118
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
+
119
142
echo " API tests complete! All endpoints returned 200 status code and CORS is properly configured."
You can’t perform that action at this time.
0 commit comments