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.
This commit is contained in:
Matt Keller 2015-03-09 08:17:46 -04:00
parent 2056c799e1
commit d600e983c2
2 changed files with 91 additions and 79 deletions

View File

@ -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 // 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 // manually. If the cleanup interval is less than one, expired items are not
// deleted from the cache before calling c.DeleteExpired(). // 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) items := make(map[string]*Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
} }

View File

@ -16,7 +16,7 @@ type TestStruct struct {
} }
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
a, found := tc.Get("a") a, found := tc.Get("a")
if found || a != nil { if found || a != nil {
@ -71,7 +71,7 @@ func TestCache(t *testing.T) {
func TestCacheTimes(t *testing.T) { func TestCacheTimes(t *testing.T) {
var found bool 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("a", 1, DefaultExpiration)
tc.Set("b", 2, NoExpiration) tc.Set("b", 2, NoExpiration)
tc.Set("c", 3, 20*time.Millisecond) tc.Set("c", 3, 20*time.Millisecond)
@ -117,7 +117,7 @@ func TestNewFrom(t *testing.T) {
Expiration: nil, Expiration: nil,
}, },
} }
tc := NewFrom(DefaultExpiration, 0, m, 0) tc := NewFrom(DefaultExpiration, 0, m)
a, found := tc.Get("a") a, found := tc.Get("a")
if !found { if !found {
t.Fatal("Did not find a") t.Fatal("Did not find a")
@ -135,7 +135,7 @@ func TestNewFrom(t *testing.T) {
} }
func TestStorePointerToStruct(t *testing.T) { func TestStorePointerToStruct(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration) tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration)
x, found := tc.Get("foo") x, found := tc.Get("foo")
if !found { if !found {
@ -155,7 +155,7 @@ func TestStorePointerToStruct(t *testing.T) {
} }
func TestIncrementWithInt(t *testing.T) { func TestIncrementWithInt(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint", 1, DefaultExpiration) tc.Set("tint", 1, DefaultExpiration)
err := tc.Increment("tint", 2) err := tc.Increment("tint", 2)
if err != nil { if err != nil {
@ -171,7 +171,7 @@ func TestIncrementWithInt(t *testing.T) {
} }
func TestIncrementWithInt8(t *testing.T) { func TestIncrementWithInt8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint8", int8(1), DefaultExpiration) tc.Set("tint8", int8(1), DefaultExpiration)
err := tc.Increment("tint8", 2) err := tc.Increment("tint8", 2)
if err != nil { if err != nil {
@ -187,7 +187,7 @@ func TestIncrementWithInt8(t *testing.T) {
} }
func TestIncrementWithInt16(t *testing.T) { func TestIncrementWithInt16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint16", int16(1), DefaultExpiration) tc.Set("tint16", int16(1), DefaultExpiration)
err := tc.Increment("tint16", 2) err := tc.Increment("tint16", 2)
if err != nil { if err != nil {
@ -203,7 +203,7 @@ func TestIncrementWithInt16(t *testing.T) {
} }
func TestIncrementWithInt32(t *testing.T) { func TestIncrementWithInt32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint32", int32(1), DefaultExpiration) tc.Set("tint32", int32(1), DefaultExpiration)
err := tc.Increment("tint32", 2) err := tc.Increment("tint32", 2)
if err != nil { if err != nil {
@ -219,7 +219,7 @@ func TestIncrementWithInt32(t *testing.T) {
} }
func TestIncrementWithInt64(t *testing.T) { func TestIncrementWithInt64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint64", int64(1), DefaultExpiration) tc.Set("tint64", int64(1), DefaultExpiration)
err := tc.Increment("tint64", 2) err := tc.Increment("tint64", 2)
if err != nil { if err != nil {
@ -235,7 +235,7 @@ func TestIncrementWithInt64(t *testing.T) {
} }
func TestIncrementWithUint(t *testing.T) { func TestIncrementWithUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint", uint(1), DefaultExpiration) tc.Set("tuint", uint(1), DefaultExpiration)
err := tc.Increment("tuint", 2) err := tc.Increment("tuint", 2)
if err != nil { if err != nil {
@ -251,7 +251,7 @@ func TestIncrementWithUint(t *testing.T) {
} }
func TestIncrementWithUintptr(t *testing.T) { func TestIncrementWithUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration) tc.Set("tuintptr", uintptr(1), DefaultExpiration)
err := tc.Increment("tuintptr", 2) err := tc.Increment("tuintptr", 2)
if err != nil { if err != nil {
@ -268,7 +268,7 @@ func TestIncrementWithUintptr(t *testing.T) {
} }
func TestIncrementWithUint8(t *testing.T) { func TestIncrementWithUint8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint8", uint8(1), DefaultExpiration) tc.Set("tuint8", uint8(1), DefaultExpiration)
err := tc.Increment("tuint8", 2) err := tc.Increment("tuint8", 2)
if err != nil { if err != nil {
@ -284,7 +284,7 @@ func TestIncrementWithUint8(t *testing.T) {
} }
func TestIncrementWithUint16(t *testing.T) { func TestIncrementWithUint16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint16", uint16(1), DefaultExpiration) tc.Set("tuint16", uint16(1), DefaultExpiration)
err := tc.Increment("tuint16", 2) err := tc.Increment("tuint16", 2)
if err != nil { if err != nil {
@ -301,7 +301,7 @@ func TestIncrementWithUint16(t *testing.T) {
} }
func TestIncrementWithUint32(t *testing.T) { func TestIncrementWithUint32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint32", uint32(1), DefaultExpiration) tc.Set("tuint32", uint32(1), DefaultExpiration)
err := tc.Increment("tuint32", 2) err := tc.Increment("tuint32", 2)
if err != nil { if err != nil {
@ -317,7 +317,7 @@ func TestIncrementWithUint32(t *testing.T) {
} }
func TestIncrementWithUint64(t *testing.T) { func TestIncrementWithUint64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint64", uint64(1), DefaultExpiration) tc.Set("tuint64", uint64(1), DefaultExpiration)
err := tc.Increment("tuint64", 2) err := tc.Increment("tuint64", 2)
if err != nil { if err != nil {
@ -334,7 +334,7 @@ func TestIncrementWithUint64(t *testing.T) {
} }
func TestIncrementWithFloat32(t *testing.T) { func TestIncrementWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(1.5), DefaultExpiration) tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.Increment("float32", 2) err := tc.Increment("float32", 2)
if err != nil { if err != nil {
@ -350,7 +350,7 @@ func TestIncrementWithFloat32(t *testing.T) {
} }
func TestIncrementWithFloat64(t *testing.T) { func TestIncrementWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(1.5), DefaultExpiration) tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.Increment("float64", 2) err := tc.Increment("float64", 2)
if err != nil { if err != nil {
@ -366,7 +366,7 @@ func TestIncrementWithFloat64(t *testing.T) {
} }
func TestIncrementFloatWithFloat32(t *testing.T) { func TestIncrementFloatWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(1.5), DefaultExpiration) tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.IncrementFloat("float32", 2) err := tc.IncrementFloat("float32", 2)
if err != nil { if err != nil {
@ -382,7 +382,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) {
} }
func TestIncrementFloatWithFloat64(t *testing.T) { func TestIncrementFloatWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(1.5), DefaultExpiration) tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.IncrementFloat("float64", 2) err := tc.IncrementFloat("float64", 2)
if err != nil { if err != nil {
@ -398,7 +398,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) {
} }
func TestDecrementWithInt(t *testing.T) { func TestDecrementWithInt(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int", int(5), DefaultExpiration) tc.Set("int", int(5), DefaultExpiration)
err := tc.Decrement("int", 2) err := tc.Decrement("int", 2)
if err != nil { if err != nil {
@ -414,7 +414,7 @@ func TestDecrementWithInt(t *testing.T) {
} }
func TestDecrementWithInt8(t *testing.T) { func TestDecrementWithInt8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int8", int8(5), DefaultExpiration) tc.Set("int8", int8(5), DefaultExpiration)
err := tc.Decrement("int8", 2) err := tc.Decrement("int8", 2)
if err != nil { if err != nil {
@ -430,7 +430,7 @@ func TestDecrementWithInt8(t *testing.T) {
} }
func TestDecrementWithInt16(t *testing.T) { func TestDecrementWithInt16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int16", int16(5), DefaultExpiration) tc.Set("int16", int16(5), DefaultExpiration)
err := tc.Decrement("int16", 2) err := tc.Decrement("int16", 2)
if err != nil { if err != nil {
@ -446,7 +446,7 @@ func TestDecrementWithInt16(t *testing.T) {
} }
func TestDecrementWithInt32(t *testing.T) { func TestDecrementWithInt32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int32", int32(5), DefaultExpiration) tc.Set("int32", int32(5), DefaultExpiration)
err := tc.Decrement("int32", 2) err := tc.Decrement("int32", 2)
if err != nil { if err != nil {
@ -462,7 +462,7 @@ func TestDecrementWithInt32(t *testing.T) {
} }
func TestDecrementWithInt64(t *testing.T) { func TestDecrementWithInt64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int64", int64(5), DefaultExpiration) tc.Set("int64", int64(5), DefaultExpiration)
err := tc.Decrement("int64", 2) err := tc.Decrement("int64", 2)
if err != nil { if err != nil {
@ -478,7 +478,7 @@ func TestDecrementWithInt64(t *testing.T) {
} }
func TestDecrementWithUint(t *testing.T) { func TestDecrementWithUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint", uint(5), DefaultExpiration) tc.Set("uint", uint(5), DefaultExpiration)
err := tc.Decrement("uint", 2) err := tc.Decrement("uint", 2)
if err != nil { if err != nil {
@ -494,7 +494,7 @@ func TestDecrementWithUint(t *testing.T) {
} }
func TestDecrementWithUintptr(t *testing.T) { func TestDecrementWithUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration) tc.Set("uintptr", uintptr(5), DefaultExpiration)
err := tc.Decrement("uintptr", 2) err := tc.Decrement("uintptr", 2)
if err != nil { if err != nil {
@ -510,7 +510,7 @@ func TestDecrementWithUintptr(t *testing.T) {
} }
func TestDecrementWithUint8(t *testing.T) { func TestDecrementWithUint8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint8", uint8(5), DefaultExpiration) tc.Set("uint8", uint8(5), DefaultExpiration)
err := tc.Decrement("uint8", 2) err := tc.Decrement("uint8", 2)
if err != nil { if err != nil {
@ -526,7 +526,7 @@ func TestDecrementWithUint8(t *testing.T) {
} }
func TestDecrementWithUint16(t *testing.T) { func TestDecrementWithUint16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint16", uint16(5), DefaultExpiration) tc.Set("uint16", uint16(5), DefaultExpiration)
err := tc.Decrement("uint16", 2) err := tc.Decrement("uint16", 2)
if err != nil { if err != nil {
@ -542,7 +542,7 @@ func TestDecrementWithUint16(t *testing.T) {
} }
func TestDecrementWithUint32(t *testing.T) { func TestDecrementWithUint32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint32", uint32(5), DefaultExpiration) tc.Set("uint32", uint32(5), DefaultExpiration)
err := tc.Decrement("uint32", 2) err := tc.Decrement("uint32", 2)
if err != nil { if err != nil {
@ -558,7 +558,7 @@ func TestDecrementWithUint32(t *testing.T) {
} }
func TestDecrementWithUint64(t *testing.T) { func TestDecrementWithUint64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint64", uint64(5), DefaultExpiration) tc.Set("uint64", uint64(5), DefaultExpiration)
err := tc.Decrement("uint64", 2) err := tc.Decrement("uint64", 2)
if err != nil { if err != nil {
@ -574,7 +574,7 @@ func TestDecrementWithUint64(t *testing.T) {
} }
func TestDecrementWithFloat32(t *testing.T) { func TestDecrementWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(5.5), DefaultExpiration) tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.Decrement("float32", 2) err := tc.Decrement("float32", 2)
if err != nil { if err != nil {
@ -590,7 +590,7 @@ func TestDecrementWithFloat32(t *testing.T) {
} }
func TestDecrementWithFloat64(t *testing.T) { func TestDecrementWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(5.5), DefaultExpiration) tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.Decrement("float64", 2) err := tc.Decrement("float64", 2)
if err != nil { if err != nil {
@ -606,7 +606,7 @@ func TestDecrementWithFloat64(t *testing.T) {
} }
func TestDecrementFloatWithFloat32(t *testing.T) { func TestDecrementFloatWithFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(5.5), DefaultExpiration) tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.DecrementFloat("float32", 2) err := tc.DecrementFloat("float32", 2)
if err != nil { if err != nil {
@ -622,7 +622,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) {
} }
func TestDecrementFloatWithFloat64(t *testing.T) { func TestDecrementFloatWithFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(5.5), DefaultExpiration) tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.DecrementFloat("float64", 2) err := tc.DecrementFloat("float64", 2)
if err != nil { if err != nil {
@ -638,7 +638,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) {
} }
func TestIncrementInt(t *testing.T) { func TestIncrementInt(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint", 1, DefaultExpiration) tc.Set("tint", 1, DefaultExpiration)
n, err := tc.IncrementInt("tint", 2) n, err := tc.IncrementInt("tint", 2)
if err != nil { if err != nil {
@ -657,7 +657,7 @@ func TestIncrementInt(t *testing.T) {
} }
func TestIncrementInt8(t *testing.T) { func TestIncrementInt8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint8", int8(1), DefaultExpiration) tc.Set("tint8", int8(1), DefaultExpiration)
n, err := tc.IncrementInt8("tint8", 2) n, err := tc.IncrementInt8("tint8", 2)
if err != nil { if err != nil {
@ -676,7 +676,7 @@ func TestIncrementInt8(t *testing.T) {
} }
func TestIncrementInt16(t *testing.T) { func TestIncrementInt16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint16", int16(1), DefaultExpiration) tc.Set("tint16", int16(1), DefaultExpiration)
n, err := tc.IncrementInt16("tint16", 2) n, err := tc.IncrementInt16("tint16", 2)
if err != nil { if err != nil {
@ -695,7 +695,7 @@ func TestIncrementInt16(t *testing.T) {
} }
func TestIncrementInt32(t *testing.T) { func TestIncrementInt32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint32", int32(1), DefaultExpiration) tc.Set("tint32", int32(1), DefaultExpiration)
n, err := tc.IncrementInt32("tint32", 2) n, err := tc.IncrementInt32("tint32", 2)
if err != nil { if err != nil {
@ -714,7 +714,7 @@ func TestIncrementInt32(t *testing.T) {
} }
func TestIncrementInt64(t *testing.T) { func TestIncrementInt64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tint64", int64(1), DefaultExpiration) tc.Set("tint64", int64(1), DefaultExpiration)
n, err := tc.IncrementInt64("tint64", 2) n, err := tc.IncrementInt64("tint64", 2)
if err != nil { if err != nil {
@ -733,7 +733,7 @@ func TestIncrementInt64(t *testing.T) {
} }
func TestIncrementUint(t *testing.T) { func TestIncrementUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint", uint(1), DefaultExpiration) tc.Set("tuint", uint(1), DefaultExpiration)
n, err := tc.IncrementUint("tuint", 2) n, err := tc.IncrementUint("tuint", 2)
if err != nil { if err != nil {
@ -752,7 +752,7 @@ func TestIncrementUint(t *testing.T) {
} }
func TestIncrementUintptr(t *testing.T) { func TestIncrementUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration) tc.Set("tuintptr", uintptr(1), DefaultExpiration)
n, err := tc.IncrementUintptr("tuintptr", 2) n, err := tc.IncrementUintptr("tuintptr", 2)
if err != nil { if err != nil {
@ -771,7 +771,7 @@ func TestIncrementUintptr(t *testing.T) {
} }
func TestIncrementUint8(t *testing.T) { func TestIncrementUint8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint8", uint8(1), DefaultExpiration) tc.Set("tuint8", uint8(1), DefaultExpiration)
n, err := tc.IncrementUint8("tuint8", 2) n, err := tc.IncrementUint8("tuint8", 2)
if err != nil { if err != nil {
@ -790,7 +790,7 @@ func TestIncrementUint8(t *testing.T) {
} }
func TestIncrementUint16(t *testing.T) { func TestIncrementUint16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint16", uint16(1), DefaultExpiration) tc.Set("tuint16", uint16(1), DefaultExpiration)
n, err := tc.IncrementUint16("tuint16", 2) n, err := tc.IncrementUint16("tuint16", 2)
if err != nil { if err != nil {
@ -809,7 +809,7 @@ func TestIncrementUint16(t *testing.T) {
} }
func TestIncrementUint32(t *testing.T) { func TestIncrementUint32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint32", uint32(1), DefaultExpiration) tc.Set("tuint32", uint32(1), DefaultExpiration)
n, err := tc.IncrementUint32("tuint32", 2) n, err := tc.IncrementUint32("tuint32", 2)
if err != nil { if err != nil {
@ -828,7 +828,7 @@ func TestIncrementUint32(t *testing.T) {
} }
func TestIncrementUint64(t *testing.T) { func TestIncrementUint64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("tuint64", uint64(1), DefaultExpiration) tc.Set("tuint64", uint64(1), DefaultExpiration)
n, err := tc.IncrementUint64("tuint64", 2) n, err := tc.IncrementUint64("tuint64", 2)
if err != nil { if err != nil {
@ -847,7 +847,7 @@ func TestIncrementUint64(t *testing.T) {
} }
func TestIncrementFloat32(t *testing.T) { func TestIncrementFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(1.5), DefaultExpiration) tc.Set("float32", float32(1.5), DefaultExpiration)
n, err := tc.IncrementFloat32("float32", 2) n, err := tc.IncrementFloat32("float32", 2)
if err != nil { if err != nil {
@ -866,7 +866,7 @@ func TestIncrementFloat32(t *testing.T) {
} }
func TestIncrementFloat64(t *testing.T) { func TestIncrementFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(1.5), DefaultExpiration) tc.Set("float64", float64(1.5), DefaultExpiration)
n, err := tc.IncrementFloat64("float64", 2) n, err := tc.IncrementFloat64("float64", 2)
if err != nil { if err != nil {
@ -885,7 +885,7 @@ func TestIncrementFloat64(t *testing.T) {
} }
func TestDecrementInt8(t *testing.T) { func TestDecrementInt8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int8", int8(5), DefaultExpiration) tc.Set("int8", int8(5), DefaultExpiration)
n, err := tc.DecrementInt8("int8", 2) n, err := tc.DecrementInt8("int8", 2)
if err != nil { if err != nil {
@ -904,7 +904,7 @@ func TestDecrementInt8(t *testing.T) {
} }
func TestDecrementInt16(t *testing.T) { func TestDecrementInt16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int16", int16(5), DefaultExpiration) tc.Set("int16", int16(5), DefaultExpiration)
n, err := tc.DecrementInt16("int16", 2) n, err := tc.DecrementInt16("int16", 2)
if err != nil { if err != nil {
@ -923,7 +923,7 @@ func TestDecrementInt16(t *testing.T) {
} }
func TestDecrementInt32(t *testing.T) { func TestDecrementInt32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int32", int32(5), DefaultExpiration) tc.Set("int32", int32(5), DefaultExpiration)
n, err := tc.DecrementInt32("int32", 2) n, err := tc.DecrementInt32("int32", 2)
if err != nil { if err != nil {
@ -942,7 +942,7 @@ func TestDecrementInt32(t *testing.T) {
} }
func TestDecrementInt64(t *testing.T) { func TestDecrementInt64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int64", int64(5), DefaultExpiration) tc.Set("int64", int64(5), DefaultExpiration)
n, err := tc.DecrementInt64("int64", 2) n, err := tc.DecrementInt64("int64", 2)
if err != nil { if err != nil {
@ -961,7 +961,7 @@ func TestDecrementInt64(t *testing.T) {
} }
func TestDecrementUint(t *testing.T) { func TestDecrementUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint", uint(5), DefaultExpiration) tc.Set("uint", uint(5), DefaultExpiration)
n, err := tc.DecrementUint("uint", 2) n, err := tc.DecrementUint("uint", 2)
if err != nil { if err != nil {
@ -980,7 +980,7 @@ func TestDecrementUint(t *testing.T) {
} }
func TestDecrementUintptr(t *testing.T) { func TestDecrementUintptr(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration) tc.Set("uintptr", uintptr(5), DefaultExpiration)
n, err := tc.DecrementUintptr("uintptr", 2) n, err := tc.DecrementUintptr("uintptr", 2)
if err != nil { if err != nil {
@ -999,7 +999,7 @@ func TestDecrementUintptr(t *testing.T) {
} }
func TestDecrementUint8(t *testing.T) { func TestDecrementUint8(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint8", uint8(5), DefaultExpiration) tc.Set("uint8", uint8(5), DefaultExpiration)
n, err := tc.DecrementUint8("uint8", 2) n, err := tc.DecrementUint8("uint8", 2)
if err != nil { if err != nil {
@ -1018,7 +1018,7 @@ func TestDecrementUint8(t *testing.T) {
} }
func TestDecrementUint16(t *testing.T) { func TestDecrementUint16(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint16", uint16(5), DefaultExpiration) tc.Set("uint16", uint16(5), DefaultExpiration)
n, err := tc.DecrementUint16("uint16", 2) n, err := tc.DecrementUint16("uint16", 2)
if err != nil { if err != nil {
@ -1037,7 +1037,7 @@ func TestDecrementUint16(t *testing.T) {
} }
func TestDecrementUint32(t *testing.T) { func TestDecrementUint32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint32", uint32(5), DefaultExpiration) tc.Set("uint32", uint32(5), DefaultExpiration)
n, err := tc.DecrementUint32("uint32", 2) n, err := tc.DecrementUint32("uint32", 2)
if err != nil { if err != nil {
@ -1056,7 +1056,7 @@ func TestDecrementUint32(t *testing.T) {
} }
func TestDecrementUint64(t *testing.T) { func TestDecrementUint64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint64", uint64(5), DefaultExpiration) tc.Set("uint64", uint64(5), DefaultExpiration)
n, err := tc.DecrementUint64("uint64", 2) n, err := tc.DecrementUint64("uint64", 2)
if err != nil { if err != nil {
@ -1075,7 +1075,7 @@ func TestDecrementUint64(t *testing.T) {
} }
func TestDecrementFloat32(t *testing.T) { func TestDecrementFloat32(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float32", float32(5), DefaultExpiration) tc.Set("float32", float32(5), DefaultExpiration)
n, err := tc.DecrementFloat32("float32", 2) n, err := tc.DecrementFloat32("float32", 2)
if err != nil { if err != nil {
@ -1094,7 +1094,7 @@ func TestDecrementFloat32(t *testing.T) {
} }
func TestDecrementFloat64(t *testing.T) { func TestDecrementFloat64(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("float64", float64(5), DefaultExpiration) tc.Set("float64", float64(5), DefaultExpiration)
n, err := tc.DecrementFloat64("float64", 2) n, err := tc.DecrementFloat64("float64", 2)
if err != nil { if err != nil {
@ -1113,7 +1113,7 @@ func TestDecrementFloat64(t *testing.T) {
} }
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
err := tc.Add("foo", "bar", DefaultExpiration) err := tc.Add("foo", "bar", DefaultExpiration)
if err != nil { if err != nil {
t.Error("Couldn't add foo even though it shouldn't exist") 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) { func TestReplace(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
err := tc.Replace("foo", "bar", DefaultExpiration) err := tc.Replace("foo", "bar", DefaultExpiration)
if err == nil { if err == nil {
t.Error("Replaced foo when it shouldn't exist") t.Error("Replaced foo when it shouldn't exist")
@ -1138,7 +1138,7 @@ func TestReplace(t *testing.T) {
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
tc.Delete("foo") tc.Delete("foo")
x, found := tc.Get("foo") x, found := tc.Get("foo")
@ -1151,7 +1151,7 @@ func TestDelete(t *testing.T) {
} }
func TestItemCount(t *testing.T) { func TestItemCount(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", "1", DefaultExpiration) tc.Set("foo", "1", DefaultExpiration)
tc.Set("bar", "2", DefaultExpiration) tc.Set("bar", "2", DefaultExpiration)
tc.Set("baz", "3", DefaultExpiration) tc.Set("baz", "3", DefaultExpiration)
@ -1161,7 +1161,7 @@ func TestItemCount(t *testing.T) {
} }
func TestFlush(t *testing.T) { func TestFlush(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
tc.Set("baz", "yes", DefaultExpiration) tc.Set("baz", "yes", DefaultExpiration)
tc.Flush() tc.Flush()
@ -1182,7 +1182,7 @@ func TestFlush(t *testing.T) {
} }
func TestIncrementOverflowInt(t *testing.T) { func TestIncrementOverflowInt(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("int8", int8(127), DefaultExpiration) tc.Set("int8", int8(127), DefaultExpiration)
err := tc.Increment("int8", 1) err := tc.Increment("int8", 1)
if err != nil { if err != nil {
@ -1197,7 +1197,7 @@ func TestIncrementOverflowInt(t *testing.T) {
} }
func TestIncrementOverflowUint(t *testing.T) { func TestIncrementOverflowUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint8", uint8(255), DefaultExpiration) tc.Set("uint8", uint8(255), DefaultExpiration)
err := tc.Increment("uint8", 1) err := tc.Increment("uint8", 1)
if err != nil { if err != nil {
@ -1211,7 +1211,7 @@ func TestIncrementOverflowUint(t *testing.T) {
} }
func TestDecrementUnderflowUint(t *testing.T) { func TestDecrementUnderflowUint(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("uint8", uint8(0), DefaultExpiration) tc.Set("uint8", uint8(0), DefaultExpiration)
err := tc.Decrement("uint8", 1) err := tc.Decrement("uint8", 1)
if err != nil { if err != nil {
@ -1225,7 +1225,7 @@ func TestDecrementUnderflowUint(t *testing.T) {
} }
func TestCacheSerialization(t *testing.T) { func TestCacheSerialization(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
testFillAndSerialize(t, tc) testFillAndSerialize(t, tc)
// Check if gob.Register behaves properly even after multiple gob.Register // 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) t.Fatal("Couldn't save cache to fp:", err)
} }
oc := New(DefaultExpiration, 0, 0) oc := New(DefaultExpiration, 0)
err = oc.Load(fp) err = oc.Load(fp)
if err != nil { if err != nil {
t.Fatal("Couldn't load cache from fp:", err) 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) { func TestFileSerialization(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Add("a", "a", DefaultExpiration) tc.Add("a", "a", DefaultExpiration)
tc.Add("b", "b", DefaultExpiration) tc.Add("b", "b", DefaultExpiration)
f, err := ioutil.TempFile("", "go-cache-cache.dat") f, err := ioutil.TempFile("", "go-cache-cache.dat")
@ -1363,7 +1363,7 @@ func TestFileSerialization(t *testing.T) {
f.Close() f.Close()
tc.SaveFile(fname) tc.SaveFile(fname)
oc := New(DefaultExpiration, 0, 0) oc := New(DefaultExpiration, 0)
oc.Add("a", "aa", 0) // this should not be overwritten oc.Add("a", "aa", 0) // this should not be overwritten
err = oc.LoadFile(fname) err = oc.LoadFile(fname)
if err != nil { if err != nil {
@ -1391,7 +1391,7 @@ func TestFileSerialization(t *testing.T) {
} }
func TestSerializeUnserializable(t *testing.T) { func TestSerializeUnserializable(t *testing.T) {
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
ch := make(chan bool, 1) ch := make(chan bool, 1)
ch <- true ch <- true
tc.Set("chan", ch, DefaultExpiration) tc.Set("chan", ch, DefaultExpiration)
@ -1404,7 +1404,7 @@ func TestSerializeUnserializable(t *testing.T) {
func BenchmarkCacheGet(b *testing.B) { func BenchmarkCacheGet(b *testing.B) {
b.StopTimer() b.StopTimer()
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@ -1428,7 +1428,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
func BenchmarkCacheGetConcurrent(b *testing.B) { func BenchmarkCacheGetConcurrent(b *testing.B) {
b.StopTimer() b.StopTimer()
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
workers := runtime.NumCPU() workers := runtime.NumCPU()
@ -1476,7 +1476,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
// in sharded_test.go. // in sharded_test.go.
b.StopTimer() b.StopTimer()
n := 10000 n := 10000
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
keys := make([]string, n) keys := make([]string, n)
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
k := "foo" + strconv.Itoa(n) k := "foo" + strconv.Itoa(n)
@ -1500,7 +1500,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
func BenchmarkCacheSet(b *testing.B) { func BenchmarkCacheSet(b *testing.B) {
b.StopTimer() b.StopTimer()
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
@ -1521,7 +1521,7 @@ func BenchmarkRWMutexMapSet(b *testing.B) {
func BenchmarkCacheSetDelete(b *testing.B) { func BenchmarkCacheSetDelete(b *testing.B) {
b.StopTimer() b.StopTimer()
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", DefaultExpiration) tc.Set("foo", "bar", DefaultExpiration)
@ -1546,7 +1546,7 @@ func BenchmarkRWMutexMapSetDelete(b *testing.B) {
func BenchmarkCacheSetDeleteSingleLock(b *testing.B) { func BenchmarkCacheSetDeleteSingleLock(b *testing.B) {
b.StopTimer() b.StopTimer()
tc := New(DefaultExpiration, 0, 0) tc := New(DefaultExpiration, 0)
b.StartTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
tc.Lock() tc.Lock()