guard access to item.promotions in LayeredCache, which was applied to Cache in 557d56ec6f
This commit is contained in:
parent
142396791e
commit
692cd618b2
|
@ -192,7 +192,7 @@ func (c *LayeredCache) worker() {
|
||||||
}
|
}
|
||||||
case item := <-c.deletables:
|
case item := <-c.deletables:
|
||||||
if item.element == nil {
|
if item.element == nil {
|
||||||
item.promotions = -2
|
atomic.StoreInt32(&item.promotions, -2)
|
||||||
} else {
|
} else {
|
||||||
c.size -= item.size
|
c.size -= item.size
|
||||||
if c.onDelete != nil {
|
if c.onDelete != nil {
|
||||||
|
@ -206,13 +206,13 @@ func (c *LayeredCache) worker() {
|
||||||
|
|
||||||
func (c *LayeredCache) doPromote(item *Item) bool {
|
func (c *LayeredCache) doPromote(item *Item) bool {
|
||||||
// deleted before it ever got promoted
|
// deleted before it ever got promoted
|
||||||
if item.promotions == -2 {
|
if atomic.LoadInt32(&item.promotions) == -2 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if item.element != nil { //not a new item
|
if item.element != nil { //not a new item
|
||||||
if item.shouldPromote(c.getsPerPromote) {
|
if item.shouldPromote(c.getsPerPromote) {
|
||||||
c.list.MoveToFront(item.element)
|
c.list.MoveToFront(item.element)
|
||||||
item.promotions = 0
|
atomic.StoreInt32(&item.promotions, 0)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
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.
|
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.
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue