fixed possible nil panic when item is deleted immediately after being added

This commit is contained in:
Karl Seguin 2014-10-25 12:24:52 +07:00
parent b0e3fca0f6
commit 3a00ce8f0a
2 changed files with 18 additions and 1 deletions

View file

@ -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)

View file

@ -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++ {