Skip to content

Commit 3d0e484

Browse files
committed
stats details
1 parent 97ba65e commit 3d0e484

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

cache/middleware.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,49 @@ const invalidateCache = (req, res, next) => {
383383
* Expose cache statistics at /cache/stats endpoint
384384
*/
385385
const cacheStats = async (req, res) => {
386+
const includeDetails = req.query.details === 'true'
386387
const stats = await cache.getStats()
388+
389+
if (includeDetails) {
390+
// Get all cache keys for detailed view
391+
try {
392+
const keysMap = await cache.clusterCache.keys()
393+
const allKeys = new Set()
394+
395+
for (const instanceKeys of Object.values(keysMap)) {
396+
if (Array.isArray(instanceKeys)) {
397+
instanceKeys.forEach(key => {
398+
if (!key.startsWith('_stats_worker_')) {
399+
allKeys.add(key)
400+
}
401+
})
402+
}
403+
}
404+
405+
const details = []
406+
let position = 0
407+
for (const key of allKeys) {
408+
const value = await cache.clusterCache.get(key, undefined)
409+
const size = cache._calculateSize(value)
410+
const accessTime = cache.keyAccessTimes.get(key) || 0
411+
const age = accessTime > 0 ? Date.now() - accessTime : 0
412+
413+
details.push({
414+
position,
415+
key,
416+
age: cache._formatUptime(age),
417+
bytes: size
418+
})
419+
position++
420+
}
421+
422+
stats.details = details
423+
} catch (err) {
424+
// If details fetch fails, just return basic stats
425+
stats.detailsError = err.message
426+
}
427+
}
428+
387429
res.status(200).json(stats)
388430
}
389431

0 commit comments

Comments
 (0)