更新 cache/cache.go

This commit is contained in:
XOF
2025-12-15 02:43:23 +08:00
parent 9be1bdadcb
commit 1bc606ee00

29
cache/cache.go vendored
View File

@@ -214,26 +214,23 @@ func (mc *MemoryCache) Stats() (entries int, size int64) {
}) })
return count, mc.currentSize return count, mc.currentSize
} }
// GetStats 返回详细的缓存统计信息 // GetStats 返回详细的缓存统计信息
func (mc *MemoryCache) GetStats() map[string]interface{} { func (mc *MemoryCache) GetStats() map[string]interface{} {
entries, size := mc.Stats() entries, size := mc.Stats()
utilizationPercent := float64(0)
if mc.maxSize > 0 {
utilizationPercent = float64(size) / float64(mc.maxSize) * 100
}
return map[string]interface{}{ return map[string]interface{}{
"entries": entries, "entries": entries,
"size": size, "size_bytes": size,
"max_size": mc.maxSize, "size_mb": float64(size) / 1024 / 1024,
"utilization": float64(size) / float64(mc.maxSize) * 100, "max_size_bytes": mc.maxSize,
"ttl_seconds": int64(mc.ttl.Seconds()), "max_size_mb": float64(mc.maxSize) / 1024 / 1024,
"utilization_pct": utilizationPercent,
"ttl_seconds": int64(mc.ttl.Seconds()),
"enabled": mc.maxSize > 0,
} }
} }
func (mc *MemoryCache) Stats() (entries int, size int64) {
count := 0
mc.entries.Range(func(key, value interface{}) bool {
count++
return true
})
return count, mc.currentSize
}