From f5a8da1443b124bd712f67c0759ebffb3d270be5 Mon Sep 17 00:00:00 2001 From: Anhui-tqhuang Date: Fri, 12 Sep 2025 17:21:53 +0800 Subject: [PATCH] chore: expose cost of cache --- cache.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cache.go b/cache.go index 1cfcaf9..afb5a3e 100644 --- a/cache.go +++ b/cache.go @@ -637,6 +637,16 @@ func (c *Cache[K, V]) Metrics() Metrics { return c.metrics } +// Cost returns the total cost of all items currently stored in the cache. +// Note: Cost tracking is only active when a maximum cost limit is configured. +// If no maximum cost is set, this method will always return 0. +func (c *Cache[K, V]) Cost() uint64 { + c.items.mu.RLock() + defer c.items.mu.RUnlock() + + return c.cost +} + // Start starts an automatic cleanup process that periodically deletes // expired items. // It blocks until Stop is called.