fixed possible nil panic when item is deleted immediately after being added
This commit is contained in:
parent
b0e3fca0f6
commit
3a00ce8f0a
2 changed files with 18 additions and 1 deletions
10
cache.go
10
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)
|
||||
|
|
|
@ -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++ {
|
||||
|
|
Loading…
Reference in a new issue