sm
/
cache
1
0
Fork 0

Add NoExpiration and DefaultExpiration constants and use them (thanks to Jason Mooberry)

This commit is contained in:
Patrick Mylund Nielsen 2014-12-22 01:46:22 -05:00
parent 73a30b2033
commit 46827c6a61
2 changed files with 183 additions and 178 deletions

View File

@ -23,6 +23,11 @@ func (item *Item) Expired() bool {
return item.Expiration.Before(time.Now())
}
const (
NoExpiration time.Duration = -1
DefaultExpiration time.Duration = 0
)
type Cache struct {
*cache
// If this is confusing, see the comment at the bottom of New()
@ -35,9 +40,9 @@ type cache struct {
janitor *janitor
}
// Add an item to the cache, replacing any existing item. If the duration is 0,
// the cache's default expiration time is used. If it is -1, the item never
// expires.
// Add an item to the cache, replacing any existing item. If the duration is 0
// (DefaultExpiration), the cache's default expiration time is used. If it is -1
// (NoExpiration), the item never expires.
func (c *cache) Set(k string, x interface{}, d time.Duration) {
c.Lock()
c.set(k, x, d)
@ -48,7 +53,7 @@ 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
if d == 0 {
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
@ -982,10 +987,10 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]*Item)
}
// Return a new cache with a given default expiration duration and cleanup
// interval. If the expiration duration is less than 1, 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 DeleteExpired.
// interval. 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 DeleteExpired.
func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
items := make(map[string]*Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)

View File

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