Adding support for OnDelete callback function

OnDelete will receive an item that is being processed for deletion to support calling cleanup function specific to the item stored
This commit is contained in:
Alexej Kubarev
2018-07-16 18:20:17 +02:00
parent 72059a01e9
commit 7421e2d7b4
5 changed files with 66 additions and 1 deletions

View File

@@ -23,6 +23,28 @@ func (_ CacheTests) DeletesAValue() {
Expect(cache.Get("worm").Value()).To.Equal("sand")
}
func (_ CacheTests) OnDeleteCallbackCalled() {
onDeleteFnCalled := false
onDeleteFn := func(item *Item) {
if item.key == "spice" {
onDeleteFnCalled = true
}
}
cache := New(Configure().OnDelete(onDeleteFn))
cache.Set("spice", "flow", time.Minute)
cache.Set("worm", "sand", time.Minute)
time.Sleep(time.Millisecond * 10) // Run once to init
cache.Delete("spice")
time.Sleep(time.Millisecond * 10) // Wait for worker to pick up deleted items
Expect(cache.Get("spice")).To.Equal(nil)
Expect(cache.Get("worm").Value()).To.Equal("sand")
Expect(onDeleteFnCalled).To.Equal(true)
}
func (_ CacheTests) FetchesExpiredItems() {
cache := New(Configure())
fn := func() (interface{}, error) { return "moo-moo", nil }