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

memoryStore.Read() returns honor Record.Expiry #579

Merged
merged 3 commits into from Jul 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions data/store/memory/memory.go
Expand Up @@ -32,10 +32,16 @@ func (m *memoryStore) Dump() ([]*store.Record, error) {
d := v.r.Expiry
t := time.Since(v.c)

// expired
if d > time.Duration(0) && t > d {
continue
if d > time.Duration(0) {
// expired
if t > d {
continue
}
// update expiry
v.r.Expiry -= t
v.c = time.Now()
}

values = append(values, v.r)
}

Expand All @@ -56,8 +62,13 @@ func (m *memoryStore) Read(key string) (*store.Record, error) {
t := time.Since(v.c)

// expired
if d > time.Duration(0) && t > d {
return nil, store.ErrNotFound
if d > time.Duration(0) {
if t > d {
return nil, store.ErrNotFound
}
// update expiry
v.r.Expiry -= t
v.c = time.Now()
}

return v.r, nil
Expand Down
37 changes: 37 additions & 0 deletions data/store/memory/memory_test.go
@@ -0,0 +1,37 @@
package memory

import (
"testing"
"time"

"github.com/micro/go-micro/data/store"
)

func TestReadRecordExpire(t *testing.T) {
s := NewStore()

var (
key = "foo"
expire = 100 * time.Millisecond
)
rec := &store.Record{
Key: key,
Value: nil,
Expiry: expire,
}
s.Write(rec)

rrec, err := s.Read(key)
if err != nil {
t.Fatal(err)
}
if rrec.Expiry >= expire {
t.Fatal("expiry of read record is not changed")
}

time.Sleep(expire)

if _, err := s.Read(key); err != store.ErrNotFound {
t.Fatal("expire elapsed, but key still accessable")
}
}