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] delete expired value on load #25

Merged
merged 1 commit into from
Feb 21, 2019
Merged
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
30 changes: 27 additions & 3 deletions gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ func (v *value) isValid() bool {
return v.expire <= 0 || fastime.UnixNanoNow() <= v.expire
}

func (v *value) Val() interface{} {
return *v.val
}

func (v *value) Expire() time.Duration {
return *(*time.Duration)(unsafe.Pointer(&v.expire))
}

// SetDefaultExpire set expire duration
func (g *gache) SetDefaultExpire(ex time.Duration) Gache {
atomic.StoreInt64(&g.expire, *(*int64)(unsafe.Pointer(&ex)))
Expand Down Expand Up @@ -325,7 +333,21 @@ func Foreach(ctx context.Context, f func(string, interface{}, int64) bool) Gache

// Write writes all cached data to writer
func (g *gache) Write(ctx context.Context, w io.Writer) error {
return gob.NewEncoder(lz4.NewWriter(w)).Encode(g.ToRawMap(ctx))
m := make(map[string]*value)
mu := new(sync.Mutex)
gb := gob.NewEncoder(lz4.NewWriter(w))
g.Foreach(ctx, func(key string, val interface{}, exp int64) bool {
v := &value{
val: &val,
expire: exp,
}
mu.Lock()
m[key] = v
mu.Unlock()
gob.Register(v.Val())
return true
})
return gb.Encode(m)
}

// Write writes all cached data to writer
Expand All @@ -335,13 +357,15 @@ func Write(ctx context.Context, w io.Writer) error {

// Read reads reader data to cache
func (g *gache) Read(r io.Reader) error {
m := make(map[string]interface{})
m := make(map[string]*value)
err := gob.NewDecoder(lz4.NewReader(r)).Decode(&m)
if err != nil {
return err
}
for k, v := range m {
g.Set(k, v)
if v.isValid() {
g.Set(k, v.Val())
}
}
return nil
}
Expand Down