Skip to content

Commit

Permalink
guard access to item.promotions in LayeredCache, which was applied to…
Browse files Browse the repository at this point in the history
… Cache in 557d56e
  • Loading branch information
Karl Seguin committed Dec 27, 2018
1 parent 1423967 commit 692cd61
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions layeredcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 692cd61

Please sign in to comment.