Fixes #21. Callong OnDelete during gc()
This commit is contained in:
parent
7e55af0a9c
commit
243f5c8219
3 changed files with 15 additions and 4 deletions
3
cache.go
3
cache.go
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -2,9 +2,9 @@ package ccache
|
|||
|
||||
import (
|
||||
. "github.com/karlseguin/expect"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type SecondaryCacheTests struct{}
|
||||
|
|
Loading…
Reference in a new issue