store.Read() returns honor Record.Expiry

This commit is contained in:
magodo 2019-07-11 12:51:55 +08:00
parent 4cad7697cc
commit 3abe3aa28b
2 changed files with 46 additions and 0 deletions

View File

@ -36,6 +36,11 @@ func (m *memoryStore) Dump() ([]*store.Record, error) {
if d > time.Duration(0) && t > d { if d > time.Duration(0) && t > d {
continue continue
} }
// update expiry
v.r.Expiry -= t
v.c = time.Now()
values = append(values, v.r) values = append(values, v.r)
} }
@ -60,6 +65,10 @@ func (m *memoryStore) Read(key string) (*store.Record, error) {
return nil, store.ErrNotFound return nil, store.ErrNotFound
} }
// update expiry
v.r.Expiry -= t
v.c = time.Now()
return v.r, nil return v.r, nil
} }

View File

@ -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")
}
}