Skip to content

Commit

Permalink
implement c.Keys func
Browse files Browse the repository at this point in the history
  • Loading branch information
knbr13 committed May 22, 2024
1 parent eea362d commit f6c5331
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion mcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f6c5331

Please sign in to comment.