From 1420786345995349bab9534f0a47a3878c2d04b4 Mon Sep 17 00:00:00 2001 From: Matthew Kanwisher Date: Thu, 16 May 2013 17:38:53 -0400 Subject: [PATCH] use multi reader locks --- cache.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index 7e3877f..5e053e0 100644 --- a/cache.go +++ b/cache.go @@ -76,7 +76,7 @@ type Cache struct { } type cache struct { - sync.Mutex + sync.RWMutex defaultExpiration time.Duration items map[string]*item janitor *janitor @@ -139,9 +139,9 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error { // Get an item from the cache. Returns the item or nil, and a bool indicating // whether the key was found. func (c *cache) Get(k string) (interface{}, bool) { - c.Lock() + c.RLock() x, found := c.get(k) - c.Unlock() + c.RUnlock() return x, found } @@ -937,9 +937,9 @@ func (c *cache) LoadFile(fname string) error { // Returns the number of items in the cache. This may include items that have // expired, but have not yet been cleaned up. func (c *cache) ItemCount() int { - c.Lock() + c.RLock() n := len(c.items) - c.Unlock() + c.RUnlock() return n }