From 9cc10f6f2f43eb8e1659c4a7ed966bb8640a6d3e Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Fri, 17 Aug 2012 11:35:20 +0200 Subject: [PATCH] Use a type switch instead, and Use unexported NewSharded in test --- cache.go | 47 ++++++++++++++++++++++------------------------- cache_test.go | 2 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/cache.go b/cache.go index bfbcfe3..5528455 100644 --- a/cache.go +++ b/cache.go @@ -7,7 +7,6 @@ import ( "hash/fnv" "io" "os" - "reflect" "runtime" "sync" "time" @@ -142,38 +141,36 @@ func (c *cache) IncrementFloat(k string, n float64) error { c.Unlock() return fmt.Errorf("item not found") } - - t := reflect.TypeOf(v.Object) - switch t.Kind() { - default: - c.Unlock() - return fmt.Errorf("The value of %s is not an integer", k) - case reflect.Uint: + switch v.Object.(type) { + case int: + v.Object = v.Object.(int) + int(n) + case int8: + v.Object = v.Object.(int8) + int8(n) + case int16: + v.Object = v.Object.(int16) + int16(n) + case int32: + v.Object = v.Object.(int32) + int32(n) + case int64: + v.Object = v.Object.(int64) + int64(n) + case uint: v.Object = v.Object.(uint) + uint(n) - case reflect.Uintptr: + case uintptr: v.Object = v.Object.(uintptr) + uintptr(n) - case reflect.Uint8: + case uint8: v.Object = v.Object.(uint8) + uint8(n) - case reflect.Uint16: + case uint16: v.Object = v.Object.(uint16) + uint16(n) - case reflect.Uint32: + case uint32: v.Object = v.Object.(uint32) + uint32(n) - case reflect.Uint64: + case uint64: v.Object = v.Object.(uint64) + uint64(n) - case reflect.Int: - v.Object = v.Object.(int) + int(n) - case reflect.Int8: - v.Object = v.Object.(int8) + int8(n) - case reflect.Int16: - v.Object = v.Object.(int16) + int16(n) - case reflect.Int32: - v.Object = v.Object.(int32) + int32(n) - case reflect.Int64: - v.Object = v.Object.(int64) + int64(n) - case reflect.Float32: + case float32: v.Object = v.Object.(float32) + float32(n) - case reflect.Float64: + case float64: v.Object = v.Object.(float64) + n + default: + c.Unlock() + return fmt.Errorf("The value of %s is not an integer", k) } c.Unlock() return nil diff --git a/cache_test.go b/cache_test.go index 1f2c82d..68cf133 100644 --- a/cache_test.go +++ b/cache_test.go @@ -734,7 +734,7 @@ func BenchmarkCacheGetManyConcurrent(b *testing.B) { func BenchmarkShardedCacheGetManyConcurrent(b *testing.B) { b.StopTimer() n := 10000 - tsc := NewSharded(20, 0, 0) + tsc := unexportedNewSharded(20, 0, 0) keys := make([]string, n) for i := 0; i < n; i++ { k := "foo" + strconv.Itoa(n)