diff --git a/mcache.go b/mcache.go index a325642..ef33f05 100644 --- a/mcache.go +++ b/mcache.go @@ -202,7 +202,22 @@ func (src *MCache[K, V]) CopyTo(dst *MCache[K, V]) { } } -// Keys returns a slice containing the keys of the map in random order. +func (c *MCache[K, V]) Keys() []K { + c.mu.RLock() + defer c.mu.RUnlock() + + keys := make([]K, 0, c.Count()) + + for k, v := range c.m { + if v.expireAt == nil || !v.expireAt.Before(time.Now()) { + keys = append(keys, k) + } + } + + return keys +} + +// Keys returns a slice containing the keys of the map in random order, including invalid keys if exists. func (c *MCache[K, V]) AllKeys() []K { c.mu.RLock() defer c.mu.RUnlock()