From f66ee0bbc64486436bf37b121e1fac71403f4b79 Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Fri, 27 Feb 2015 15:57:16 -0500 Subject: [PATCH 1/7] Injected LRU capabilities that simply: * ensures that when an item is added, if the number of unexpired items >= maximum number of items specified, the oldest (based on ACCESS) is expired * when an item is "gotten", or "set", the access time is updated The existing janitor system is unchanged, and used to actually delete the items. The only change from a consumer standpoint, is one addition parameter at the end of a "New" call, that specifies the maximum number of items to allow, or 0, if you want to disable the LRU at all. --- CONTRIBUTORS | 1 + cache.go | 215 +++++++++++++++++++++++++++++++++++++++++++++++--- cache_test.go | 156 ++++++++++++++++++------------------ 3 files changed, 283 insertions(+), 89 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 8a4da4e..1b07e1e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -6,3 +6,4 @@ code was contributed.) Dustin Sallings Jason Mooberry Sergey Shepelev +Matthew Keller diff --git a/cache.go b/cache.go index 12f7f0b..c4ec9a1 100644 --- a/cache.go +++ b/cache.go @@ -13,6 +13,8 @@ import ( type Item struct { Object interface{} Expiration *time.Time + Ctime *time.Time + Atime *time.Time } // Returns true if the item has expired. @@ -23,6 +25,21 @@ func (item *Item) Expired() bool { return item.Expiration.Before(time.Now()) } +// Return the Atime +func (item *Item) LastAccessed() *time.Time { + return item.Atime +} + +func (item *Item) Accessed() { + now := time.Now() + item.Atime = &now +} + +// Return the Ctime +func (item *Item) Created() *time.Time { + return item.Ctime +} + const ( // For use with functions that take an expiration time. NoExpiration time.Duration = -1 @@ -42,6 +59,7 @@ type cache struct { defaultExpiration time.Duration items map[string]*Item janitor *janitor + maxItems int } // Add an item to the cache, replacing any existing item. If the duration is 0 @@ -57,16 +75,40 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { func (c *cache) set(k string, x interface{}, d time.Duration) { var e *time.Time + t := time.Now() + at := &t + ct := &t + if d == DefaultExpiration { d = c.defaultExpiration } if d > 0 { - t := time.Now().Add(d) + t = time.Now().Add(d) e = &t } - c.items[k] = &Item{ - Object: x, - Expiration: e, + + if c.maxItems > 0 { + // We don't want to hit the expiration if we're updating. + _, found := c.get(k) + if ! found { + // Create, means maybe expire + c.ExpireLRU() + } else { + // Update, means don't expire, or change the Ctime + ct = c.items[k].Created() + } + c.items[k] = &Item{ + Object: x, + Expiration: e, + Ctime: ct, + Atime: at, + } + } else { + // Not using maxItems + c.items[k] = &Item{ + Object: x, + Expiration: e, + } } } @@ -112,6 +154,9 @@ func (c *cache) get(k string) (interface{}, bool) { if !found || item.Expired() { return nil, false } + + if c.maxItems > 0 { item.Accessed() } + return item.Object, true } @@ -127,6 +172,9 @@ func (c *cache) Increment(k string, n int64) error { c.Unlock() return fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + switch v.Object.(type) { case int: v.Object = v.Object.(int) + int(n) @@ -174,6 +222,9 @@ func (c *cache) IncrementFloat(k string, n float64) error { c.Unlock() return fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + switch v.Object.(type) { case float32: v.Object = v.Object.(float32) + float32(n) @@ -197,6 +248,9 @@ func (c *cache) IncrementInt(k string, n int) (int, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int) if !ok { c.Unlock() @@ -218,6 +272,9 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int8) if !ok { c.Unlock() @@ -239,6 +296,9 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int16) if !ok { c.Unlock() @@ -260,6 +320,9 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int32) if !ok { c.Unlock() @@ -281,6 +344,9 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int64) if !ok { c.Unlock() @@ -302,6 +368,9 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint) if !ok { c.Unlock() @@ -323,6 +392,9 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uintptr) if !ok { c.Unlock() @@ -344,6 +416,9 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint8) if !ok { c.Unlock() @@ -365,6 +440,9 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint16) if !ok { c.Unlock() @@ -386,6 +464,9 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint32) if !ok { c.Unlock() @@ -407,6 +488,9 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint64) if !ok { c.Unlock() @@ -428,6 +512,9 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(float32) if !ok { c.Unlock() @@ -449,6 +536,9 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(float64) if !ok { c.Unlock() @@ -474,6 +564,9 @@ func (c *cache) Decrement(k string, n int64) error { c.Unlock() return fmt.Errorf("Item not found") } + + if c.maxItems > 0 { v.Accessed() } + switch v.Object.(type) { case int: v.Object = v.Object.(int) - int(n) @@ -521,6 +614,9 @@ func (c *cache) DecrementFloat(k string, n float64) error { c.Unlock() return fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + switch v.Object.(type) { case float32: v.Object = v.Object.(float32) - float32(n) @@ -544,6 +640,9 @@ func (c *cache) DecrementInt(k string, n int) (int, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int) if !ok { c.Unlock() @@ -565,6 +664,9 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int8) if !ok { c.Unlock() @@ -586,6 +688,9 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int16) if !ok { c.Unlock() @@ -607,6 +712,9 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int32) if !ok { c.Unlock() @@ -628,6 +736,9 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(int64) if !ok { c.Unlock() @@ -649,6 +760,9 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint) if !ok { c.Unlock() @@ -670,6 +784,9 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uintptr) if !ok { c.Unlock() @@ -691,6 +808,9 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint8) if !ok { c.Unlock() @@ -712,6 +832,9 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint16) if !ok { c.Unlock() @@ -733,6 +856,9 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint32) if !ok { c.Unlock() @@ -754,6 +880,9 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(uint64) if !ok { c.Unlock() @@ -775,6 +904,9 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(float32) if !ok { c.Unlock() @@ -796,6 +928,9 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } + + if c.maxItems > 0 { v.Accessed() } + rv, ok := v.Object.(float64) if !ok { c.Unlock() @@ -927,6 +1062,19 @@ func (c *cache) ItemCount() int { return n } +// Returns the number of items in the cache *that have not expired* +func (c *cache) itemCount() int { + n := 0 + now := time.Now() + + for _, v := range c.items { + if v.Expiration == nil || ! v.Expiration.Before(now) { + n++ + } + } + return n +} + // Delete all items from the cache. func (c *cache) Flush() { c.Lock() @@ -934,6 +1082,50 @@ func (c *cache) Flush() { c.Unlock() } +// Find oldest N items, and Expire them. +// Returns the number "expired". +// BIG HAIRY ALERT: This function assumes it is called +// from inside a c.Lock() .. c.Unlock() block. +func (c *cache) ExpireLRU() { + + // We need the number of UNexpired Items + itemCount := c.itemCount() + + if itemCount >= c.maxItems { + + // Cache the current time. + // We do this "early" so that when we set an Expiration, + // it is definitely "do" when the janitor ticks + notw := time.Now() + + var lastTime int64 = 0 + var lastName string = "" + + for k, v := range c.items { + if v.Expired() == false { + // unexpired item + + atime := v.LastAccessed().UnixNano() + if lastTime == 0 { + // We need to expire something, so let's start with the first item + lastTime = atime + lastName = k + } else if atime < lastTime { + // We found a least-recently-used item + lastTime = atime + lastName = k + } + } + } + + if lastTime > 0 { + // We expire the item, but making it look .Expired(), + // so the janitor will clean it up for us + c.items[lastName].Expiration = ¬w + } + } +} + type janitor struct { Interval time.Duration stop chan bool @@ -964,19 +1156,20 @@ 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 map[string]*Item, mi int) *cache { if de == 0 { de = -1 } c := &cache{ defaultExpiration: de, + maxItems: mi, items: m, } return c } -func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item) *Cache { - c := newCache(de, m) +func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item, mi int) *Cache { + c := newCache(de, m, mi) // This trick ensures that the janitor goroutine (which--granted it // was enabled--is running DeleteExpired on c forever) does not keep // the returned C object from being garbage collected. When it is @@ -995,9 +1188,9 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item) // the items in the cache never expire (by default), and must be deleted // 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 { +func New(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache { items := make(map[string]*Item) - return newCacheWithJanitor(defaultExpiration, cleanupInterval, items) + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) } // Return a new cache with a given default expiration duration and cleanup @@ -1021,6 +1214,6 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache { // gob.Register() the individual types stored in the cache before encoding a // 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) +func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item, maxItems int) *Cache { + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) } diff --git a/cache_test.go b/cache_test.go index d5b2a60..c92ebd5 100644 --- a/cache_test.go +++ b/cache_test.go @@ -16,7 +16,7 @@ type TestStruct struct { } func TestCache(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) a, found := tc.Get("a") if found || a != nil { @@ -71,7 +71,7 @@ func TestCache(t *testing.T) { func TestCacheTimes(t *testing.T) { var found bool - tc := New(50*time.Millisecond, 1*time.Millisecond) + tc := New(50*time.Millisecond, 1*time.Millisecond, 0) tc.Set("a", 1, DefaultExpiration) tc.Set("b", 2, NoExpiration) tc.Set("c", 3, 20*time.Millisecond) @@ -117,7 +117,7 @@ func TestNewFrom(t *testing.T) { Expiration: nil, }, } - tc := NewFrom(DefaultExpiration, 0, m) + tc := NewFrom(DefaultExpiration, 0, m, 0) a, found := tc.Get("a") if !found { t.Fatal("Did not find a") @@ -135,7 +135,7 @@ func TestNewFrom(t *testing.T) { } func TestStorePointerToStruct(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration) x, found := tc.Get("foo") if !found { @@ -155,7 +155,7 @@ func TestStorePointerToStruct(t *testing.T) { } func TestIncrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint", 1, DefaultExpiration) err := tc.Increment("tint", 2) if err != nil { @@ -171,7 +171,7 @@ func TestIncrementWithInt(t *testing.T) { } func TestIncrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint8", int8(1), DefaultExpiration) err := tc.Increment("tint8", 2) if err != nil { @@ -187,7 +187,7 @@ func TestIncrementWithInt8(t *testing.T) { } func TestIncrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint16", int16(1), DefaultExpiration) err := tc.Increment("tint16", 2) if err != nil { @@ -203,7 +203,7 @@ func TestIncrementWithInt16(t *testing.T) { } func TestIncrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint32", int32(1), DefaultExpiration) err := tc.Increment("tint32", 2) if err != nil { @@ -219,7 +219,7 @@ func TestIncrementWithInt32(t *testing.T) { } func TestIncrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint64", int64(1), DefaultExpiration) err := tc.Increment("tint64", 2) if err != nil { @@ -235,7 +235,7 @@ func TestIncrementWithInt64(t *testing.T) { } func TestIncrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint", uint(1), DefaultExpiration) err := tc.Increment("tuint", 2) if err != nil { @@ -251,7 +251,7 @@ func TestIncrementWithUint(t *testing.T) { } func TestIncrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuintptr", uintptr(1), DefaultExpiration) err := tc.Increment("tuintptr", 2) if err != nil { @@ -268,7 +268,7 @@ func TestIncrementWithUintptr(t *testing.T) { } func TestIncrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint8", uint8(1), DefaultExpiration) err := tc.Increment("tuint8", 2) if err != nil { @@ -284,7 +284,7 @@ func TestIncrementWithUint8(t *testing.T) { } func TestIncrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint16", uint16(1), DefaultExpiration) err := tc.Increment("tuint16", 2) if err != nil { @@ -301,7 +301,7 @@ func TestIncrementWithUint16(t *testing.T) { } func TestIncrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint32", uint32(1), DefaultExpiration) err := tc.Increment("tuint32", 2) if err != nil { @@ -317,7 +317,7 @@ func TestIncrementWithUint32(t *testing.T) { } func TestIncrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint64", uint64(1), DefaultExpiration) err := tc.Increment("tuint64", 2) if err != nil { @@ -334,7 +334,7 @@ func TestIncrementWithUint64(t *testing.T) { } func TestIncrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.Increment("float32", 2) if err != nil { @@ -350,7 +350,7 @@ func TestIncrementWithFloat32(t *testing.T) { } func TestIncrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.Increment("float64", 2) if err != nil { @@ -366,7 +366,7 @@ func TestIncrementWithFloat64(t *testing.T) { } func TestIncrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.IncrementFloat("float32", 2) if err != nil { @@ -382,7 +382,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) { } func TestIncrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.IncrementFloat("float64", 2) if err != nil { @@ -398,7 +398,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) { } func TestDecrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int", int(5), DefaultExpiration) err := tc.Decrement("int", 2) if err != nil { @@ -414,7 +414,7 @@ func TestDecrementWithInt(t *testing.T) { } func TestDecrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int8", int8(5), DefaultExpiration) err := tc.Decrement("int8", 2) if err != nil { @@ -430,7 +430,7 @@ func TestDecrementWithInt8(t *testing.T) { } func TestDecrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int16", int16(5), DefaultExpiration) err := tc.Decrement("int16", 2) if err != nil { @@ -446,7 +446,7 @@ func TestDecrementWithInt16(t *testing.T) { } func TestDecrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int32", int32(5), DefaultExpiration) err := tc.Decrement("int32", 2) if err != nil { @@ -462,7 +462,7 @@ func TestDecrementWithInt32(t *testing.T) { } func TestDecrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int64", int64(5), DefaultExpiration) err := tc.Decrement("int64", 2) if err != nil { @@ -478,7 +478,7 @@ func TestDecrementWithInt64(t *testing.T) { } func TestDecrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint", uint(5), DefaultExpiration) err := tc.Decrement("uint", 2) if err != nil { @@ -494,7 +494,7 @@ func TestDecrementWithUint(t *testing.T) { } func TestDecrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uintptr", uintptr(5), DefaultExpiration) err := tc.Decrement("uintptr", 2) if err != nil { @@ -510,7 +510,7 @@ func TestDecrementWithUintptr(t *testing.T) { } func TestDecrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint8", uint8(5), DefaultExpiration) err := tc.Decrement("uint8", 2) if err != nil { @@ -526,7 +526,7 @@ func TestDecrementWithUint8(t *testing.T) { } func TestDecrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint16", uint16(5), DefaultExpiration) err := tc.Decrement("uint16", 2) if err != nil { @@ -542,7 +542,7 @@ func TestDecrementWithUint16(t *testing.T) { } func TestDecrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint32", uint32(5), DefaultExpiration) err := tc.Decrement("uint32", 2) if err != nil { @@ -558,7 +558,7 @@ func TestDecrementWithUint32(t *testing.T) { } func TestDecrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint64", uint64(5), DefaultExpiration) err := tc.Decrement("uint64", 2) if err != nil { @@ -574,7 +574,7 @@ func TestDecrementWithUint64(t *testing.T) { } func TestDecrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.Decrement("float32", 2) if err != nil { @@ -590,7 +590,7 @@ func TestDecrementWithFloat32(t *testing.T) { } func TestDecrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.Decrement("float64", 2) if err != nil { @@ -606,7 +606,7 @@ func TestDecrementWithFloat64(t *testing.T) { } func TestDecrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.DecrementFloat("float32", 2) if err != nil { @@ -622,7 +622,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) { } func TestDecrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.DecrementFloat("float64", 2) if err != nil { @@ -638,7 +638,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) { } func TestIncrementInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint", 1, DefaultExpiration) n, err := tc.IncrementInt("tint", 2) if err != nil { @@ -657,7 +657,7 @@ func TestIncrementInt(t *testing.T) { } func TestIncrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint8", int8(1), DefaultExpiration) n, err := tc.IncrementInt8("tint8", 2) if err != nil { @@ -676,7 +676,7 @@ func TestIncrementInt8(t *testing.T) { } func TestIncrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint16", int16(1), DefaultExpiration) n, err := tc.IncrementInt16("tint16", 2) if err != nil { @@ -695,7 +695,7 @@ func TestIncrementInt16(t *testing.T) { } func TestIncrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint32", int32(1), DefaultExpiration) n, err := tc.IncrementInt32("tint32", 2) if err != nil { @@ -714,7 +714,7 @@ func TestIncrementInt32(t *testing.T) { } func TestIncrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tint64", int64(1), DefaultExpiration) n, err := tc.IncrementInt64("tint64", 2) if err != nil { @@ -733,7 +733,7 @@ func TestIncrementInt64(t *testing.T) { } func TestIncrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint", uint(1), DefaultExpiration) n, err := tc.IncrementUint("tuint", 2) if err != nil { @@ -752,7 +752,7 @@ func TestIncrementUint(t *testing.T) { } func TestIncrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuintptr", uintptr(1), DefaultExpiration) n, err := tc.IncrementUintptr("tuintptr", 2) if err != nil { @@ -771,7 +771,7 @@ func TestIncrementUintptr(t *testing.T) { } func TestIncrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint8", uint8(1), DefaultExpiration) n, err := tc.IncrementUint8("tuint8", 2) if err != nil { @@ -790,7 +790,7 @@ func TestIncrementUint8(t *testing.T) { } func TestIncrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint16", uint16(1), DefaultExpiration) n, err := tc.IncrementUint16("tuint16", 2) if err != nil { @@ -809,7 +809,7 @@ func TestIncrementUint16(t *testing.T) { } func TestIncrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint32", uint32(1), DefaultExpiration) n, err := tc.IncrementUint32("tuint32", 2) if err != nil { @@ -828,7 +828,7 @@ func TestIncrementUint32(t *testing.T) { } func TestIncrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("tuint64", uint64(1), DefaultExpiration) n, err := tc.IncrementUint64("tuint64", 2) if err != nil { @@ -847,7 +847,7 @@ func TestIncrementUint64(t *testing.T) { } func TestIncrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(1.5), DefaultExpiration) n, err := tc.IncrementFloat32("float32", 2) if err != nil { @@ -866,7 +866,7 @@ func TestIncrementFloat32(t *testing.T) { } func TestIncrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(1.5), DefaultExpiration) n, err := tc.IncrementFloat64("float64", 2) if err != nil { @@ -885,7 +885,7 @@ func TestIncrementFloat64(t *testing.T) { } func TestDecrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int8", int8(5), DefaultExpiration) n, err := tc.DecrementInt8("int8", 2) if err != nil { @@ -904,7 +904,7 @@ func TestDecrementInt8(t *testing.T) { } func TestDecrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int16", int16(5), DefaultExpiration) n, err := tc.DecrementInt16("int16", 2) if err != nil { @@ -923,7 +923,7 @@ func TestDecrementInt16(t *testing.T) { } func TestDecrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int32", int32(5), DefaultExpiration) n, err := tc.DecrementInt32("int32", 2) if err != nil { @@ -942,7 +942,7 @@ func TestDecrementInt32(t *testing.T) { } func TestDecrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int64", int64(5), DefaultExpiration) n, err := tc.DecrementInt64("int64", 2) if err != nil { @@ -961,7 +961,7 @@ func TestDecrementInt64(t *testing.T) { } func TestDecrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint", uint(5), DefaultExpiration) n, err := tc.DecrementUint("uint", 2) if err != nil { @@ -980,7 +980,7 @@ func TestDecrementUint(t *testing.T) { } func TestDecrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uintptr", uintptr(5), DefaultExpiration) n, err := tc.DecrementUintptr("uintptr", 2) if err != nil { @@ -999,7 +999,7 @@ func TestDecrementUintptr(t *testing.T) { } func TestDecrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint8", uint8(5), DefaultExpiration) n, err := tc.DecrementUint8("uint8", 2) if err != nil { @@ -1018,7 +1018,7 @@ func TestDecrementUint8(t *testing.T) { } func TestDecrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint16", uint16(5), DefaultExpiration) n, err := tc.DecrementUint16("uint16", 2) if err != nil { @@ -1037,7 +1037,7 @@ func TestDecrementUint16(t *testing.T) { } func TestDecrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint32", uint32(5), DefaultExpiration) n, err := tc.DecrementUint32("uint32", 2) if err != nil { @@ -1056,7 +1056,7 @@ func TestDecrementUint32(t *testing.T) { } func TestDecrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint64", uint64(5), DefaultExpiration) n, err := tc.DecrementUint64("uint64", 2) if err != nil { @@ -1075,7 +1075,7 @@ func TestDecrementUint64(t *testing.T) { } func TestDecrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float32", float32(5), DefaultExpiration) n, err := tc.DecrementFloat32("float32", 2) if err != nil { @@ -1094,7 +1094,7 @@ func TestDecrementFloat32(t *testing.T) { } func TestDecrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("float64", float64(5), DefaultExpiration) n, err := tc.DecrementFloat64("float64", 2) if err != nil { @@ -1113,7 +1113,7 @@ func TestDecrementFloat64(t *testing.T) { } func TestAdd(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) err := tc.Add("foo", "bar", DefaultExpiration) if err != nil { t.Error("Couldn't add foo even though it shouldn't exist") @@ -1125,7 +1125,7 @@ func TestAdd(t *testing.T) { } func TestReplace(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) err := tc.Replace("foo", "bar", DefaultExpiration) if err == nil { t.Error("Replaced foo when it shouldn't exist") @@ -1138,7 +1138,7 @@ func TestReplace(t *testing.T) { } func TestDelete(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", "bar", DefaultExpiration) tc.Delete("foo") x, found := tc.Get("foo") @@ -1151,7 +1151,7 @@ func TestDelete(t *testing.T) { } func TestItemCount(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", "1", DefaultExpiration) tc.Set("bar", "2", DefaultExpiration) tc.Set("baz", "3", DefaultExpiration) @@ -1161,7 +1161,7 @@ func TestItemCount(t *testing.T) { } func TestFlush(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", "bar", DefaultExpiration) tc.Set("baz", "yes", DefaultExpiration) tc.Flush() @@ -1182,7 +1182,7 @@ func TestFlush(t *testing.T) { } func TestIncrementOverflowInt(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("int8", int8(127), DefaultExpiration) err := tc.Increment("int8", 1) if err != nil { @@ -1197,7 +1197,7 @@ func TestIncrementOverflowInt(t *testing.T) { } func TestIncrementOverflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint8", uint8(255), DefaultExpiration) err := tc.Increment("uint8", 1) if err != nil { @@ -1211,7 +1211,7 @@ func TestIncrementOverflowUint(t *testing.T) { } func TestDecrementUnderflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("uint8", uint8(0), DefaultExpiration) err := tc.Decrement("uint8", 1) if err != nil { @@ -1225,7 +1225,7 @@ func TestDecrementUnderflowUint(t *testing.T) { } func TestCacheSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) testFillAndSerialize(t, tc) // Check if gob.Register behaves properly even after multiple gob.Register @@ -1261,7 +1261,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { t.Fatal("Couldn't save cache to fp:", err) } - oc := New(DefaultExpiration, 0) + oc := New(DefaultExpiration, 0, 0) err = oc.Load(fp) if err != nil { t.Fatal("Couldn't load cache from fp:", err) @@ -1352,7 +1352,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { } func TestFileSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Add("a", "a", DefaultExpiration) tc.Add("b", "b", DefaultExpiration) f, err := ioutil.TempFile("", "go-cache-cache.dat") @@ -1363,7 +1363,7 @@ func TestFileSerialization(t *testing.T) { f.Close() tc.SaveFile(fname) - oc := New(DefaultExpiration, 0) + oc := New(DefaultExpiration, 0, 0) oc.Add("a", "aa", 0) // this should not be overwritten err = oc.LoadFile(fname) if err != nil { @@ -1391,7 +1391,7 @@ func TestFileSerialization(t *testing.T) { } func TestSerializeUnserializable(t *testing.T) { - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) ch := make(chan bool, 1) ch <- true tc.Set("chan", ch, DefaultExpiration) @@ -1404,7 +1404,7 @@ func TestSerializeUnserializable(t *testing.T) { func BenchmarkCacheGet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", "bar", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -1428,7 +1428,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) { func BenchmarkCacheGetConcurrent(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) tc.Set("foo", "bar", DefaultExpiration) wg := new(sync.WaitGroup) workers := runtime.NumCPU() @@ -1476,7 +1476,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { // in sharded_test.go. b.StopTimer() n := 10000 - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) @@ -1500,7 +1500,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { func BenchmarkCacheSet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1521,7 +1521,7 @@ func BenchmarkRWMutexMapSet(b *testing.B) { func BenchmarkCacheSetDelete(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1546,7 +1546,7 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) { func BenchmarkCacheSetDeleteSingleLock(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0) + tc := New(DefaultExpiration, 0, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Lock() From b60c6ee2c8285b6f7ded322f310c3da66b9b79f0 Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Fri, 6 Mar 2015 08:14:50 -0500 Subject: [PATCH 2/7] Consistency. --- cache.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cache.go b/cache.go index c4ec9a1..32a0025 100644 --- a/cache.go +++ b/cache.go @@ -1096,7 +1096,7 @@ func (c *cache) ExpireLRU() { // Cache the current time. // We do this "early" so that when we set an Expiration, // it is definitely "do" when the janitor ticks - notw := time.Now() + now := time.Now() var lastTime int64 = 0 var lastName string = "" @@ -1121,7 +1121,7 @@ func (c *cache) ExpireLRU() { if lastTime > 0 { // We expire the item, but making it look .Expired(), // so the janitor will clean it up for us - c.items[lastName].Expiration = ¬w + c.items[lastName].Expiration = &now } } } From bf414c412ef249842403523697e643da1b8aa4b7 Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Sun, 8 Mar 2015 11:50:57 -0400 Subject: [PATCH 3/7] Performance was suffering with very large caches, when the cache was under maxItems pressure. Reverted the LRU handler back to the first iteration: lazy deletion via the janitor, with bulk removal. --- cache.go | 96 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/cache.go b/cache.go index 32a0025..4a850f6 100644 --- a/cache.go +++ b/cache.go @@ -90,11 +90,8 @@ func (c *cache) set(k string, x interface{}, d time.Duration) { if c.maxItems > 0 { // We don't want to hit the expiration if we're updating. _, found := c.get(k) - if ! found { - // Create, means maybe expire - c.ExpireLRU() - } else { - // Update, means don't expire, or change the Ctime + if found { + // Update means change the Ctime ct = c.items[k].Created() } c.items[k] = &Item{ @@ -1062,16 +1059,11 @@ func (c *cache) ItemCount() int { return n } -// Returns the number of items in the cache *that have not expired* +// Returns the number of items in the cache without locking. This may include +// items that have expired, but have not yet been cleaned up. Equivalent to +// len(c.Items()). func (c *cache) itemCount() int { - n := 0 - now := time.Now() - - for _, v := range c.items { - if v.Expiration == nil || ! v.Expiration.Before(now) { - n++ - } - } + n := len(c.items) return n } @@ -1082,48 +1074,50 @@ func (c *cache) Flush() { c.Unlock() } -// Find oldest N items, and Expire them. -// Returns the number "expired". -// BIG HAIRY ALERT: This function assumes it is called -// from inside a c.Lock() .. c.Unlock() block. -func (c *cache) ExpireLRU() { - - // We need the number of UNexpired Items - itemCount := c.itemCount() - - if itemCount >= c.maxItems { +// Find oldest N items, and delete them. +func (c *cache) DeleteLRU(numItems int) { - // Cache the current time. - // We do this "early" so that when we set an Expiration, - // it is definitely "do" when the janitor ticks - now := time.Now() + c.Lock() - var lastTime int64 = 0 - var lastName string = "" + var lastTime int64 = 0 + lastItems := make([]string, numItems) // ringbuffer for the last numItems + liCount := 0 + full := false - for k, v := range c.items { - if v.Expired() == false { - // unexpired item + for k, v := range c.items { + if v.Expired() == false { + // unexpired item + + atime := v.LastAccessed().UnixNano() + if full == false || atime < lastTime { + // We found a least-recently-used item, + // or our purge buffer isn't full yet + lastTime = atime - atime := v.LastAccessed().UnixNano() - if lastTime == 0 { - // We need to expire something, so let's start with the first item - lastTime = atime - lastName = k - } else if atime < lastTime { - // We found a least-recently-used item - lastTime = atime - lastName = k + // Append it to the buffer, + // or start overwriting it + if liCount < numItems { + lastItems[liCount] = k + liCount ++ + } else { + lastItems[0] = k + liCount = 1 + full = true } } } - - if lastTime > 0 { - // We expire the item, but making it look .Expired(), - // so the janitor will clean it up for us - c.items[lastName].Expiration = &now + } + + if lastTime > 0 { + // We expire the items, but making it look .Expired(), + // so the janitor will clean it up for us + for i := 0; i < len(lastItems) && lastItems[i] != ""; i++ { + lastName := lastItems[i] + c.delete(lastName) } } + + c.Unlock() } type janitor struct { @@ -1138,6 +1132,14 @@ func (j *janitor) Run(c *cache) { select { case <-tick: c.DeleteExpired() + + if c.maxItems > 0 { + // Purge any LRU overages + overCount := c.itemCount() - c.maxItems + if overCount > 0 { + c.DeleteLRU(overCount) + } + } case <-j.stop: return } From 2056c799e1ea6b9cce8c08c54965ec013adacd5b Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Sun, 8 Mar 2015 12:39:08 -0400 Subject: [PATCH 4/7] Comment correction --- cache.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cache.go b/cache.go index 4a850f6..2b368ea 100644 --- a/cache.go +++ b/cache.go @@ -1109,8 +1109,7 @@ func (c *cache) DeleteLRU(numItems int) { } if lastTime > 0 { - // We expire the items, but making it look .Expired(), - // so the janitor will clean it up for us + // Clean up the items for i := 0; i < len(lastItems) && lastItems[i] != ""; i++ { lastName := lastItems[i] c.delete(lastName) From d600e983c275dc54c3621fa3e52f64cdf73fcadc Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Mon, 9 Mar 2015 08:17:46 -0400 Subject: [PATCH 5/7] For inclusion with upstream projection, Cache.New() has been reverted to its former argument list, and Cache.NewWithLRU() as been added to allow setting maxItems. cache_test has also been reverted. --- cache.go | 14 ++++- cache_test.go | 156 +++++++++++++++++++++++++------------------------- 2 files changed, 91 insertions(+), 79 deletions(-) diff --git a/cache.go b/cache.go index 2b368ea..cdcbc79 100644 --- a/cache.go +++ b/cache.go @@ -1189,7 +1189,19 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item, // the items in the cache never expire (by default), and must be deleted // 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, maxItems int) *Cache { +func New(defaultExpiration, cleanupInterval time.Duration) *Cache { + items := make(map[string]*Item) + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0) +} + +// Return a new cache with a given default expiration duration, cleanup +// interval, and maximum-ish number of items. If the expiration duration +// is less than one (or NoExpiration), the items in the cache never expire +// (by default), and must be deleted manually. If the cleanup interval is +// less than one, expired items are not deleted from the cache before +// calling c.DeleteExpired() and/or c.DeleteLRU(). If maxItems is not greater +// than zero, then there will be no soft cap on the number of items in the cache. +func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache { items := make(map[string]*Item) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) } diff --git a/cache_test.go b/cache_test.go index c92ebd5..d5b2a60 100644 --- a/cache_test.go +++ b/cache_test.go @@ -16,7 +16,7 @@ type TestStruct struct { } func TestCache(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) a, found := tc.Get("a") if found || a != nil { @@ -71,7 +71,7 @@ func TestCache(t *testing.T) { func TestCacheTimes(t *testing.T) { var found bool - tc := New(50*time.Millisecond, 1*time.Millisecond, 0) + tc := New(50*time.Millisecond, 1*time.Millisecond) tc.Set("a", 1, DefaultExpiration) tc.Set("b", 2, NoExpiration) tc.Set("c", 3, 20*time.Millisecond) @@ -117,7 +117,7 @@ func TestNewFrom(t *testing.T) { Expiration: nil, }, } - tc := NewFrom(DefaultExpiration, 0, m, 0) + tc := NewFrom(DefaultExpiration, 0, m) a, found := tc.Get("a") if !found { t.Fatal("Did not find a") @@ -135,7 +135,7 @@ func TestNewFrom(t *testing.T) { } func TestStorePointerToStruct(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration) x, found := tc.Get("foo") if !found { @@ -155,7 +155,7 @@ func TestStorePointerToStruct(t *testing.T) { } func TestIncrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint", 1, DefaultExpiration) err := tc.Increment("tint", 2) if err != nil { @@ -171,7 +171,7 @@ func TestIncrementWithInt(t *testing.T) { } func TestIncrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint8", int8(1), DefaultExpiration) err := tc.Increment("tint8", 2) if err != nil { @@ -187,7 +187,7 @@ func TestIncrementWithInt8(t *testing.T) { } func TestIncrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint16", int16(1), DefaultExpiration) err := tc.Increment("tint16", 2) if err != nil { @@ -203,7 +203,7 @@ func TestIncrementWithInt16(t *testing.T) { } func TestIncrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint32", int32(1), DefaultExpiration) err := tc.Increment("tint32", 2) if err != nil { @@ -219,7 +219,7 @@ func TestIncrementWithInt32(t *testing.T) { } func TestIncrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint64", int64(1), DefaultExpiration) err := tc.Increment("tint64", 2) if err != nil { @@ -235,7 +235,7 @@ func TestIncrementWithInt64(t *testing.T) { } func TestIncrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint", uint(1), DefaultExpiration) err := tc.Increment("tuint", 2) if err != nil { @@ -251,7 +251,7 @@ func TestIncrementWithUint(t *testing.T) { } func TestIncrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuintptr", uintptr(1), DefaultExpiration) err := tc.Increment("tuintptr", 2) if err != nil { @@ -268,7 +268,7 @@ func TestIncrementWithUintptr(t *testing.T) { } func TestIncrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint8", uint8(1), DefaultExpiration) err := tc.Increment("tuint8", 2) if err != nil { @@ -284,7 +284,7 @@ func TestIncrementWithUint8(t *testing.T) { } func TestIncrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint16", uint16(1), DefaultExpiration) err := tc.Increment("tuint16", 2) if err != nil { @@ -301,7 +301,7 @@ func TestIncrementWithUint16(t *testing.T) { } func TestIncrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint32", uint32(1), DefaultExpiration) err := tc.Increment("tuint32", 2) if err != nil { @@ -317,7 +317,7 @@ func TestIncrementWithUint32(t *testing.T) { } func TestIncrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint64", uint64(1), DefaultExpiration) err := tc.Increment("tuint64", 2) if err != nil { @@ -334,7 +334,7 @@ func TestIncrementWithUint64(t *testing.T) { } func TestIncrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.Increment("float32", 2) if err != nil { @@ -350,7 +350,7 @@ func TestIncrementWithFloat32(t *testing.T) { } func TestIncrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.Increment("float64", 2) if err != nil { @@ -366,7 +366,7 @@ func TestIncrementWithFloat64(t *testing.T) { } func TestIncrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(1.5), DefaultExpiration) err := tc.IncrementFloat("float32", 2) if err != nil { @@ -382,7 +382,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) { } func TestIncrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(1.5), DefaultExpiration) err := tc.IncrementFloat("float64", 2) if err != nil { @@ -398,7 +398,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) { } func TestDecrementWithInt(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int", int(5), DefaultExpiration) err := tc.Decrement("int", 2) if err != nil { @@ -414,7 +414,7 @@ func TestDecrementWithInt(t *testing.T) { } func TestDecrementWithInt8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int8", int8(5), DefaultExpiration) err := tc.Decrement("int8", 2) if err != nil { @@ -430,7 +430,7 @@ func TestDecrementWithInt8(t *testing.T) { } func TestDecrementWithInt16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int16", int16(5), DefaultExpiration) err := tc.Decrement("int16", 2) if err != nil { @@ -446,7 +446,7 @@ func TestDecrementWithInt16(t *testing.T) { } func TestDecrementWithInt32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int32", int32(5), DefaultExpiration) err := tc.Decrement("int32", 2) if err != nil { @@ -462,7 +462,7 @@ func TestDecrementWithInt32(t *testing.T) { } func TestDecrementWithInt64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int64", int64(5), DefaultExpiration) err := tc.Decrement("int64", 2) if err != nil { @@ -478,7 +478,7 @@ func TestDecrementWithInt64(t *testing.T) { } func TestDecrementWithUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint", uint(5), DefaultExpiration) err := tc.Decrement("uint", 2) if err != nil { @@ -494,7 +494,7 @@ func TestDecrementWithUint(t *testing.T) { } func TestDecrementWithUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uintptr", uintptr(5), DefaultExpiration) err := tc.Decrement("uintptr", 2) if err != nil { @@ -510,7 +510,7 @@ func TestDecrementWithUintptr(t *testing.T) { } func TestDecrementWithUint8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint8", uint8(5), DefaultExpiration) err := tc.Decrement("uint8", 2) if err != nil { @@ -526,7 +526,7 @@ func TestDecrementWithUint8(t *testing.T) { } func TestDecrementWithUint16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint16", uint16(5), DefaultExpiration) err := tc.Decrement("uint16", 2) if err != nil { @@ -542,7 +542,7 @@ func TestDecrementWithUint16(t *testing.T) { } func TestDecrementWithUint32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint32", uint32(5), DefaultExpiration) err := tc.Decrement("uint32", 2) if err != nil { @@ -558,7 +558,7 @@ func TestDecrementWithUint32(t *testing.T) { } func TestDecrementWithUint64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint64", uint64(5), DefaultExpiration) err := tc.Decrement("uint64", 2) if err != nil { @@ -574,7 +574,7 @@ func TestDecrementWithUint64(t *testing.T) { } func TestDecrementWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.Decrement("float32", 2) if err != nil { @@ -590,7 +590,7 @@ func TestDecrementWithFloat32(t *testing.T) { } func TestDecrementWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.Decrement("float64", 2) if err != nil { @@ -606,7 +606,7 @@ func TestDecrementWithFloat64(t *testing.T) { } func TestDecrementFloatWithFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(5.5), DefaultExpiration) err := tc.DecrementFloat("float32", 2) if err != nil { @@ -622,7 +622,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) { } func TestDecrementFloatWithFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(5.5), DefaultExpiration) err := tc.DecrementFloat("float64", 2) if err != nil { @@ -638,7 +638,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) { } func TestIncrementInt(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint", 1, DefaultExpiration) n, err := tc.IncrementInt("tint", 2) if err != nil { @@ -657,7 +657,7 @@ func TestIncrementInt(t *testing.T) { } func TestIncrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint8", int8(1), DefaultExpiration) n, err := tc.IncrementInt8("tint8", 2) if err != nil { @@ -676,7 +676,7 @@ func TestIncrementInt8(t *testing.T) { } func TestIncrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint16", int16(1), DefaultExpiration) n, err := tc.IncrementInt16("tint16", 2) if err != nil { @@ -695,7 +695,7 @@ func TestIncrementInt16(t *testing.T) { } func TestIncrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint32", int32(1), DefaultExpiration) n, err := tc.IncrementInt32("tint32", 2) if err != nil { @@ -714,7 +714,7 @@ func TestIncrementInt32(t *testing.T) { } func TestIncrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tint64", int64(1), DefaultExpiration) n, err := tc.IncrementInt64("tint64", 2) if err != nil { @@ -733,7 +733,7 @@ func TestIncrementInt64(t *testing.T) { } func TestIncrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint", uint(1), DefaultExpiration) n, err := tc.IncrementUint("tuint", 2) if err != nil { @@ -752,7 +752,7 @@ func TestIncrementUint(t *testing.T) { } func TestIncrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuintptr", uintptr(1), DefaultExpiration) n, err := tc.IncrementUintptr("tuintptr", 2) if err != nil { @@ -771,7 +771,7 @@ func TestIncrementUintptr(t *testing.T) { } func TestIncrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint8", uint8(1), DefaultExpiration) n, err := tc.IncrementUint8("tuint8", 2) if err != nil { @@ -790,7 +790,7 @@ func TestIncrementUint8(t *testing.T) { } func TestIncrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint16", uint16(1), DefaultExpiration) n, err := tc.IncrementUint16("tuint16", 2) if err != nil { @@ -809,7 +809,7 @@ func TestIncrementUint16(t *testing.T) { } func TestIncrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint32", uint32(1), DefaultExpiration) n, err := tc.IncrementUint32("tuint32", 2) if err != nil { @@ -828,7 +828,7 @@ func TestIncrementUint32(t *testing.T) { } func TestIncrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("tuint64", uint64(1), DefaultExpiration) n, err := tc.IncrementUint64("tuint64", 2) if err != nil { @@ -847,7 +847,7 @@ func TestIncrementUint64(t *testing.T) { } func TestIncrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(1.5), DefaultExpiration) n, err := tc.IncrementFloat32("float32", 2) if err != nil { @@ -866,7 +866,7 @@ func TestIncrementFloat32(t *testing.T) { } func TestIncrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(1.5), DefaultExpiration) n, err := tc.IncrementFloat64("float64", 2) if err != nil { @@ -885,7 +885,7 @@ func TestIncrementFloat64(t *testing.T) { } func TestDecrementInt8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int8", int8(5), DefaultExpiration) n, err := tc.DecrementInt8("int8", 2) if err != nil { @@ -904,7 +904,7 @@ func TestDecrementInt8(t *testing.T) { } func TestDecrementInt16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int16", int16(5), DefaultExpiration) n, err := tc.DecrementInt16("int16", 2) if err != nil { @@ -923,7 +923,7 @@ func TestDecrementInt16(t *testing.T) { } func TestDecrementInt32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int32", int32(5), DefaultExpiration) n, err := tc.DecrementInt32("int32", 2) if err != nil { @@ -942,7 +942,7 @@ func TestDecrementInt32(t *testing.T) { } func TestDecrementInt64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int64", int64(5), DefaultExpiration) n, err := tc.DecrementInt64("int64", 2) if err != nil { @@ -961,7 +961,7 @@ func TestDecrementInt64(t *testing.T) { } func TestDecrementUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint", uint(5), DefaultExpiration) n, err := tc.DecrementUint("uint", 2) if err != nil { @@ -980,7 +980,7 @@ func TestDecrementUint(t *testing.T) { } func TestDecrementUintptr(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uintptr", uintptr(5), DefaultExpiration) n, err := tc.DecrementUintptr("uintptr", 2) if err != nil { @@ -999,7 +999,7 @@ func TestDecrementUintptr(t *testing.T) { } func TestDecrementUint8(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint8", uint8(5), DefaultExpiration) n, err := tc.DecrementUint8("uint8", 2) if err != nil { @@ -1018,7 +1018,7 @@ func TestDecrementUint8(t *testing.T) { } func TestDecrementUint16(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint16", uint16(5), DefaultExpiration) n, err := tc.DecrementUint16("uint16", 2) if err != nil { @@ -1037,7 +1037,7 @@ func TestDecrementUint16(t *testing.T) { } func TestDecrementUint32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint32", uint32(5), DefaultExpiration) n, err := tc.DecrementUint32("uint32", 2) if err != nil { @@ -1056,7 +1056,7 @@ func TestDecrementUint32(t *testing.T) { } func TestDecrementUint64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint64", uint64(5), DefaultExpiration) n, err := tc.DecrementUint64("uint64", 2) if err != nil { @@ -1075,7 +1075,7 @@ func TestDecrementUint64(t *testing.T) { } func TestDecrementFloat32(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float32", float32(5), DefaultExpiration) n, err := tc.DecrementFloat32("float32", 2) if err != nil { @@ -1094,7 +1094,7 @@ func TestDecrementFloat32(t *testing.T) { } func TestDecrementFloat64(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("float64", float64(5), DefaultExpiration) n, err := tc.DecrementFloat64("float64", 2) if err != nil { @@ -1113,7 +1113,7 @@ func TestDecrementFloat64(t *testing.T) { } func TestAdd(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) err := tc.Add("foo", "bar", DefaultExpiration) if err != nil { t.Error("Couldn't add foo even though it shouldn't exist") @@ -1125,7 +1125,7 @@ func TestAdd(t *testing.T) { } func TestReplace(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) err := tc.Replace("foo", "bar", DefaultExpiration) if err == nil { t.Error("Replaced foo when it shouldn't exist") @@ -1138,7 +1138,7 @@ func TestReplace(t *testing.T) { } func TestDelete(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", "bar", DefaultExpiration) tc.Delete("foo") x, found := tc.Get("foo") @@ -1151,7 +1151,7 @@ func TestDelete(t *testing.T) { } func TestItemCount(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", "1", DefaultExpiration) tc.Set("bar", "2", DefaultExpiration) tc.Set("baz", "3", DefaultExpiration) @@ -1161,7 +1161,7 @@ func TestItemCount(t *testing.T) { } func TestFlush(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", "bar", DefaultExpiration) tc.Set("baz", "yes", DefaultExpiration) tc.Flush() @@ -1182,7 +1182,7 @@ func TestFlush(t *testing.T) { } func TestIncrementOverflowInt(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("int8", int8(127), DefaultExpiration) err := tc.Increment("int8", 1) if err != nil { @@ -1197,7 +1197,7 @@ func TestIncrementOverflowInt(t *testing.T) { } func TestIncrementOverflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint8", uint8(255), DefaultExpiration) err := tc.Increment("uint8", 1) if err != nil { @@ -1211,7 +1211,7 @@ func TestIncrementOverflowUint(t *testing.T) { } func TestDecrementUnderflowUint(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("uint8", uint8(0), DefaultExpiration) err := tc.Decrement("uint8", 1) if err != nil { @@ -1225,7 +1225,7 @@ func TestDecrementUnderflowUint(t *testing.T) { } func TestCacheSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) testFillAndSerialize(t, tc) // Check if gob.Register behaves properly even after multiple gob.Register @@ -1261,7 +1261,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { t.Fatal("Couldn't save cache to fp:", err) } - oc := New(DefaultExpiration, 0, 0) + oc := New(DefaultExpiration, 0) err = oc.Load(fp) if err != nil { t.Fatal("Couldn't load cache from fp:", err) @@ -1352,7 +1352,7 @@ func testFillAndSerialize(t *testing.T, tc *Cache) { } func TestFileSerialization(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Add("a", "a", DefaultExpiration) tc.Add("b", "b", DefaultExpiration) f, err := ioutil.TempFile("", "go-cache-cache.dat") @@ -1363,7 +1363,7 @@ func TestFileSerialization(t *testing.T) { f.Close() tc.SaveFile(fname) - oc := New(DefaultExpiration, 0, 0) + oc := New(DefaultExpiration, 0) oc.Add("a", "aa", 0) // this should not be overwritten err = oc.LoadFile(fname) if err != nil { @@ -1391,7 +1391,7 @@ func TestFileSerialization(t *testing.T) { } func TestSerializeUnserializable(t *testing.T) { - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) ch := make(chan bool, 1) ch <- true tc.Set("chan", ch, DefaultExpiration) @@ -1404,7 +1404,7 @@ func TestSerializeUnserializable(t *testing.T) { func BenchmarkCacheGet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", "bar", DefaultExpiration) b.StartTimer() for i := 0; i < b.N; i++ { @@ -1428,7 +1428,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) { func BenchmarkCacheGetConcurrent(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) tc.Set("foo", "bar", DefaultExpiration) wg := new(sync.WaitGroup) workers := runtime.NumCPU() @@ -1476,7 +1476,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { // in sharded_test.go. b.StopTimer() n := 10000 - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n) @@ -1500,7 +1500,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { func BenchmarkCacheSet(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1521,7 +1521,7 @@ func BenchmarkRWMutexMapSet(b *testing.B) { func BenchmarkCacheSetDelete(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Set("foo", "bar", DefaultExpiration) @@ -1546,7 +1546,7 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) { func BenchmarkCacheSetDeleteSingleLock(b *testing.B) { b.StopTimer() - tc := New(DefaultExpiration, 0, 0) + tc := New(DefaultExpiration, 0) b.StartTimer() for i := 0; i < b.N; i++ { tc.Lock() From 378662d53fa5db3a60dc2c008fd6f434fc5ea76a Mon Sep 17 00:00:00 2001 From: Matt Keller Date: Mon, 9 Mar 2015 08:23:46 -0400 Subject: [PATCH 6/7] Readme update --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 5c14e66..a64d822 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,15 @@ cache can be saved to and loaded from a file (using `c.Items()` to retrieve the items map to serialize, and `NewFrom()` to create a cache from a deserialized one) to recover from downtime quickly. (See the docs for `NewFrom()` for caveats.) +When creating a cache object using `NewWithLRU()`, if you set the maxItems value +above 0, the LRU functionality is enabled. The cache automatically updates a +timestamp every time a given item is retrieved. In the background, the janitor takes +care of deleting items that have expired because of staleness, or are +least-recently-used when the cache is under pressure. Whatever you set your purge +interval to controls when the item will actually be removed from the cache. If you +don't want to use the janitor, and wish to manually purge LRU items, then +`c.DeleteLRU(n)` where `n` is the number of items you'd like to purge. + ### Installation `go get github.com/pmylund/go-cache` From d8d9d6491e72bd9f633d3db43d0bb30d8fd95082 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 18 Apr 2017 12:40:40 -0400 Subject: [PATCH 7/7] Various fixes for upstream acceptance, and formatting Removes the ctime tracking and access. Reverts NewFrom() changes from upstream, and adds NewFromWithLRU() in place of it. --- cache.go | 304 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 179 insertions(+), 125 deletions(-) diff --git a/cache.go b/cache.go index cdcbc79..b2f27b6 100644 --- a/cache.go +++ b/cache.go @@ -13,7 +13,6 @@ import ( type Item struct { Object interface{} Expiration *time.Time - Ctime *time.Time Atime *time.Time } @@ -30,16 +29,12 @@ func (item *Item) LastAccessed() *time.Time { return item.Atime } +// Set the Atime func (item *Item) Accessed() { now := time.Now() item.Atime = &now } -// Return the Ctime -func (item *Item) Created() *time.Time { - return item.Ctime -} - const ( // For use with functions that take an expiration time. NoExpiration time.Duration = -1 @@ -75,29 +70,23 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { func (c *cache) set(k string, x interface{}, d time.Duration) { var e *time.Time - t := time.Now() - at := &t - ct := &t - + if d == DefaultExpiration { d = c.defaultExpiration } if d > 0 { - t = time.Now().Add(d) + t := time.Now().Add(d) e = &t } - + if c.maxItems > 0 { - // We don't want to hit the expiration if we're updating. - _, found := c.get(k) - if found { - // Update means change the Ctime - ct = c.items[k].Created() - } + // Using maxItems (LRU) + now := time.Now() + at := &now + c.items[k] = &Item{ Object: x, Expiration: e, - Ctime: ct, Atime: at, } } else { @@ -151,9 +140,11 @@ func (c *cache) get(k string) (interface{}, bool) { if !found || item.Expired() { return nil, false } - - if c.maxItems > 0 { item.Accessed() } - + + if c.maxItems > 0 { + item.Accessed() + } + return item.Object, true } @@ -169,9 +160,11 @@ func (c *cache) Increment(k string, n int64) error { c.Unlock() return fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + switch v.Object.(type) { case int: v.Object = v.Object.(int) + int(n) @@ -219,9 +212,11 @@ func (c *cache) IncrementFloat(k string, n float64) error { c.Unlock() return fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + switch v.Object.(type) { case float32: v.Object = v.Object.(float32) + float32(n) @@ -245,9 +240,11 @@ func (c *cache) IncrementInt(k string, n int) (int, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int) if !ok { c.Unlock() @@ -269,9 +266,11 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int8) if !ok { c.Unlock() @@ -293,9 +292,11 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int16) if !ok { c.Unlock() @@ -317,9 +318,11 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int32) if !ok { c.Unlock() @@ -341,9 +344,11 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int64) if !ok { c.Unlock() @@ -365,9 +370,11 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint) if !ok { c.Unlock() @@ -389,9 +396,11 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uintptr) if !ok { c.Unlock() @@ -413,9 +422,11 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint8) if !ok { c.Unlock() @@ -438,7 +449,9 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) { return 0, fmt.Errorf("Item %s not found", k) } - if c.maxItems > 0 { v.Accessed() } + if c.maxItems > 0 { + v.Accessed() + } rv, ok := v.Object.(uint16) if !ok { @@ -461,9 +474,11 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint32) if !ok { c.Unlock() @@ -485,9 +500,11 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint64) if !ok { c.Unlock() @@ -509,9 +526,11 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(float32) if !ok { c.Unlock() @@ -533,9 +552,11 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(float64) if !ok { c.Unlock() @@ -561,9 +582,11 @@ func (c *cache) Decrement(k string, n int64) error { c.Unlock() return fmt.Errorf("Item not found") } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + switch v.Object.(type) { case int: v.Object = v.Object.(int) - int(n) @@ -612,8 +635,10 @@ func (c *cache) DecrementFloat(k string, n float64) error { return fmt.Errorf("Item %s not found", k) } - if c.maxItems > 0 { v.Accessed() } - + if c.maxItems > 0 { + v.Accessed() + } + switch v.Object.(type) { case float32: v.Object = v.Object.(float32) - float32(n) @@ -637,9 +662,11 @@ func (c *cache) DecrementInt(k string, n int) (int, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int) if !ok { c.Unlock() @@ -661,9 +688,11 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int8) if !ok { c.Unlock() @@ -685,9 +714,11 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int16) if !ok { c.Unlock() @@ -709,9 +740,11 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int32) if !ok { c.Unlock() @@ -733,9 +766,11 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(int64) if !ok { c.Unlock() @@ -757,9 +792,11 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint) if !ok { c.Unlock() @@ -781,9 +818,11 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uintptr) if !ok { c.Unlock() @@ -805,9 +844,11 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint8) if !ok { c.Unlock() @@ -829,9 +870,11 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint16) if !ok { c.Unlock() @@ -853,9 +896,11 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint32) if !ok { c.Unlock() @@ -878,8 +923,10 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) { return 0, fmt.Errorf("Item %s not found", k) } - if c.maxItems > 0 { v.Accessed() } - + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(uint64) if !ok { c.Unlock() @@ -901,9 +948,11 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(float32) if !ok { c.Unlock() @@ -925,9 +974,11 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) { c.Unlock() return 0, fmt.Errorf("Item %s not found", k) } - - if c.maxItems > 0 { v.Accessed() } - + + if c.maxItems > 0 { + v.Accessed() + } + rv, ok := v.Object.(float64) if !ok { c.Unlock() @@ -1059,8 +1110,8 @@ func (c *cache) ItemCount() int { return n } -// Returns the number of items in the cache without locking. This may include -// items that have expired, but have not yet been cleaned up. Equivalent to +// Returns the number of items in the cache without locking. This may include +// items that have expired, but have not yet been cleaned up. Equivalent to // len(c.Items()). func (c *cache) itemCount() int { n := len(c.items) @@ -1076,8 +1127,9 @@ func (c *cache) Flush() { // Find oldest N items, and delete them. func (c *cache) DeleteLRU(numItems int) { - + c.Lock() + defer c.Unlock() var lastTime int64 = 0 lastItems := make([]string, numItems) // ringbuffer for the last numItems @@ -1087,18 +1139,18 @@ func (c *cache) DeleteLRU(numItems int) { for k, v := range c.items { if v.Expired() == false { // unexpired item - + atime := v.LastAccessed().UnixNano() if full == false || atime < lastTime { // We found a least-recently-used item, // or our purge buffer isn't full yet lastTime = atime - + // Append it to the buffer, // or start overwriting it if liCount < numItems { lastItems[liCount] = k - liCount ++ + liCount++ } else { lastItems[0] = k liCount = 1 @@ -1107,7 +1159,7 @@ func (c *cache) DeleteLRU(numItems int) { } } } - + if lastTime > 0 { // Clean up the items for i := 0; i < len(lastItems) && lastItems[i] != ""; i++ { @@ -1115,8 +1167,6 @@ func (c *cache) DeleteLRU(numItems int) { c.delete(lastName) } } - - c.Unlock() } type janitor struct { @@ -1131,7 +1181,7 @@ func (j *janitor) Run(c *cache) { select { case <-tick: c.DeleteExpired() - + if c.maxItems > 0 { // Purge any LRU overages overCount := c.itemCount() - c.maxItems @@ -1195,13 +1245,13 @@ func New(defaultExpiration, cleanupInterval time.Duration) *Cache { } // Return a new cache with a given default expiration duration, cleanup -// interval, and maximum-ish number of items. If the expiration duration -// is less than one (or NoExpiration), the items in the cache never expire -// (by default), and must be deleted manually. If the cleanup interval is -// less than one, expired items are not deleted from the cache before -// calling c.DeleteExpired() and/or c.DeleteLRU(). If maxItems is not greater +// interval, and maximum-ish number of items. If the expiration duration +// is less than one (or NoExpiration), the items in the cache never expire +// (by default), and must be deleted manually. If the cleanup interval is +// less than one, expired items are not deleted from the cache before +// calling c.DeleteExpired() and/or c.DeleteLRU(). If maxItems is not greater // than zero, then there will be no soft cap on the number of items in the cache. -func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache { +func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache { items := make(map[string]*Item) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) } @@ -1227,6 +1277,10 @@ func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) // gob.Register() the individual types stored in the cache before encoding a // 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, maxItems int) *Cache { +func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item) *Cache { + return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0) +} + +func NewFromWithLRU(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item, maxItems int) *Cache { return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) }