From 1bc606ee002e75b9bcef1ec86d50565d9c3807ba Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 02:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20cache/cache.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/cache.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) 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 -}