diff --git a/cache/cache.go b/cache/cache.go index 04d75ef..5dc508f 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -214,26 +214,23 @@ func (mc *MemoryCache) Stats() (entries int, size int64) { }) return count, mc.currentSize } - // GetStats 返回详细的缓存统计信息 func (mc *MemoryCache) GetStats() map[string]interface{} { entries, size := mc.Stats() + utilizationPercent := float64(0) + if mc.maxSize > 0 { + utilizationPercent = float64(size) / float64(mc.maxSize) * 100 + } + return map[string]interface{}{ - "entries": entries, - "size": size, - "max_size": mc.maxSize, - "utilization": float64(size) / float64(mc.maxSize) * 100, - "ttl_seconds": int64(mc.ttl.Seconds()), + "entries": entries, + "size_bytes": size, + "size_mb": float64(size) / 1024 / 1024, + "max_size_bytes": mc.maxSize, + "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 -}