make Clear thread-safe

This commit is contained in:
Karl Seguin
2020-08-16 21:12:47 +08:00
parent 839a17bedb
commit 1189f7f993
3 changed files with 27 additions and 13 deletions

View File

@@ -164,13 +164,11 @@ func (c *LayeredCache) DeleteFunc(primary string, matches func(key string, item
return c.bucket(primary).deleteFunc(primary, matches, c.deletables)
}
//this isn't thread safe. It's meant to be called from non-concurrent tests
// Clears the cache
func (c *LayeredCache) Clear() {
for _, bucket := range c.buckets {
bucket.clear()
}
c.size = 0
c.list = list.New()
done := make(chan struct{})
c.control <- clear{done: done}
<-done
}
func (c *LayeredCache) Stop() {
@@ -249,6 +247,13 @@ func (c *LayeredCache) worker() {
if c.size > c.maxSize {
dropped += c.gc()
}
case clear:
for _, bucket := range c.buckets {
bucket.clear()
}
c.size = 0
c.list = list.New()
msg.done <- struct{}{}
}
}
}