diff --git a/cache.go b/cache.go index da08b41..7d9f72a 100644 --- a/cache.go +++ b/cache.go @@ -131,12 +131,20 @@ func (c *Cache) worker() { c.gc() } case item := <-c.deletables: - c.list.Remove(item.element) + if item.element == nil { + item.promotions = -2 + } else { + c.list.Remove(item.element) + } } } } func (c *Cache) doPromote(item *Item) bool { + //already deleted + if item.promotions == -2 { + return false + } item.promotions = 0 if item.element != nil { //not a new item c.list.MoveToFront(item.element) diff --git a/cache_test.go b/cache_test.go index 01cde6f..190373f 100644 --- a/cache_test.go +++ b/cache_test.go @@ -13,6 +13,15 @@ func Test_Cache(t *testing.T) { Expectify(new(CacheTests), t) } +func (c *CacheTests) DeletesAValue() { + cache := New(Configure()) + cache.Set("spice", "flow", time.Minute) + cache.Set("worm", "sand", time.Minute) + cache.Delete("spice") + Expect(cache.Get("spice")).To.Equal(nil) + Expect(cache.Get("worm").(string)).To.Equal("sand") +} + func (c *CacheTests) GCsTheOldestItems() { cache := New(Configure().ItemsToPrune(10)) for i := 0; i < 500; i++ {