Skip to content

Commit

Permalink
fix: do not use rwmutex for cache lock
Browse files Browse the repository at this point in the history
Has() modifies counter & getEntry() counter and entriesList, so they are not read only
  • Loading branch information
ydylla committed Sep 18, 2022
1 parent 72e9013 commit e7856bd
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type cache struct {
deletes int64
evictions int64

lock sync.RWMutex
lock sync.Mutex
sequence uint64
entriesList *list.List
entriesMap map[uint64]*list.Element
Expand All @@ -68,8 +68,8 @@ type cache struct {
var _ Cache = (*cache)(nil)

func (c *cache) Stats() Stats {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()

return Stats{
Items: c.entriesList.Len(),
Expand All @@ -85,10 +85,10 @@ func (c *cache) Stats() Stats {
}

func (c *cache) Has(key uint64) (*EntryInfo, error) {
c.lock.RLock()
c.lock.Lock()
item, ok := c.entriesMap[key]
c.has += 1
c.lock.RUnlock()
c.lock.Unlock()

if ok {
entry := item.Value.(*cacheEntry)
Expand Down Expand Up @@ -300,8 +300,8 @@ func (c *cache) buildEntryPath(entry *cacheEntry) string {

func (c *cache) getEntry(key uint64, lock bool) *cacheEntry {
if lock {
c.lock.RLock()
defer c.lock.RUnlock()
c.lock.Lock()
defer c.lock.Unlock()
}

c.gets += 1
Expand Down

0 comments on commit e7856bd

Please sign in to comment.