Skip to content

Commit 1cee430

Browse files
committed
Call OnDelete in LayeredCache.gc
Fixes #71
1 parent 67d3f75 commit 1cee430

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

layeredcache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ func (c *LayeredCache) gc() int {
353353
c.bucket(item.group).delete(item.group, item.key)
354354
c.size -= item.size
355355
c.list.Remove(element)
356+
if c.onDelete != nil {
357+
c.onDelete(item)
358+
}
356359
item.promotions = -2
357360
dropped += 1
358361
}

layeredcache_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,13 @@ func (_ LayeredCacheTests) TrackerDoesNotCleanupHeldInstance() {
227227
}
228228

229229
func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
230-
cache := Layered(Configure().MaxSize(5).ItemsToPrune(1))
230+
onDeleteFnCalled := false
231+
onDeleteFn := func(item *Item) {
232+
if item.key == "a" {
233+
onDeleteFnCalled = true
234+
}
235+
}
236+
cache := Layered(Configure().MaxSize(5).ItemsToPrune(1).OnDelete(onDeleteFn))
231237
cache.Set("xx", "a", 23, time.Minute)
232238
for i := 0; i < 7; i++ {
233239
cache.Set(strconv.Itoa(i), "a", i, time.Minute)
@@ -242,6 +248,7 @@ func (_ LayeredCacheTests) RemovesOldestItemWhenFull() {
242248
Expect(cache.Get("xx", "b").Value()).To.Equal(9001)
243249
Expect(cache.GetDropped()).To.Equal(4)
244250
Expect(cache.GetDropped()).To.Equal(0)
251+
Expect(onDeleteFnCalled).To.Equal(true)
245252
}
246253

247254
func (_ LayeredCacheTests) ResizeOnTheFly() {

0 commit comments

Comments
 (0)