mirror of
https://github.com/rclone/rclone.git
synced 2026-01-01 08:03:26 +00:00
lib/cache: add Delete and DeletePrefix methods #4811
This commit is contained in:
27
lib/cache/cache.go
vendored
27
lib/cache/cache.go
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user