sm
/
cache
1
0
Fork 0

use multi reader locks

This commit is contained in:
Matthew Kanwisher 2013-05-16 17:38:53 -04:00
parent 1fc39f1402
commit 1420786345
1 changed files with 5 additions and 5 deletions

View File

@ -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
}