Remove the item's mutex. doPromote can only happen in a single goroutine.

Bucket.set has its own lock which would prevent an item from being accessed
by multiple goroutines.
This commit is contained in:
Karl Seguin 2014-10-25 09:44:22 +07:00
parent c626aca486
commit 13c50b1ff5
3 changed files with 4 additions and 11 deletions

View file

@ -17,14 +17,12 @@ func (b *Bucket) get(key string) *Item {
}
func (b *Bucket) set(key string, value interface{}, duration time.Duration) (*Item, bool) {
expires := time.Now().Add(duration)
expires := time.Now().Add(duration).Unix()
b.Lock()
defer b.Unlock()
if existing, exists := b.lookup[key]; exists {
existing.Lock()
existing.value = value
existing.expires = expires.Unix()
existing.Unlock()
existing.expires = expires
return existing, false
}
item := newItem(key, value, expires)

View file

@ -137,8 +137,6 @@ func (c *Cache) worker() {
}
func (c *Cache) doPromote(item *Item) bool {
item.Lock()
defer item.Unlock()
item.promotions = 0
if item.element != nil { //not a new item
c.list.MoveToFront(item.element)

View file

@ -2,9 +2,7 @@ package ccache
import (
"container/list"
"sync"
"sync/atomic"
"time"
)
type TrackedItem interface {
@ -21,7 +19,6 @@ var NilTracked = new(nilItem)
type Item struct {
key string
sync.Mutex
promotions int32
refCount int32
expires int64
@ -29,12 +26,12 @@ type Item struct {
element *list.Element
}
func newItem(key string, value interface{}, expires time.Time) *Item {
func newItem(key string, value interface{}, expires int64) *Item {
return &Item{
key: key,
value: value,
promotions: -1,
expires: expires.Unix(),
expires: expires,
}
}