delete by prefix

This commit is contained in:
Eric Loza 2018-08-02 21:12:41 -03:00
parent 9f6ff22cff
commit cc2fc92276
2 changed files with 39 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
@ -947,6 +948,24 @@ func (c *cache) DeleteExpired() {
}
}
// Delete the keys with some prefix from the cache
func (c *cache) DeleteByPrefix(prefix string) {
var evictedItems []keyAndValue
c.mu.Lock()
for k := range c.items {
if strings.HasPrefix(k, prefix) {
ov, evicted := c.delete(k)
if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov})
}
}
}
c.mu.Unlock()
for _, v := range evictedItems {
c.onEvicted(v.key, v.value)
}
}
// Sets an (optional) function that is called with the key and value when an
// item is evicted from the cache. (Including when it is deleted manually, but
// not when it is overwritten.) Set to nil to disable.

View File

@ -1150,6 +1150,26 @@ func TestDelete(t *testing.T) {
}
}
func TestDeleteByPrefix(t *testing.T) {
tc := New(DefaultExpiration, 0)
keys := []string{"foo", "fooa", "foo1", "fooA"}
for _, k := range keys {
tc.Set(k, "value", DefaultExpiration)
}
tc.Set("bar", "value", DefaultExpiration)
tc.DeleteByPrefix("foo")
for _, k := range keys {
x, found := tc.Get(k)
if found || x != nil {
t.Errorf("%s was found, but it should have been deleted\n", k)
}
}
x, found := tc.Get("bar")
if !found || x == nil {
t.Error("bar was not found")
}
}
func TestItemCount(t *testing.T) {
tc := New(DefaultExpiration, 0)
tc.Set("foo", "1", DefaultExpiration)