use multi reader locks
This commit is contained in:
parent
1fc39f1402
commit
1420786345
10
cache.go
10
cache.go
|
@ -76,7 +76,7 @@ type Cache struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type cache struct {
|
type cache struct {
|
||||||
sync.Mutex
|
sync.RWMutex
|
||||||
defaultExpiration time.Duration
|
defaultExpiration time.Duration
|
||||||
items map[string]*item
|
items map[string]*item
|
||||||
janitor *janitor
|
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
|
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
||||||
// whether the key was found.
|
// whether the key was found.
|
||||||
func (c *cache) Get(k string) (interface{}, bool) {
|
func (c *cache) Get(k string) (interface{}, bool) {
|
||||||
c.Lock()
|
c.RLock()
|
||||||
x, found := c.get(k)
|
x, found := c.get(k)
|
||||||
c.Unlock()
|
c.RUnlock()
|
||||||
return x, found
|
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
|
// Returns the number of items in the cache. This may include items that have
|
||||||
// expired, but have not yet been cleaned up.
|
// expired, but have not yet been cleaned up.
|
||||||
func (c *cache) ItemCount() int {
|
func (c *cache) ItemCount() int {
|
||||||
c.Lock()
|
c.RLock()
|
||||||
n := len(c.items)
|
n := len(c.items)
|
||||||
c.Unlock()
|
c.RUnlock()
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue