guard access to item.promotions in LayeredCache, which was applied to Cache in 557d56ec6f

This commit is contained in:
Karl Seguin 2018-12-27 22:51:19 +07:00
parent 142396791e
commit 692cd618b2
2 changed files with 4 additions and 4 deletions

View file

@ -192,7 +192,7 @@ func (c *LayeredCache) worker() {
}
case item := <-c.deletables:
if item.element == nil {
item.promotions = -2
atomic.StoreInt32(&item.promotions, -2)
} else {
c.size -= item.size
if c.onDelete != nil {
@ -206,13 +206,13 @@ func (c *LayeredCache) worker() {
func (c *LayeredCache) doPromote(item *Item) bool {
// deleted before it ever got promoted
if item.promotions == -2 {
if atomic.LoadInt32(&item.promotions) == -2 {
return false
}
if item.element != nil { //not a new item
if item.shouldPromote(c.getsPerPromote) {
c.list.MoveToFront(item.element)
item.promotions = 0
atomic.StoreInt32(&item.promotions, 0)
}
return false
}

View file

@ -122,7 +122,7 @@ user := item.Value() //will be nil if "user:4" didn't exist in the cache
item.Release() //can be called even if item.Value() returned nil
```
In practive, `Release` wouldn't be called until later, at some other place in your code.
In practice, `Release` wouldn't be called until later, at some other place in your code.
There's a couple reason to use the tracking mode if other parts of your code also hold references to objects. First, if you're already going to hold a reference to these objects, there's really no reason not to have them in the cache - the memory is used up anyways.