lib/cache: add Delete and DeletePrefix methods #4811

This commit is contained in:
Nick Craig-Wood 2020-11-30 11:46:06 +00:00
parent 657be2ace5
commit f4750928ee
2 changed files with 76 additions and 0 deletions

27
lib/cache/cache.go vendored
View file

@ -3,6 +3,7 @@
package cache
import (
"strings"
"sync"
"time"
)
@ -119,6 +120,32 @@ func (c *Cache) GetMaybe(key string) (value interface{}, found bool) {
return entry.value, found
}
// Delete the entry passed in
//
// Returns true if the entry was found
func (c *Cache) Delete(key string) bool {
c.mu.Lock()
_, found := c.cache[key]
delete(c.cache, key)
c.mu.Unlock()
return found
}
// DeletePrefix deletes all entries with the given prefix
//
// Returns number of entries deleted
func (c *Cache) DeletePrefix(prefix string) (deleted int) {
c.mu.Lock()
for k := range c.cache {
if strings.HasPrefix(k, prefix) {
delete(c.cache, k)
deleted++
}
}
c.mu.Unlock()
return deleted
}
// Rename renames the item at oldKey to newKey.
//
// If there was an existing item at newKey then it takes precedence