diff --git a/layeredcache.go b/layeredcache.go index bb57a3c..1027d1b 100644 --- a/layeredcache.go +++ b/layeredcache.go @@ -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 } diff --git a/readme.md b/readme.md index 8a7efa2..bb399db 100644 --- a/readme.md +++ b/readme.md @@ -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.