Skip to content

Commit bd87e9d

Browse files
author
Yusuke Kato
authored
[patch] decrease allocation (#26)
1 parent 019d49f commit bd87e9d

File tree

3 files changed

+254
-171
lines changed

3 files changed

+254
-171
lines changed

example/gache-sample.gdb

Lines changed: 0 additions & 1 deletion
This file was deleted.

gache.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type (
2525
EnableExpiredHook() Gache
2626
Foreach(context.Context, func(string, interface{}, int64) bool) Gache
2727
Get(string) (interface{}, bool)
28+
GetWithExpire(string) (interface{}, int64, bool)
2829
Read(io.Reader) error
2930
Set(string, interface{})
3031
SetDefaultExpire(time.Duration) Gache
@@ -91,14 +92,6 @@ func (v *value) isValid() bool {
9192
return v.expire <= 0 || fastime.UnixNanoNow() <= v.expire
9293
}
9394

94-
func (v *value) Val() interface{} {
95-
return v.val
96-
}
97-
98-
func (v *value) Expire() time.Duration {
99-
return *(*time.Duration)(unsafe.Pointer(&v.expire))
100-
}
101-
10295
// SetDefaultExpire set expire duration
10396
func (g *gache) SetDefaultExpire(ex time.Duration) Gache {
10497
atomic.StoreInt64(&g.expire, *(*int64)(unsafe.Pointer(&ex)))
@@ -197,28 +190,40 @@ func ToRawMap(ctx context.Context) map[string]interface{} {
197190
}
198191

199192
// get returns value & exists from key
200-
func (g *gache) get(key string) (interface{}, bool) {
193+
func (g *gache) get(key string) (interface{}, int64, bool) {
201194
v, ok := g.shards[xxhash.Sum64(*(*[]byte)(unsafe.Pointer(&key)))&0xFF].Load(key)
202195

203196
if !ok {
204-
return nil, false
197+
return nil, 0, false
205198
}
206199

207200
if d := v.(value); d.isValid() {
208-
return d.val, true
201+
return d.val, d.expire, true
209202
}
210203

211204
g.expiration(key)
212-
return nil, false
205+
return nil, 0, false
213206
}
214207

215208
// Get returns value & exists from key
216209
func (g *gache) Get(key string) (interface{}, bool) {
217-
return g.get(key)
210+
v, _, ok := g.get(key)
211+
return v, ok
218212
}
219213

220214
// Get returns value & exists from key
221215
func Get(key string) (interface{}, bool) {
216+
v, _, ok := instance.get(key)
217+
return v, ok
218+
}
219+
220+
// GetWithExpire returns value & expire & exists from key
221+
func (g *gache) GetWithExpire(key string) (interface{}, int64, bool) {
222+
return g.get(key)
223+
}
224+
225+
// GetWithExpire returns value & expire & exists from key
226+
func GetWithExpire(key string) (interface{}, int64, bool) {
222227
return instance.get(key)
223228
}
224229

@@ -344,7 +349,7 @@ func (g *gache) Write(ctx context.Context, w io.Writer) error {
344349
mu.Lock()
345350
m[key] = v
346351
mu.Unlock()
347-
gob.Register(v.Val())
352+
gob.Register(val)
348353
return true
349354
})
350355
return gb.Encode(m)
@@ -364,7 +369,7 @@ func (g *gache) Read(r io.Reader) error {
364369
}
365370
for k, v := range m {
366371
if v.isValid() {
367-
g.Set(k, v.Val())
372+
g.Set(k, v.val)
368373
}
369374
}
370375
return nil

0 commit comments

Comments
 (0)