Skip to content

Commit 15370ec

Browse files
committed
Changes from testing
1 parent c05d4d5 commit 15370ec

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

cache/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class LRUCache {
135135

136136
// Check if expired
137137
if (this.isExpired(node)) {
138+
console.log("Expired node will be removed.")
138139
this.delete(key)
139140
this.stats.misses++
140141
return null
@@ -379,7 +380,7 @@ class LRUCache {
379380
...this.stats,
380381
length: this.cache.size,
381382
bytes: Buffer.byteLength(JSON.stringify(this.cache), 'utf8'),
382-
lifespan: readableAge(Date.now() - this.life),
383+
lifespan: this.readableAge(Date.now() - this.life),
383384
maxLength: this.maxLength,
384385
maxBytes: this.maxBytes,
385386
hitRate: `${hitRate}%`,
@@ -400,7 +401,7 @@ class LRUCache {
400401
entries.push({
401402
position,
402403
key: current.key,
403-
age: readableAge(Date.now() - current.timestamp),
404+
age: this.readableAge(Date.now() - current.timestamp),
404405
hits: current.hits,
405406
length: JSON.stringify(current.value).length,
406407
bytes: Buffer.byteLength(JSON.stringify(current.value), 'utf8')
@@ -417,9 +418,10 @@ class LRUCache {
417418
const minutes = Math.floor(seconds / 60)
418419
const hours = Math.floor(minutes / 60)
419420
const days = Math.floor(hours / 24)
420-
parts.push(`${Math.floor(days)} day${Math.floor(dats) !== 1 ? 's' : ''}`)
421-
parts.push(`${hours} hour${hours !== 1 ? 's' : ''}`)
422-
parts.push(`${minutes} minute${minutes !== 1 ? 's' : ''}`)
421+
let parts = []
422+
if (days > 0) parts.push(`${Math.floor(days)} day${Math.floor(days) !== 1 ? 's' : ''}`)
423+
if (hours > 0) parts.push(`${hours} hour${hours !== 1 ? 's' : ''}`)
424+
if (minutes > 0) parts.push(`${minutes} minute${minutes !== 1 ? 's' : ''}`)
423425
parts.push(`${seconds} second${seconds !== 1 ? 's' : ''}`)
424426
return parts.join(", ")
425427
}
@@ -429,7 +431,7 @@ class LRUCache {
429431
// Configuration can be adjusted via environment variables
430432
const CACHE_MAX_LENGTH = parseInt(process.env.CACHE_MAX_LENGTH ?? 1000)
431433
const CACHE_MAX_BYTES = parseInt(process.env.CACHE_MAX_BYTES ?? 1000000000) // 1000 MB
432-
const CACHE_TTL = parseInt(process.env.CACHE_TTL ?? 10000) // 5 minutes default
433-
const cache = new LRUCache(CACHE_MAX_LENGTH, CACHE_TTL)
434-
// Could also export this 'cache' as a instance of the LRUCache Class, but no use case for it yet.
434+
const CACHE_TTL = parseInt(process.env.CACHE_TTL ?? 300000) // 5 minutes default
435+
const cache = new LRUCache(CACHE_MAX_LENGTH, CACHE_MAX_BYTES, CACHE_TTL)
436+
435437
export default cache

cache/middleware.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const cacheQuery = (req, res, next) => {
3535
console.log(`Cache HIT: query`)
3636
res.set("Content-Type", "application/json; charset=utf-8")
3737
res.set('X-Cache', 'HIT')
38-
res.json(cachedResult)
38+
res.status(200).json(cachedResult)
3939
return
4040
}
4141

@@ -54,8 +54,8 @@ const cacheQuery = (req, res, next) => {
5454
return originalJson(data)
5555
}
5656

57-
console.log("CACHE DETAILS")
58-
console.log(cache.getDetails())
57+
console.log("CACHE STATS")
58+
console.log(cache.getStats())
5959
next()
6060
}
6161

@@ -86,7 +86,7 @@ const cacheSearch = (req, res, next) => {
8686
console.log(`Cache HIT: search "${searchText}"`)
8787
res.set("Content-Type", "application/json; charset=utf-8")
8888
res.set('X-Cache', 'HIT')
89-
res.json(cachedResult)
89+
res.status(200).json(cachedResult)
9090
return
9191
}
9292

@@ -101,8 +101,8 @@ const cacheSearch = (req, res, next) => {
101101
return originalJson(data)
102102
}
103103

104-
console.log("CACHE DETAILS")
105-
console.log(cache.getDetails())
104+
console.log("CACHE STATS")
105+
console.log(cache.getStats())
106106
next()
107107
}
108108

@@ -133,7 +133,7 @@ const cacheSearchPhrase = (req, res, next) => {
133133
console.log(`Cache HIT: search phrase "${searchText}"`)
134134
res.set("Content-Type", "application/json; charset=utf-8")
135135
res.set('X-Cache', 'HIT')
136-
res.json(cachedResult)
136+
res.status(200).json(cachedResult)
137137
return
138138
}
139139

@@ -148,8 +148,8 @@ const cacheSearchPhrase = (req, res, next) => {
148148
return originalJson(data)
149149
}
150150

151-
console.log("CACHE DETAILS")
152-
console.log(cache.getDetails())
151+
console.log("CACHE STATS")
152+
console.log(cache.getStats())
153153
next()
154154
}
155155

@@ -176,7 +176,7 @@ const cacheId = (req, res, next) => {
176176
res.set('X-Cache', 'HIT')
177177
// Apply same headers as the original controller
178178
res.set("Cache-Control", "max-age=86400, must-revalidate")
179-
res.json(cachedResult)
179+
res.status(200).json(cachedResult)
180180
return
181181
}
182182

@@ -191,8 +191,8 @@ const cacheId = (req, res, next) => {
191191
return originalJson(data)
192192
}
193193

194-
console.log("CACHE DETAILS")
195-
console.log(cache.getDetails())
194+
console.log("CACHE STATS")
195+
console.log(cache.getStats())
196196
next()
197197
}
198198

@@ -450,7 +450,7 @@ const invalidateCache = (req, res, next) => {
450450
*/
451451
const cacheStats = (req, res) => {
452452
const stats = cache.getStats()
453-
const details = req.query.details === 'true' ? cache.getDetails() : undefined
453+
const details = req.query.details === 'true' ? cache.getStats() : undefined
454454

455455
res.json({
456456
stats,

0 commit comments

Comments
 (0)