Ensure onEvicted is run after DeleteLRUAmount, and don't allocate evicted items array if no onEvicted function is registered
This commit is contained in:
parent
78ff45eea0
commit
f73e2280ec
16
cache.go
16
cache.go
@ -1161,10 +1161,9 @@ func (c *cache) DeleteLRU() {
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
var (
|
var (
|
||||||
overCount = c.itemCount() - c.maxItems
|
overCount = c.itemCount() - c.maxItems
|
||||||
evicted []keyAndValue
|
|
||||||
evictFunc = c.onEvicted
|
evictFunc = c.onEvicted
|
||||||
)
|
)
|
||||||
evicted = c.deleteLRUAmount(overCount)
|
evicted := c.deleteLRUAmount(overCount)
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
for _, v := range evicted {
|
for _, v := range evicted {
|
||||||
evictFunc(v.key, v.value)
|
evictFunc(v.key, v.value)
|
||||||
@ -1174,8 +1173,12 @@ func (c *cache) DeleteLRU() {
|
|||||||
// Delete a number of the oldest items from the cache.
|
// Delete a number of the oldest items from the cache.
|
||||||
func (c *cache) DeleteLRUAmount(numItems int) {
|
func (c *cache) DeleteLRUAmount(numItems int) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
evictFunc := c.onEvicted
|
||||||
c.deleteLRUAmount(numItems)
|
evicted := c.deleteLRUAmount(numItems)
|
||||||
|
c.mu.Unlock()
|
||||||
|
for _, v := range evicted {
|
||||||
|
evictFunc(v.key, v.value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cache) deleteLRUAmount(numItems int) []keyAndValue {
|
func (c *cache) deleteLRUAmount(numItems int) []keyAndValue {
|
||||||
@ -1187,9 +1190,12 @@ func (c *cache) deleteLRUAmount(numItems int) []keyAndValue {
|
|||||||
lastItems = make([]string, numItems) // Ring buffer
|
lastItems = make([]string, numItems) // Ring buffer
|
||||||
liCount = 0
|
liCount = 0
|
||||||
full = false
|
full = false
|
||||||
evictedItems = make([]keyAndValue, 0, numItems)
|
evictedItems []keyAndValue
|
||||||
now = time.Now().UnixNano()
|
now = time.Now().UnixNano()
|
||||||
)
|
)
|
||||||
|
if c.onEvicted != nil {
|
||||||
|
evictedItems = make([]keyAndValue, 0, numItems)
|
||||||
|
}
|
||||||
for k, v := range c.items {
|
for k, v := range c.items {
|
||||||
// "Inlining" of !Expired
|
// "Inlining" of !Expired
|
||||||
if v.Expiration == 0 || now <= v.Expiration {
|
if v.Expiration == 0 || now <= v.Expiration {
|
||||||
|
Loading…
Reference in New Issue
Block a user