Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,9 +1063,18 @@ func (c *cache) ItemCount() int {

// Delete all items from the cache.
func (c *cache) Flush() {
var oldCache map[string]Item

c.mu.Lock()
if c.onEvicted != nil {
oldCache = c.items
}
c.items = map[string]Item{}
c.mu.Unlock()

for k, v := range oldCache {
c.onEvicted(k, v.Object)
}
}

type janitor struct {
Expand Down
17 changes: 17 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,23 @@ func TestFlush(t *testing.T) {
if x != nil {
t.Error("x is not nil:", x)
}

evictMask := 0
tc.OnEvicted(func(k string, v interface{}) {
if k == "foo" && v.(string) == "bar" {
evictMask |= 0x1
}
if k == "baz" && v.(string) == "yes" {
evictMask |= 0x2
}
})
tc.Set("foo", "bar", DefaultExpiration)
tc.Set("baz", "yes", DefaultExpiration)
tc.Flush()

if evictMask != 0x3 {
t.Error("on evicted on flush fails")
}
}

func TestIncrementOverflowInt(t *testing.T) {
Expand Down