Skip to content

Commit

Permalink
fix cache.go sync.Mutex, use Lock instead of RLock
Browse files Browse the repository at this point in the history
  • Loading branch information
geektutu committed Feb 11, 2020
1 parent a441406 commit a6535ef
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions gee-cache/day2-single-node/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions gee-cache/day3-http-server/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions gee-cache/day4-consistent-hash/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions gee-cache/day5-multi-nodes/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions gee-cache/day6-single-flight/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down
10 changes: 5 additions & 5 deletions gee-cache/day7-proto-buf/geecache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
)

type cache struct {
mu sync.RWMutex
mu sync.Mutex
lru *lru.Cache
cacheBytes int64
}

func (c *cache) add(key string, value ByteView) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
c.lru = lru.New(c.cacheBytes, nil)
}
c.lru.Add(key, value)
}

func (c *cache) get(key string) (value ByteView, ok bool) {
c.mu.RLock()
defer c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
if c.lru == nil {
return
}
Expand Down

0 comments on commit a6535ef

Please sign in to comment.