muesli / cache2go

Concurrency-safe Go caching library with expiration capabilities and access counters
Other
2.11k stars 516 forks source link

why not move table.Lock() into table.addInternal()? #12

Closed yiyang5055 closed 7 years ago

yiyang5055 commented 7 years ago
func (table *CacheTable) Add(key interface{}, lifeSpan time.Duration, data interface{}) *CacheItem {
    item := NewCacheItem(key, lifeSpan, data)

    // Add item to cache.
    table.Lock()
    table.addInternal(item)

    return item
}

In general,Lock() and UnLock() is pair in function,why not move table.Lock() into table.addInternal()?Is there anything other to consider?

muesli commented 7 years ago

addInternal is used from multiple places where the table is already locked. For performance reasons we don't want to unlock & relock here.

yiyang5055 commented 7 years ago

yeah, thank you very much!