Fixes #21. Callong OnDelete during gc()

This commit is contained in:
Alexej Kubarev 2018-11-25 15:31:09 -08:00
parent 7e55af0a9c
commit 243f5c8219
No known key found for this signature in database
GPG key ID: EB7FF167F615F3BB
3 changed files with 15 additions and 4 deletions

View file

@ -223,6 +223,9 @@ func (c *Cache) gc() {
c.bucket(item.key).delete(item.key)
c.size -= item.size
c.list.Remove(element)
if c.onDelete != nil {
c.onDelete(item)
}
item.promotions = -2
}
element = prev

View file

@ -98,7 +98,14 @@ func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
}
func (_ CacheTests) RemovesOldestItemWhenFull() {
cache := New(Configure().MaxSize(5).ItemsToPrune(1))
onDeleteFnCalled := false
onDeleteFn := func(item *Item) {
if item.key == "0" {
onDeleteFnCalled = true
}
}
cache := New(Configure().MaxSize(5).ItemsToPrune(1).OnDelete(onDeleteFn))
for i := 0; i < 7; i++ {
cache.Set(strconv.Itoa(i), i, time.Minute)
}
@ -106,6 +113,7 @@ func (_ CacheTests) RemovesOldestItemWhenFull() {
Expect(cache.Get("0")).To.Equal(nil)
Expect(cache.Get("1")).To.Equal(nil)
Expect(cache.Get("2").Value()).To.Equal(2)
Expect(onDeleteFnCalled).To.Equal(true)
}
func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {

View file

@ -2,9 +2,9 @@ package ccache
import (
. "github.com/karlseguin/expect"
"strconv"
"testing"
"time"
"strconv"
)
type SecondaryCacheTests struct{}