Skip to content

Commit

Permalink
use RLock instead of Lock in (d *MCache[K, V]) Keys() func
Browse files Browse the repository at this point in the history
  • Loading branch information
knbr13 committed Apr 1, 2024
1 parent 5075bbe commit 4c479f2
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions mcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,17 @@ func (src *MCache[K, V]) CopyTo(dst Cache[K, V]) {

// Keys returns a slice containing the keys of the map in random order.
func (d *MCache[K, V]) Keys() []K {
d.mu.Lock()
defer d.mu.Unlock()
d.mu.RLock()
defer d.mu.RUnlock()

keys := make([]K, len(d.m))
i := len(d.m) - 1
var i uint

for k, v := range d.m {
if v.expireAt != nil && v.expireAt.Before(time.Now()) {
delete(d.m, k)
continue
}
for k := range d.m {
keys[i] = k
i--
i++
}
return keys[i+1:]
return keys
}

// expireKeys is a background goroutine that periodically checks for expired keys and removes them from the database.
Expand Down

0 comments on commit 4c479f2

Please sign in to comment.