Refactor delete() to tell if it really evicted something
Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
This commit is contained in:
parent
5633e08626
commit
325c2f7b87
18
cache.go
18
cache.go
|
@ -906,20 +906,22 @@ func (c *cache) Delete(k string) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
v, evicted := c.delete(k)
|
v, evicted := c.delete(k)
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
if evicted {
|
if c.onEvicted != nil && evicted {
|
||||||
c.onEvicted(k, v)
|
c.onEvicted(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cache) delete(k string) (interface{}, bool) {
|
func (c *cache) delete(k string) (interface{}, bool) {
|
||||||
if c.onEvicted != nil {
|
var ret interface{} = nil
|
||||||
if v, found := c.items[k]; found {
|
var found = false
|
||||||
|
|
||||||
|
if v, ok := c.items[k]; ok {
|
||||||
|
found = true
|
||||||
|
ret = v.Object
|
||||||
delete(c.items, k)
|
delete(c.items, k)
|
||||||
return v.Object, true
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
delete(c.items, k)
|
return ret, found
|
||||||
return nil, false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type keyAndValue struct {
|
type keyAndValue struct {
|
||||||
|
@ -936,7 +938,7 @@ func (c *cache) DeleteExpired() {
|
||||||
// "Inlining" of expired
|
// "Inlining" of expired
|
||||||
if v.Expiration > 0 && now > v.Expiration {
|
if v.Expiration > 0 && now > v.Expiration {
|
||||||
ov, evicted := c.delete(k)
|
ov, evicted := c.delete(k)
|
||||||
if evicted {
|
if c.onEvicted != nil && evicted {
|
||||||
evictedItems = append(evictedItems, keyAndValue{k, ov})
|
evictedItems = append(evictedItems, keyAndValue{k, ov})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue