diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2b16e99..2a0f5ac 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -7,3 +7,4 @@ Dustin Sallings Jason Mooberry Sergey Shepelev Alex Edwards +Dmitry Medvedev diff --git a/cache.go b/cache.go index db88d2f..8f95c30 100644 --- a/cache.go +++ b/cache.go @@ -39,7 +39,7 @@ type Cache struct { type cache struct { defaultExpiration time.Duration - items map[string]Item + items sync.Map mu sync.RWMutex onEvicted func(string, interface{}) janitor *janitor @@ -57,14 +57,10 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { if d > 0 { e = time.Now().Add(d).UnixNano() } - c.mu.Lock() - c.items[k] = Item{ + c.items.Store(k, Item{ Object: x, Expiration: e, - } - // TODO: Calls to mu.Unlock are currently not deferred because defer - // adds ~200 ns (as of go1.) - c.mu.Unlock() + }) } func (c *cache) set(k string, x interface{}, d time.Duration) { @@ -75,10 +71,10 @@ func (c *cache) set(k string, x interface{}, d time.Duration) { if d > 0 { e = time.Now().Add(d).UnixNano() } - c.items[k] = Item{ + c.items.Store(k, Item{ Object: x, Expiration: e, - } + }) } // Add an item to the cache, replacing any existing item, using the default @@ -90,48 +86,38 @@ func (c *cache) SetDefault(k string, x interface{}) { // Add an item to the cache only if an item doesn't already exist for the given // key, or if the existing item has expired. Returns an error otherwise. func (c *cache) Add(k string, x interface{}, d time.Duration) error { - c.mu.Lock() _, found := c.get(k) if found { - c.mu.Unlock() return fmt.Errorf("Item %s already exists", k) } c.set(k, x, d) - c.mu.Unlock() return nil } // Set a new value for the cache key only if it already exists, and the existing // item hasn't expired. Returns an error otherwise. func (c *cache) Replace(k string, x interface{}, d time.Duration) error { - c.mu.Lock() _, found := c.get(k) if !found { - c.mu.Unlock() return fmt.Errorf("Item %s doesn't exist", k) } c.set(k, x, d) - c.mu.Unlock() return nil } // 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.mu.RLock() - // "Inlining" of get and Expired - item, found := c.items[k] + tmp, found := c.items.Load(k) if !found { - c.mu.RUnlock() return nil, false } + item := tmp.(Item) if item.Expiration > 0 { if time.Now().UnixNano() > item.Expiration { - c.mu.RUnlock() return nil, false } } - c.mu.RUnlock() return item.Object, true } @@ -140,36 +126,31 @@ func (c *cache) Get(k string) (interface{}, bool) { // never expires a zero value for time.Time is returned), and a bool indicating // whether the key was found. func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) { - c.mu.RLock() - // "Inlining" of get and Expired - item, found := c.items[k] + tmp, found := c.items.Load(k) if !found { - c.mu.RUnlock() return nil, time.Time{}, false } - + item := tmp.(Item) if item.Expiration > 0 { if time.Now().UnixNano() > item.Expiration { - c.mu.RUnlock() return nil, time.Time{}, false } // Return the item and the expiration time - c.mu.RUnlock() return item.Object, time.Unix(0, item.Expiration), true } // If expiration <= 0 (i.e. no expiration time set) then return the item // and a zeroed time.Time - c.mu.RUnlock() return item.Object, time.Time{}, true } func (c *cache) get(k string) (interface{}, bool) { - item, found := c.items[k] + tmp, found := c.items.Load(k) if !found { return nil, false } + item := tmp.(Item) // "Inlining" of Expired if item.Expiration > 0 { if time.Now().UnixNano() > item.Expiration { @@ -179,16 +160,22 @@ func (c *cache) get(k string) (interface{}, bool) { return item.Object, true } +func (c *cache) getItem(k string) (Item, bool) { + tmp, found := c.items.Load(k) + if !found { + return Item{}, false + } + return tmp.(Item), true +} + // Increment an item of type int, int8, int16, int32, int64, uintptr, uint, // uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the // item's value is not an integer, if it was not found, or if it is not // possible to increment it by n. To retrieve the incremented value, use one // of the specialized methods, e.g. IncrementInt64. func (c *cache) Increment(k string, n int64) error { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return fmt.Errorf("Item %s not found", k) } switch v.Object.(type) { @@ -219,11 +206,9 @@ func (c *cache) Increment(k string, n int64) error { case float64: v.Object = v.Object.(float64) + float64(n) default: - c.mu.Unlock() return fmt.Errorf("The value for %s is not an integer", k) } - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nil } @@ -233,10 +218,8 @@ func (c *cache) Increment(k string, n int64) error { // value. To retrieve the incremented value, use one of the specialized methods, // e.g. IncrementFloat64. func (c *cache) IncrementFloat(k string, n float64) error { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return fmt.Errorf("Item %s not found", k) } switch v.Object.(type) { @@ -245,11 +228,9 @@ func (c *cache) IncrementFloat(k string, n float64) error { case float64: v.Object = v.Object.(float64) + n default: - c.mu.Unlock() return fmt.Errorf("The value for %s does not have type float32 or float64", k) } - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nil } @@ -257,21 +238,17 @@ func (c *cache) IncrementFloat(k string, n float64) error { // not an int, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementInt(k string, n int) (int, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -279,21 +256,17 @@ func (c *cache) IncrementInt(k string, n int) (int, error) { // not an int8, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementInt8(k string, n int8) (int8, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int8) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int8", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -301,21 +274,17 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) { // not an int16, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementInt16(k string, n int16) (int16, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int16) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int16", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -323,21 +292,17 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) { // not an int32, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementInt32(k string, n int32) (int32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int32", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -345,21 +310,17 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) { // not an int64, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementInt64(k string, n int64) (int64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int64", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -367,21 +328,17 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) { // not an uint, or if it was not found. If there is no error, the incremented // value is returned. func (c *cache) IncrementUint(k string, n uint) (uint, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -389,21 +346,17 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) { // is not an uintptr, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uintptr) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uintptr", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -411,21 +364,17 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) { // is not an uint8, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint8) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint8", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -433,21 +382,17 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { // is not an uint16, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint16) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint16", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -455,21 +400,17 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { // is not an uint32, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint32", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -477,21 +418,17 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { // is not an uint64, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint64", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -499,21 +436,17 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { // is not an float32, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(float32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an float32", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -521,21 +454,17 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { // is not an float64, or if it was not found. If there is no error, the // incremented value is returned. func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(float64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an float64", k) } nv := rv + n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -547,10 +476,8 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { func (c *cache) Decrement(k string, n int64) error { // TODO: Implement Increment and Decrement more cleanly. // (Cannot do Increment(k, n*-1) for uints.) - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return fmt.Errorf("Item not found") } switch v.Object.(type) { @@ -581,11 +508,9 @@ func (c *cache) Decrement(k string, n int64) error { case float64: v.Object = v.Object.(float64) - float64(n) default: - c.mu.Unlock() return fmt.Errorf("The value for %s is not an integer", k) } - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nil } @@ -595,10 +520,8 @@ func (c *cache) Decrement(k string, n int64) error { // value. To retrieve the decremented value, use one of the specialized methods, // e.g. DecrementFloat64. func (c *cache) DecrementFloat(k string, n float64) error { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return fmt.Errorf("Item %s not found", k) } switch v.Object.(type) { @@ -607,11 +530,9 @@ func (c *cache) DecrementFloat(k string, n float64) error { case float64: v.Object = v.Object.(float64) - n default: - c.mu.Unlock() return fmt.Errorf("The value for %s does not have type float32 or float64", k) } - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nil } @@ -619,21 +540,17 @@ func (c *cache) DecrementFloat(k string, n float64) error { // not an int, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementInt(k string, n int) (int, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -641,21 +558,17 @@ func (c *cache) DecrementInt(k string, n int) (int, error) { // not an int8, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementInt8(k string, n int8) (int8, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int8) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int8", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -663,21 +576,17 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) { // not an int16, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementInt16(k string, n int16) (int16, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int16) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int16", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -685,21 +594,17 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) { // not an int32, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementInt32(k string, n int32) (int32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int32", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -707,21 +612,17 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) { // not an int64, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementInt64(k string, n int64) (int64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(int64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an int64", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -729,21 +630,17 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) { // not an uint, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementUint(k string, n uint) (uint, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -751,21 +648,17 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) { // is not an uintptr, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uintptr) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uintptr", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -773,21 +666,17 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) { // not an uint8, or if it was not found. If there is no error, the decremented // value is returned. func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint8) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint8", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -795,21 +684,17 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { // is not an uint16, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint16) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint16", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -817,21 +702,17 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { // is not an uint32, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint32", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -839,21 +720,17 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { // is not an uint64, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(uint64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an uint64", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -861,21 +738,17 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { // is not an float32, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(float32) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an float32", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } @@ -883,42 +756,37 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { // is not an float64, or if it was not found. If there is no error, the // decremented value is returned. func (c *cache) DecrementFloat64(k string, n float64) (float64, error) { - c.mu.Lock() - v, found := c.items[k] + v, found := c.getItem(k) if !found || v.Expired() { - c.mu.Unlock() return 0, fmt.Errorf("Item %s not found", k) } rv, ok := v.Object.(float64) if !ok { - c.mu.Unlock() return 0, fmt.Errorf("The value for %s is not an float64", k) } nv := rv - n v.Object = nv - c.items[k] = v - c.mu.Unlock() + c.items.Store(k, v) return nv, nil } // Delete an item from the cache. Does nothing if the key is not in the cache. func (c *cache) Delete(k string) { - c.mu.Lock() - v, evicted := c.delete(k) - c.mu.Unlock() - if evicted { + if v, evicted := c.delete(k); evicted { c.onEvicted(k, v) } } func (c *cache) delete(k string) (interface{}, bool) { if c.onEvicted != nil { - if v, found := c.items[k]; found { - delete(c.items, k) + tmp, found := c.items.Load(k) + v := tmp.(Item) + if found { + c.items.Delete(k) return v.Object, true } } - delete(c.items, k) + c.items.Delete(k) return nil, false } @@ -931,8 +799,9 @@ type keyAndValue struct { func (c *cache) DeleteExpired() { var evictedItems []keyAndValue now := time.Now().UnixNano() - c.mu.Lock() - for k, v := range c.items { + c.items.Range(func(key, value interface{}) bool { + v := value.(Item) + k := key.(string) // "Inlining" of expired if v.Expiration > 0 && now > v.Expiration { ov, evicted := c.delete(k) @@ -940,8 +809,9 @@ func (c *cache) DeleteExpired() { evictedItems = append(evictedItems, keyAndValue{k, ov}) } } - } - c.mu.Unlock() + return true // if false, Range stops + }) + for _, v := range evictedItems { c.onEvicted(v.key, v.value) } @@ -961,18 +831,24 @@ func (c *cache) OnEvicted(f func(string, interface{})) { // NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the // documentation for NewFrom().) func (c *cache) Save(w io.Writer) (err error) { + m := make(map[string]Item) + c.items.Range(func(key, value interface{}) bool { + v := value.(Item) + k := key.(string) + m[k] = v + return true // if false, Range stops + }) + enc := gob.NewEncoder(w) defer func() { if x := recover(); x != nil { err = fmt.Errorf("Error registering item types with Gob library") } }() - c.mu.RLock() - defer c.mu.RUnlock() - for _, v := range c.items { + for _, v := range m { gob.Register(v.Object) } - err = enc.Encode(&c.items) + err = enc.Encode(m) return } @@ -1004,12 +880,10 @@ func (c *cache) Load(r io.Reader) error { items := map[string]Item{} err := dec.Decode(&items) if err == nil { - c.mu.Lock() - defer c.mu.Unlock() for k, v := range items { - ov, found := c.items[k] + ov, found := c.getItem(k) if !found || ov.Expired() { - c.items[k] = v + c.items.Store(k, v) } } } @@ -1036,36 +910,37 @@ func (c *cache) LoadFile(fname string) error { // Copies all unexpired items in the cache into a new map and returns it. func (c *cache) Items() map[string]Item { - c.mu.RLock() - defer c.mu.RUnlock() - m := make(map[string]Item, len(c.items)) + m := make(map[string]Item) now := time.Now().UnixNano() - for k, v := range c.items { + c.items.Range(func(key, value interface{}) bool { + v := value.(Item) + k := key.(string) // "Inlining" of Expired if v.Expiration > 0 { if now > v.Expiration { - continue + return true } } m[k] = v - } + return true // if false, Range stops + }) return m } // 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.mu.RLock() - n := len(c.items) - c.mu.RUnlock() + n := 0 + c.items.Range(func(_, _ interface{}) bool { + n++ + return true + }) return n } // Delete all items from the cache. func (c *cache) Flush() { - c.mu.Lock() - c.items = map[string]Item{} - c.mu.Unlock() + c.items = sync.Map{} } type janitor struct { @@ -1099,7 +974,7 @@ func runJanitor(c *cache, ci time.Duration) { go j.Run(c) } -func newCache(de time.Duration, m map[string]Item) *cache { +func newCache(de time.Duration, m sync.Map) *cache { if de == 0 { de = -1 } @@ -1110,7 +985,7 @@ func newCache(de time.Duration, m map[string]Item) *cache { return c } -func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *Cache { +func newCacheWithJanitor(de time.Duration, ci time.Duration, m sync.Map) *Cache { c := newCache(de, m) // This trick ensures that the janitor goroutine (which--granted it // was enabled--is running DeleteExpired on c forever) does not keep @@ -1131,7 +1006,7 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) // manually. If the cleanup interval is less than one, expired items are not // deleted from the cache before calling c.DeleteExpired(). func New(defaultExpiration, cleanupInterval time.Duration) *Cache { - items := make(map[string]Item) + items := sync.Map{} return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) } @@ -1157,5 +1032,9 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache { // map retrieved with c.Items(), and to register those same types before // decoding a blob containing an items map. func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache { - return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) + m := sync.Map{} + for k, v := range items { + m.Store(k, v) + } + return newCacheWithJanitor(defaultExpiration, cleanupInterval, m) } diff --git a/cache_test.go b/cache_test.go index de3e9d6..6d8db84 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1762,8 +1762,12 @@ func TestGetWithExpiration(t *testing.T) { } else if e2 := x.(int); e2+2 != 3 { t.Error("e (which should be 1) plus 2 does not equal 3; value:", e2) } - if expiration.UnixNano() != tc.items["e"].Expiration { - t.Error("expiration for e is not the correct time") + tmp, ok := tc.items.Load("e") + if ok { + e := tmp.(Item) + if expiration.UnixNano() != e.Expiration { + t.Error("expiration for e is not the correct time") + } } if expiration.UnixNano() < time.Now().UnixNano() { t.Error("expiration for e is in the past") diff --git a/sharded.go b/sharded.go index bcc0538..029b9d4 100644 --- a/sharded.go +++ b/sharded.go @@ -8,6 +8,7 @@ import ( "os" "runtime" "time" + "sync" ) // This is an experimental and unexported (for now) attempt at making a cache @@ -171,7 +172,7 @@ func newShardedCache(n int, de time.Duration) *shardedCache { for i := 0; i < n; i++ { c := &cache{ defaultExpiration: de, - items: map[string]Item{}, + items: sync.Map{}, } sc.cs[i] = c }