fixed possible nil panic when item is deleted immediately after being added
This commit is contained in:
10
cache.go
10
cache.go
@@ -131,12 +131,20 @@ func (c *Cache) worker() {
|
|||||||
c.gc()
|
c.gc()
|
||||||
}
|
}
|
||||||
case item := <-c.deletables:
|
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 {
|
func (c *Cache) doPromote(item *Item) bool {
|
||||||
|
//already deleted
|
||||||
|
if item.promotions == -2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
item.promotions = 0
|
item.promotions = 0
|
||||||
if item.element != nil { //not a new item
|
if item.element != nil { //not a new item
|
||||||
c.list.MoveToFront(item.element)
|
c.list.MoveToFront(item.element)
|
||||||
|
@@ -13,6 +13,15 @@ func Test_Cache(t *testing.T) {
|
|||||||
Expectify(new(CacheTests), 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() {
|
func (c *CacheTests) GCsTheOldestItems() {
|
||||||
cache := New(Configure().ItemsToPrune(10))
|
cache := New(Configure().ItemsToPrune(10))
|
||||||
for i := 0; i < 500; i++ {
|
for i := 0; i < 500; i++ {
|
||||||
|
Reference in New Issue
Block a user