sm
/
cache
1
0
Fork 0
This commit is contained in:
Jason Mooberry 2014-11-10 20:52:28 +00:00
commit a90c8f0c44
3 changed files with 100 additions and 95 deletions

8
README
View File

@ -23,12 +23,12 @@ import "github.com/pmylund/go-cache"
c := cache.New(5*time.Minute, 30*time.Second)
// Set the value of the key "foo" to "bar", with the default expiration time
c.Set("foo", "bar", 0)
c.Set("foo", "bar", cache.DefaultExpiration)
// Set the value of the key "baz" to 42, with no expiration time
// (the item won't be removed until it is re-set, or removed using
// c.Delete("baz")
c.Set("baz", 42, -1)
c.Set("baz", 42, cache.NoExpiration)
// Get the string associated with the key "foo" from the cache
foo, found := c.Get("foo")
@ -61,7 +61,7 @@ if x, found := c.Get("foo"); found {
// foo can then be passed around freely as a string
// Want performance? Store pointers!
c.Set("foo", &MyStruct, 0)
c.Set("foo", &MyStruct, cache.DefaultExpiration)
if x, found := c.Get("foo"); found {
foo := x.(*MyStruct)
...
@ -73,7 +73,7 @@ if x, found := c.Get("foo"); found {
// pointer you've stored in the cache, retrieving that pointer with Get will
// point you to the same data:
foo := &MyStruct{Num: 1}
c.Set("foo", foo, 0)
c.Set("foo", foo, cache.DefaultExpiration)
...
x, _ := c.Get("foo")
foo := x.(*MyStruct)

View File

@ -83,6 +83,11 @@ type cache struct {
janitor *janitor
}
const (
NoExpiration time.Duration = -1
DefaultExpiration time.Duration = 0
)
// 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.
@ -96,7 +101,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 {

View File

@ -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)
@ -108,7 +108,7 @@ func TestCacheTimes(t *testing.T) {
func TestStorePointerToStruct(t *testing.T) {
tc := New(0, 0)
tc.Set("foo", &TestStruct{Num: 1}, 0)
tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration)
x, found := tc.Get("foo")
if !found {
t.Fatal("*TestStruct was not found for foo")
@ -128,7 +128,7 @@ func TestStorePointerToStruct(t *testing.T) {
func TestIncrementWithInt(t *testing.T) {
tc := New(0, 0)
tc.Set("tint", 1, 0)
tc.Set("tint", 1, DefaultExpiration)
err := tc.Increment("tint", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -144,7 +144,7 @@ func TestIncrementWithInt(t *testing.T) {
func TestIncrementWithInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("tint8", int8(1), 0)
tc.Set("tint8", int8(1), DefaultExpiration)
err := tc.Increment("tint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -160,7 +160,7 @@ func TestIncrementWithInt8(t *testing.T) {
func TestIncrementWithInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("tint16", int16(1), 0)
tc.Set("tint16", int16(1), DefaultExpiration)
err := tc.Increment("tint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -176,7 +176,7 @@ func TestIncrementWithInt16(t *testing.T) {
func TestIncrementWithInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint32", int32(1), 0)
tc.Set("tint32", int32(1), DefaultExpiration)
err := tc.Increment("tint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -192,7 +192,7 @@ func TestIncrementWithInt32(t *testing.T) {
func TestIncrementWithInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("tint64", int64(1), 0)
tc.Set("tint64", int64(1), DefaultExpiration)
err := tc.Increment("tint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -208,7 +208,7 @@ func TestIncrementWithInt64(t *testing.T) {
func TestIncrementWithUint(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint", uint(1), 0)
tc.Set("tuint", uint(1), DefaultExpiration)
err := tc.Increment("tuint", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -224,7 +224,7 @@ func TestIncrementWithUint(t *testing.T) {
func TestIncrementWithUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("tuintptr", uintptr(1), 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
err := tc.Increment("tuintptr", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -241,7 +241,7 @@ func TestIncrementWithUintptr(t *testing.T) {
func TestIncrementWithUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint8", uint8(1), 0)
tc.Set("tuint8", uint8(1), DefaultExpiration)
err := tc.Increment("tuint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -257,7 +257,7 @@ func TestIncrementWithUint8(t *testing.T) {
func TestIncrementWithUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint16", uint16(1), 0)
tc.Set("tuint16", uint16(1), DefaultExpiration)
err := tc.Increment("tuint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -274,7 +274,7 @@ func TestIncrementWithUint16(t *testing.T) {
func TestIncrementWithUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint32", uint32(1), 0)
tc.Set("tuint32", uint32(1), DefaultExpiration)
err := tc.Increment("tuint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -290,7 +290,7 @@ func TestIncrementWithUint32(t *testing.T) {
func TestIncrementWithUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint64", uint64(1), 0)
tc.Set("tuint64", uint64(1), DefaultExpiration)
err := tc.Increment("tuint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -307,7 +307,7 @@ func TestIncrementWithUint64(t *testing.T) {
func TestIncrementWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(1.5), 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.Increment("float32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -323,7 +323,7 @@ func TestIncrementWithFloat32(t *testing.T) {
func TestIncrementWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(1.5), 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.Increment("float64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -339,7 +339,7 @@ func TestIncrementWithFloat64(t *testing.T) {
func TestIncrementFloatWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(1.5), 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
err := tc.IncrementFloat("float32", 2)
if err != nil {
t.Error("Error incrementfloating:", err)
@ -355,7 +355,7 @@ func TestIncrementFloatWithFloat32(t *testing.T) {
func TestIncrementFloatWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(1.5), 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
err := tc.IncrementFloat("float64", 2)
if err != nil {
t.Error("Error incrementfloating:", err)
@ -371,7 +371,7 @@ func TestIncrementFloatWithFloat64(t *testing.T) {
func TestDecrementWithInt(t *testing.T) {
tc := New(0, 0)
tc.Set("int", int(5), 0)
tc.Set("int", int(5), DefaultExpiration)
err := tc.Decrement("int", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -387,7 +387,7 @@ func TestDecrementWithInt(t *testing.T) {
func TestDecrementWithInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("int8", int8(5), 0)
tc.Set("int8", int8(5), DefaultExpiration)
err := tc.Decrement("int8", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -403,7 +403,7 @@ func TestDecrementWithInt8(t *testing.T) {
func TestDecrementWithInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("int16", int16(5), 0)
tc.Set("int16", int16(5), DefaultExpiration)
err := tc.Decrement("int16", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -419,7 +419,7 @@ func TestDecrementWithInt16(t *testing.T) {
func TestDecrementWithInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("int32", int32(5), 0)
tc.Set("int32", int32(5), DefaultExpiration)
err := tc.Decrement("int32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -435,7 +435,7 @@ func TestDecrementWithInt32(t *testing.T) {
func TestDecrementWithInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("int64", int64(5), 0)
tc.Set("int64", int64(5), DefaultExpiration)
err := tc.Decrement("int64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -451,7 +451,7 @@ func TestDecrementWithInt64(t *testing.T) {
func TestDecrementWithUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint", uint(5), 0)
tc.Set("uint", uint(5), DefaultExpiration)
err := tc.Decrement("uint", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -467,7 +467,7 @@ func TestDecrementWithUint(t *testing.T) {
func TestDecrementWithUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("uintptr", uintptr(5), 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration)
err := tc.Decrement("uintptr", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -483,7 +483,7 @@ func TestDecrementWithUintptr(t *testing.T) {
func TestDecrementWithUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(5), 0)
tc.Set("uint8", uint8(5), DefaultExpiration)
err := tc.Decrement("uint8", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -499,7 +499,7 @@ func TestDecrementWithUint8(t *testing.T) {
func TestDecrementWithUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("uint16", uint16(5), 0)
tc.Set("uint16", uint16(5), DefaultExpiration)
err := tc.Decrement("uint16", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -515,7 +515,7 @@ func TestDecrementWithUint16(t *testing.T) {
func TestDecrementWithUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("uint32", uint32(5), 0)
tc.Set("uint32", uint32(5), DefaultExpiration)
err := tc.Decrement("uint32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -531,7 +531,7 @@ func TestDecrementWithUint32(t *testing.T) {
func TestDecrementWithUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("uint64", uint64(5), 0)
tc.Set("uint64", uint64(5), DefaultExpiration)
err := tc.Decrement("uint64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -547,7 +547,7 @@ func TestDecrementWithUint64(t *testing.T) {
func TestDecrementWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5.5), 0)
tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.Decrement("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -563,7 +563,7 @@ func TestDecrementWithFloat32(t *testing.T) {
func TestDecrementWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5.5), 0)
tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.Decrement("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -579,7 +579,7 @@ func TestDecrementWithFloat64(t *testing.T) {
func TestDecrementFloatWithFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5.5), 0)
tc.Set("float32", float32(5.5), DefaultExpiration)
err := tc.DecrementFloat("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -595,7 +595,7 @@ func TestDecrementFloatWithFloat32(t *testing.T) {
func TestDecrementFloatWithFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5.5), 0)
tc.Set("float64", float64(5.5), DefaultExpiration)
err := tc.DecrementFloat("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -611,7 +611,7 @@ func TestDecrementFloatWithFloat64(t *testing.T) {
func TestIncrementInt(t *testing.T) {
tc := New(0, 0)
tc.Set("tint", 1, 0)
tc.Set("tint", 1, DefaultExpiration)
n, err := tc.IncrementInt("tint", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -630,7 +630,7 @@ func TestIncrementInt(t *testing.T) {
func TestIncrementInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("tint8", int8(1), 0)
tc.Set("tint8", int8(1), DefaultExpiration)
n, err := tc.IncrementInt8("tint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -649,7 +649,7 @@ func TestIncrementInt8(t *testing.T) {
func TestIncrementInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("tint16", int16(1), 0)
tc.Set("tint16", int16(1), DefaultExpiration)
n, err := tc.IncrementInt16("tint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -668,7 +668,7 @@ func TestIncrementInt16(t *testing.T) {
func TestIncrementInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("tint32", int32(1), 0)
tc.Set("tint32", int32(1), DefaultExpiration)
n, err := tc.IncrementInt32("tint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -687,7 +687,7 @@ func TestIncrementInt32(t *testing.T) {
func TestIncrementInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("tint64", int64(1), 0)
tc.Set("tint64", int64(1), DefaultExpiration)
n, err := tc.IncrementInt64("tint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -706,7 +706,7 @@ func TestIncrementInt64(t *testing.T) {
func TestIncrementUint(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint", uint(1), 0)
tc.Set("tuint", uint(1), DefaultExpiration)
n, err := tc.IncrementUint("tuint", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -725,7 +725,7 @@ func TestIncrementUint(t *testing.T) {
func TestIncrementUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("tuintptr", uintptr(1), 0)
tc.Set("tuintptr", uintptr(1), DefaultExpiration)
n, err := tc.IncrementUintptr("tuintptr", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -744,7 +744,7 @@ func TestIncrementUintptr(t *testing.T) {
func TestIncrementUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint8", uint8(1), 0)
tc.Set("tuint8", uint8(1), DefaultExpiration)
n, err := tc.IncrementUint8("tuint8", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -763,7 +763,7 @@ func TestIncrementUint8(t *testing.T) {
func TestIncrementUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint16", uint16(1), 0)
tc.Set("tuint16", uint16(1), DefaultExpiration)
n, err := tc.IncrementUint16("tuint16", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -782,7 +782,7 @@ func TestIncrementUint16(t *testing.T) {
func TestIncrementUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint32", uint32(1), 0)
tc.Set("tuint32", uint32(1), DefaultExpiration)
n, err := tc.IncrementUint32("tuint32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -801,7 +801,7 @@ func TestIncrementUint32(t *testing.T) {
func TestIncrementUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("tuint64", uint64(1), 0)
tc.Set("tuint64", uint64(1), DefaultExpiration)
n, err := tc.IncrementUint64("tuint64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -820,7 +820,7 @@ func TestIncrementUint64(t *testing.T) {
func TestIncrementFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(1.5), 0)
tc.Set("float32", float32(1.5), DefaultExpiration)
n, err := tc.IncrementFloat32("float32", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -839,7 +839,7 @@ func TestIncrementFloat32(t *testing.T) {
func TestIncrementFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(1.5), 0)
tc.Set("float64", float64(1.5), DefaultExpiration)
n, err := tc.IncrementFloat64("float64", 2)
if err != nil {
t.Error("Error incrementing:", err)
@ -858,7 +858,7 @@ func TestIncrementFloat64(t *testing.T) {
func TestDecrementInt8(t *testing.T) {
tc := New(0, 0)
tc.Set("int8", int8(5), 0)
tc.Set("int8", int8(5), DefaultExpiration)
n, err := tc.DecrementInt8("int8", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -877,7 +877,7 @@ func TestDecrementInt8(t *testing.T) {
func TestDecrementInt16(t *testing.T) {
tc := New(0, 0)
tc.Set("int16", int16(5), 0)
tc.Set("int16", int16(5), DefaultExpiration)
n, err := tc.DecrementInt16("int16", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -896,7 +896,7 @@ func TestDecrementInt16(t *testing.T) {
func TestDecrementInt32(t *testing.T) {
tc := New(0, 0)
tc.Set("int32", int32(5), 0)
tc.Set("int32", int32(5), DefaultExpiration)
n, err := tc.DecrementInt32("int32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -915,7 +915,7 @@ func TestDecrementInt32(t *testing.T) {
func TestDecrementInt64(t *testing.T) {
tc := New(0, 0)
tc.Set("int64", int64(5), 0)
tc.Set("int64", int64(5), DefaultExpiration)
n, err := tc.DecrementInt64("int64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -934,7 +934,7 @@ func TestDecrementInt64(t *testing.T) {
func TestDecrementUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint", uint(5), 0)
tc.Set("uint", uint(5), DefaultExpiration)
n, err := tc.DecrementUint("uint", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -953,7 +953,7 @@ func TestDecrementUint(t *testing.T) {
func TestDecrementUintptr(t *testing.T) {
tc := New(0, 0)
tc.Set("uintptr", uintptr(5), 0)
tc.Set("uintptr", uintptr(5), DefaultExpiration)
n, err := tc.DecrementUintptr("uintptr", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -972,7 +972,7 @@ func TestDecrementUintptr(t *testing.T) {
func TestDecrementUint8(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(5), 0)
tc.Set("uint8", uint8(5), DefaultExpiration)
n, err := tc.DecrementUint8("uint8", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -991,7 +991,7 @@ func TestDecrementUint8(t *testing.T) {
func TestDecrementUint16(t *testing.T) {
tc := New(0, 0)
tc.Set("uint16", uint16(5), 0)
tc.Set("uint16", uint16(5), DefaultExpiration)
n, err := tc.DecrementUint16("uint16", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -1010,7 +1010,7 @@ func TestDecrementUint16(t *testing.T) {
func TestDecrementUint32(t *testing.T) {
tc := New(0, 0)
tc.Set("uint32", uint32(5), 0)
tc.Set("uint32", uint32(5), DefaultExpiration)
n, err := tc.DecrementUint32("uint32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -1029,7 +1029,7 @@ func TestDecrementUint32(t *testing.T) {
func TestDecrementUint64(t *testing.T) {
tc := New(0, 0)
tc.Set("uint64", uint64(5), 0)
tc.Set("uint64", uint64(5), DefaultExpiration)
n, err := tc.DecrementUint64("uint64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -1048,7 +1048,7 @@ func TestDecrementUint64(t *testing.T) {
func TestDecrementFloat32(t *testing.T) {
tc := New(0, 0)
tc.Set("float32", float32(5), 0)
tc.Set("float32", float32(5), DefaultExpiration)
n, err := tc.DecrementFloat32("float32", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -1067,7 +1067,7 @@ func TestDecrementFloat32(t *testing.T) {
func TestDecrementFloat64(t *testing.T) {
tc := New(0, 0)
tc.Set("float64", float64(5), 0)
tc.Set("float64", float64(5), DefaultExpiration)
n, err := tc.DecrementFloat64("float64", 2)
if err != nil {
t.Error("Error decrementing:", err)
@ -1086,11 +1086,11 @@ func TestDecrementFloat64(t *testing.T) {
func TestAdd(t *testing.T) {
tc := New(0, 0)
err := tc.Add("foo", "bar", 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")
}
@ -1098,12 +1098,12 @@ func TestAdd(t *testing.T) {
func TestReplace(t *testing.T) {
tc := New(0, 0)
err := tc.Replace("foo", "bar", 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")
}
@ -1111,7 +1111,7 @@ func TestReplace(t *testing.T) {
func TestDelete(t *testing.T) {
tc := New(0, 0)
tc.Set("foo", "bar", 0)
tc.Set("foo", "bar", DefaultExpiration)
tc.Delete("foo")
x, found := tc.Get("foo")
if found {
@ -1124,9 +1124,9 @@ 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.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)
}
@ -1134,8 +1134,8 @@ func TestItemCount(t *testing.T) {
func TestFlush(t *testing.T) {
tc := New(0, 0)
tc.Set("foo", "bar", 0)
tc.Set("baz", "yes", 0)
tc.Set("foo", "bar", DefaultExpiration)
tc.Set("baz", "yes", DefaultExpiration)
tc.Flush()
x, found := tc.Get("foo")
if found {
@ -1155,7 +1155,7 @@ func TestFlush(t *testing.T) {
func TestIncrementOverflowInt(t *testing.T) {
tc := New(0, 0)
tc.Set("int8", int8(127), 0)
tc.Set("int8", int8(127), DefaultExpiration)
err := tc.Increment("int8", 1)
if err != nil {
t.Error("Error incrementing int8:", err)
@ -1170,7 +1170,7 @@ func TestIncrementOverflowInt(t *testing.T) {
func TestIncrementOverflowUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(255), 0)
tc.Set("uint8", uint8(255), DefaultExpiration)
err := tc.Increment("uint8", 1)
if err != nil {
t.Error("Error incrementing int8:", err)
@ -1184,7 +1184,7 @@ func TestIncrementOverflowUint(t *testing.T) {
func TestDecrementUnderflowUint(t *testing.T) {
tc := New(0, 0)
tc.Set("uint8", uint8(0), 0)
tc.Set("uint8", uint8(0), DefaultExpiration)
err := tc.Decrement("uint8", 1)
if err != nil {
t.Error("Error decrementing int8:", err)
@ -1206,11 +1206,11 @@ 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},
@ -1325,8 +1325,8 @@ 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.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)
@ -1336,7 +1336,7 @@ func TestFileSerialization(t *testing.T) {
tc.SaveFile(fname)
oc := New(0, 0)
oc.Add("a", "aa", 0) // this should not be overwritten
oc.Add("a", "aa", DefaultExpiration) // this should not be overwritten
err = oc.LoadFile(fname)
if err != nil {
t.Error(err)
@ -1366,7 +1366,7 @@ func TestSerializeUnserializable(t *testing.T) {
tc := New(0, 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" {
@ -1377,7 +1377,7 @@ func TestSerializeUnserializable(t *testing.T) {
func BenchmarkCacheGet(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc.Set("foo", "bar", 0)
tc.Set("foo", "bar", DefaultExpiration)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Get("foo")
@ -1401,7 +1401,7 @@ func BenchmarkRWMutexMapGet(b *testing.B) {
func BenchmarkCacheGetConcurrent(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc.Set("foo", "bar", 0)
tc.Set("foo", "bar", DefaultExpiration)
wg := new(sync.WaitGroup)
workers := runtime.NumCPU()
each := b.N / workers
@ -1452,7 +1452,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) {
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)
@ -1477,7 +1477,7 @@ func BenchmarkShardedCacheGetManyConcurrent(b *testing.B) {
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)
@ -1496,10 +1496,10 @@ func BenchmarkShardedCacheGetManyConcurrent(b *testing.B) {
func BenchmarkCacheSet(b *testing.B) {
b.StopTimer()
tc := New(0, 0)
tc := New(0, DefaultExpiration)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", 0)
tc.Set("foo", "bar", DefaultExpiration)
}
}
@ -1520,7 +1520,7 @@ func BenchmarkCacheSetDelete(b *testing.B) {
tc := New(0, 0)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Set("foo", "bar", 0)
tc.Set("foo", "bar", DefaultExpiration)
tc.Delete("foo")
}
}