Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch] feature add Len function that returns object conunts #35

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
glg.Info(v3)
}

glg.Debugf("Len:\t%d", gache.Len())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value of glg.Debugf is not checked (from errcheck)

// set gache default expire time
gc := gache.New().SetDefaultExpire(time.Second * 10)

Expand Down
19 changes: 17 additions & 2 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
SetExpiredHook(f func(context.Context, string)) Gache
SetWithExpire(string, interface{}, time.Duration)
StartExpired(context.Context, time.Duration) Gache
Len() int
ToMap(context.Context) *sync.Map
ToRawMap(context.Context) map[string]interface{}
Write(context.Context, io.Writer) error
Expand Down Expand Up @@ -63,6 +64,7 @@ type (
expFuncEnabled bool
expGroup singleflight.Group
expire int64
l uint64
shards [256]*sync.Map
}

Expand Down Expand Up @@ -192,7 +194,7 @@ func ToMap(ctx context.Context) *sync.Map {

// ToRawMap returns All Cache Key-Value map
func (g *gache) ToRawMap(ctx context.Context) map[string]interface{} {
m := make(map[string]interface{})
m := make(map[string]interface{}, g.Len())
mu := new(sync.Mutex)
g.Foreach(ctx, func(key string, val interface{}, exp int64) bool {
mu.Lock()
Expand Down Expand Up @@ -251,6 +253,7 @@ func (g *gache) set(key string, val interface{}, expire int64) {
if expire > 0 {
expire = fastime.UnixNanoNow() + expire
}
atomic.AddUint64(&g.l, 1)
g.shards[xxhash.Sum64(*(*[]byte)(unsafe.Pointer(&key)))&0xFF].Store(key, value{
expire: expire,
val: val,
Expand Down Expand Up @@ -279,6 +282,7 @@ func Set(key string, val interface{}) {

// Delete deletes value from Gache using key
func (g *gache) Delete(key string) {
atomic.StoreUint64(&g.l, atomic.LoadUint64(&g.l)-1)
g.shards[xxhash.Sum64(*(*[]byte)(unsafe.Pointer(&key)))&0xFF].Delete(key)
}

Expand Down Expand Up @@ -358,9 +362,20 @@ func Foreach(ctx context.Context, f func(string, interface{}, int64) bool) Gache
return instance.Foreach(ctx, f)
}

// Len returns stored object length
func Len() int {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function Len should have comment or be unexported

return instance.Len()
}

// Len returns stored object length
func (g *gache) Len() int {
l := atomic.LoadUint64(&g.l)
return *(*int)(unsafe.Pointer(&l))
}

// Write writes all cached data to writer
func (g *gache) Write(ctx context.Context, w io.Writer) error {
m := make(map[string]value)
m := make(map[string]value, g.Len())
mu := new(sync.Mutex)
gb := gob.NewEncoder(lz4.NewWriter(w))
g.Foreach(ctx, func(key string, val interface{}, exp int64) bool {
Expand Down
Loading