Skip to content

Commit

Permalink
[patch] remove interface reflection cast from atomic expire variable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Kato authored Feb 5, 2019
1 parent 1795f15 commit 04a7f03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
19 changes: 9 additions & 10 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
35 changes: 17 additions & 18 deletions gache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 04a7f03

Please sign in to comment.