From 04a7f039c21be0a889ad01bef45e5a4dbeae26f7 Mon Sep 17 00:00:00 2001 From: Yusuke Kato Date: Wed, 6 Feb 2019 02:36:46 +0900 Subject: [PATCH] [patch] remove interface reflection cast from atomic expire variable (#12) --- gache.go | 19 +++++++++---------- gache_test.go | 35 +++++++++++++++++------------------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/gache.go b/gache.go index ff7d781..eba86c1 100644 --- a/gache.go +++ b/gache.go @@ -34,7 +34,7 @@ type ( gache struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -64,14 +64,13 @@ func New() Gache { // newGache returns *gache instance func newGache() *gache { g := &gache{ - expire: new(atomic.Value), + expire: int64(time.Second * 30), expChan: make(chan string, 1000), } g.l = uint64(len(g.shards)) for i := range g.shards { g.shards[i] = new(sync.Map) } - g.expire.Store(time.Second * 30) return g } @@ -90,7 +89,7 @@ func (v *value) isValid() bool { // SetDefaultExpire set expire duration func (g *gache) SetDefaultExpire(ex time.Duration) Gache { - g.expire.Store(ex) + atomic.StoreInt64(&g.expire, *(*int64)(unsafe.Pointer(&ex))) return g } @@ -196,31 +195,31 @@ func Get(key string) (interface{}, bool) { } // set sets key-value & expiration to Gache -func (g *gache) set(key string, val interface{}, expire time.Duration) { +func (g *gache) set(key string, val interface{}, expire int64) { g.shards[xxhash.Sum64(*(*[]byte)(unsafe.Pointer(&key)))%g.l].Store(key, &value{ - expire: fastime.UnixNanoNow() + *(*int64)(unsafe.Pointer(&expire)), + expire: fastime.UnixNanoNow() + expire, val: &val, }) } // SetWithExpire sets key-value & expiration to Gache func (g *gache) SetWithExpire(key string, val interface{}, expire time.Duration) { - g.set(key, val, expire) + g.set(key, val, *(*int64)(unsafe.Pointer(&expire))) } // SetWithExpire sets key-value & expiration to Gache func SetWithExpire(key string, val interface{}, expire time.Duration) { - instance.set(key, val, expire) + instance.set(key, val, *(*int64)(unsafe.Pointer(&expire))) } // Set sets key-value to Gache using default expiration func (g *gache) Set(key string, val interface{}) { - g.set(key, val, g.expire.Load().(time.Duration)) + g.set(key, val, atomic.LoadInt64(&g.expire)) } // Set sets key-value to Gache using default expiration func Set(key string, val interface{}) { - instance.set(key, val, instance.expire.Load().(time.Duration)) + instance.set(key, val, atomic.LoadInt64(&instance.expire)) } // Delete deletes value from Gache using key diff --git a/gache_test.go b/gache_test.go index 8e55a07..cb3cc18 100644 --- a/gache_test.go +++ b/gache_test.go @@ -4,7 +4,6 @@ import ( "context" "reflect" "sync" - "sync/atomic" "testing" "time" @@ -88,7 +87,7 @@ func Test_gache_SetDefaultExpire(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -147,7 +146,7 @@ func Test_gache_EnableExpiredHook(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -198,7 +197,7 @@ func Test_gache_DisableExpiredHook(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -249,7 +248,7 @@ func Test_gache_SetExpiredHook(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -308,7 +307,7 @@ func Test_gache_StartExpired(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -348,7 +347,7 @@ func Test_gache_ToMap(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -407,7 +406,7 @@ func Test_gache_get(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -451,7 +450,7 @@ func Test_gache_Get(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -520,7 +519,7 @@ func Test_gache_set(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -529,7 +528,7 @@ func Test_gache_set(t *testing.T) { type args struct { key string val interface{} - expire time.Duration + expire int64 } tests := []struct { name string @@ -558,7 +557,7 @@ func Test_gache_SetWithExpire(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -615,7 +614,7 @@ func Test_gache_Set(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -670,7 +669,7 @@ func Test_gache_Delete(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -723,7 +722,7 @@ func Test_gache_expiration(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -759,7 +758,7 @@ func Test_gache_DeleteExpired(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -818,7 +817,7 @@ func Test_gache_Foreach(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string @@ -879,7 +878,7 @@ func Test_gache_Clear(t *testing.T) { type fields struct { l uint64 shards [255]*sync.Map - expire *atomic.Value + expire int64 expFuncEnabled bool expFunc func(context.Context, string) expChan chan string