From a6535ef328ae0043721d631cf927c90637a64534 Mon Sep 17 00:00:00 2001 From: gzdaijie Date: Wed, 12 Feb 2020 01:57:07 +0800 Subject: [PATCH] fix cache.go sync.Mutex, use Lock instead of RLock --- gee-cache/day2-single-node/geecache/cache.go | 10 +++++----- gee-cache/day3-http-server/geecache/cache.go | 10 +++++----- gee-cache/day4-consistent-hash/geecache/cache.go | 10 +++++----- gee-cache/day5-multi-nodes/geecache/cache.go | 10 +++++----- gee-cache/day6-single-flight/geecache/cache.go | 10 +++++----- gee-cache/day7-proto-buf/geecache/cache.go | 10 +++++----- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/gee-cache/day2-single-node/geecache/cache.go b/gee-cache/day2-single-node/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day2-single-node/geecache/cache.go +++ b/gee-cache/day2-single-node/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 } diff --git a/gee-cache/day3-http-server/geecache/cache.go b/gee-cache/day3-http-server/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day3-http-server/geecache/cache.go +++ b/gee-cache/day3-http-server/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 } diff --git a/gee-cache/day4-consistent-hash/geecache/cache.go b/gee-cache/day4-consistent-hash/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day4-consistent-hash/geecache/cache.go +++ b/gee-cache/day4-consistent-hash/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 } diff --git a/gee-cache/day5-multi-nodes/geecache/cache.go b/gee-cache/day5-multi-nodes/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day5-multi-nodes/geecache/cache.go +++ b/gee-cache/day5-multi-nodes/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 } diff --git a/gee-cache/day6-single-flight/geecache/cache.go b/gee-cache/day6-single-flight/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day6-single-flight/geecache/cache.go +++ b/gee-cache/day6-single-flight/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 } diff --git a/gee-cache/day7-proto-buf/geecache/cache.go b/gee-cache/day7-proto-buf/geecache/cache.go index 703d033..665c3f3 100644 --- a/gee-cache/day7-proto-buf/geecache/cache.go +++ b/gee-cache/day7-proto-buf/geecache/cache.go @@ -6,14 +6,14 @@ 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) } @@ -21,8 +21,8 @@ func (c *cache) add(key string, value ByteView) { } 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 }