Skip to content

Commit c6d0b57

Browse files
author
Yusuke Kato
authored
[patch] feature add Len function that returns object conunts (#35)
Signed-off-by: kpango <[email protected]>
1 parent 3fa7218 commit c6d0b57

File tree

5 files changed

+118
-13
lines changed

5 files changed

+118
-13
lines changed

example/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func main() {
3636
glg.Info(v3)
3737
}
3838

39+
glg.Debugf("Len:\t%d", gache.Len())
3940
// set gache default expire time
4041
gc := gache.New().SetDefaultExpire(time.Second * 10)
4142

gache.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type (
3232
SetExpiredHook(f func(context.Context, string)) Gache
3333
SetWithExpire(string, interface{}, time.Duration)
3434
StartExpired(context.Context, time.Duration) Gache
35+
Len() int
3536
ToMap(context.Context) *sync.Map
3637
ToRawMap(context.Context) map[string]interface{}
3738
Write(context.Context, io.Writer) error
@@ -63,6 +64,7 @@ type (
6364
expFuncEnabled bool
6465
expGroup singleflight.Group
6566
expire int64
67+
l uint64
6668
shards [256]*sync.Map
6769
}
6870

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

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

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

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

365+
// Len returns stored object length
366+
func Len() int {
367+
return instance.Len()
368+
}
369+
370+
// Len returns stored object length
371+
func (g *gache) Len() int {
372+
l := atomic.LoadUint64(&g.l)
373+
return *(*int)(unsafe.Pointer(&l))
374+
}
375+
361376
// Write writes all cached data to writer
362377
func (g *gache) Write(ctx context.Context, w io.Writer) error {
363-
m := make(map[string]value)
378+
m := make(map[string]value, g.Len())
364379
mu := new(sync.Mutex)
365380
gb := gob.NewEncoder(lz4.NewWriter(w))
366381
g.Foreach(ctx, func(key string, val interface{}, exp int64) bool {

0 commit comments

Comments
 (0)