Skip to content

Commit 3f1f399

Browse files
committed
Clean out /cache/clear route and logic
1 parent fa6e2cf commit 3f1f399

2 files changed

Lines changed: 2 additions & 43 deletions

File tree

cache/middleware.js

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@ const cacheQuery = (req, res, next) => {
3737
// Try to get from cache
3838
const cachedResult = cache.get(cacheKey)
3939
if (cachedResult) {
40-
console.log(`Cache HIT: query`)
4140
res.set("Content-Type", "application/json; charset=utf-8")
4241
res.set('X-Cache', 'HIT')
4342
res.status(200).json(cachedResult)
4443
return
4544
}
46-
47-
console.log(`Cache MISS: query`)
4845
res.set('X-Cache', 'MISS')
4946

5047
// Store original json method
@@ -90,14 +87,11 @@ const cacheSearch = (req, res, next) => {
9087

9188
const cachedResult = cache.get(cacheKey)
9289
if (cachedResult) {
93-
console.log(`Cache HIT: search "${searchText}"`)
9490
res.set("Content-Type", "application/json; charset=utf-8")
9591
res.set('X-Cache', 'HIT')
9692
res.status(200).json(cachedResult)
9793
return
9894
}
99-
100-
console.log(`Cache MISS: search "${searchText}"`)
10195
res.set('X-Cache', 'MISS')
10296

10397
const originalJson = res.json.bind(res)
@@ -139,14 +133,11 @@ const cacheSearchPhrase = (req, res, next) => {
139133

140134
const cachedResult = cache.get(cacheKey)
141135
if (cachedResult) {
142-
console.log(`Cache HIT: search phrase "${searchText}"`)
143136
res.set("Content-Type", "application/json; charset=utf-8")
144137
res.set('X-Cache', 'HIT')
145138
res.status(200).json(cachedResult)
146139
return
147140
}
148-
149-
console.log(`Cache MISS: search phrase "${searchText}"`)
150141
res.set('X-Cache', 'MISS')
151142

152143
const originalJson = res.json.bind(res)
@@ -182,16 +173,13 @@ const cacheId = (req, res, next) => {
182173
const cachedResult = cache.get(cacheKey)
183174

184175
if (cachedResult) {
185-
console.log(`Cache HIT: id ${id}`)
186176
res.set("Content-Type", "application/json; charset=utf-8")
187177
res.set('X-Cache', 'HIT')
188178
// Apply same headers as the original controller
189179
res.set("Cache-Control", "max-age=86400, must-revalidate")
190180
res.status(200).json(cachedResult)
191181
return
192182
}
193-
194-
console.log(`Cache MISS: id ${id}`)
195183
res.set('X-Cache', 'MISS')
196184

197185
const originalJson = res.json.bind(res)
@@ -227,14 +215,11 @@ const cacheHistory = (req, res, next) => {
227215
const cachedResult = cache.get(cacheKey)
228216

229217
if (cachedResult) {
230-
console.log(`Cache HIT: history ${id}`)
231218
res.set("Content-Type", "application/json; charset=utf-8")
232219
res.set('X-Cache', 'HIT')
233220
res.json(cachedResult)
234221
return
235222
}
236-
237-
console.log(`Cache MISS: history ${id}`)
238223
res.set('X-Cache', 'MISS')
239224

240225
const originalJson = res.json.bind(res)
@@ -271,14 +256,11 @@ const cacheSince = (req, res, next) => {
271256
const cachedResult = cache.get(cacheKey)
272257

273258
if (cachedResult) {
274-
console.log(`Cache HIT: since ${id}`)
275259
res.set("Content-Type", "application/json; charset=utf-8")
276260
res.set('X-Cache', 'HIT')
277261
res.json(cachedResult)
278262
return
279263
}
280-
281-
console.log(`Cache MISS: since ${id}`)
282264
res.set('X-Cache', 'MISS')
283265

284266
const originalJson = res.json.bind(res)
@@ -471,21 +453,6 @@ const cacheStats = (req, res) => {
471453
res.status(200).json(response)
472454
}
473455

474-
/**
475-
* Middleware to clear cache at /cache/clear endpoint
476-
* Should be protected in production
477-
*/
478-
const cacheClear = (req, res) => {
479-
const sizeBefore = cache.cache.size
480-
cache.clear()
481-
482-
res.status(200).json({
483-
message: 'Cache cleared',
484-
entriesCleared: sizeBefore,
485-
currentSize: cache.cache.size
486-
})
487-
}
488-
489456
/**
490457
* Cache middleware for GOG fragments endpoint
491458
* Caches POST requests for WitnessFragment entities from ManuscriptWitness
@@ -511,14 +478,11 @@ const cacheGogFragments = (req, res, next) => {
511478

512479
const cachedResponse = cache.get(cacheKey)
513480
if (cachedResponse) {
514-
console.log(`Cache HIT for GOG fragments: ${manID}`)
515481
res.set('X-Cache', 'HIT')
516482
res.set('Content-Type', 'application/json; charset=utf-8')
517483
res.json(cachedResponse)
518484
return
519485
}
520-
521-
console.log(`Cache MISS for GOG fragments: ${manID}`)
522486
res.set('X-Cache', 'MISS')
523487

524488
// Intercept res.json to cache the response
@@ -558,14 +522,11 @@ const cacheGogGlosses = (req, res, next) => {
558522

559523
const cachedResponse = cache.get(cacheKey)
560524
if (cachedResponse) {
561-
console.log(`Cache HIT for GOG glosses: ${manID}`)
562525
res.set('X-Cache', 'HIT')
563526
res.set('Content-Type', 'application/json; charset=utf-8')
564527
res.json(cachedResponse)
565528
return
566529
}
567-
568-
console.log(`Cache MISS for GOG glosses: ${manID}`)
569530
res.set('X-Cache', 'MISS')
570531

571532
// Intercept res.json to cache the response
@@ -590,6 +551,5 @@ export {
590551
cacheGogFragments,
591552
cacheGogGlosses,
592553
invalidateCache,
593-
cacheStats,
594-
cacheClear
554+
cacheStats
595555
}

routes/api-routes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import sinceRouter from './since.js';
4545
// Support GET requests like v1/history/{object id} to discover all previous versions tracing back to the prime.
4646
import historyRouter from './history.js';
4747
// Cache management endpoints
48-
import { cacheStats, cacheClear } from '../cache/middleware.js'
48+
import { cacheStats } from '../cache/middleware.js'
4949

5050
router.use(staticRouter)
5151
router.use('/id',idRouter)
@@ -64,7 +64,6 @@ router.use('/api/unset', unsetRouter)
6464
router.use('/api/release', releaseRouter)
6565
// Cache management endpoints
6666
router.get('/api/cache/stats', cacheStats)
67-
router.post('/api/cache/clear', cacheClear)
6867
// Set default API response
6968
router.get('/api', (req, res) => {
7069
res.json({

0 commit comments

Comments
 (0)