From 3abe3aa28b9b66ec701bb0358e2df44d1490040f Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 11 Jul 2019 12:51:55 +0800 Subject: [PATCH 1/3] store.Read() returns honor `Record.Expiry` --- data/store/memory/memory.go | 9 ++++++++ data/store/memory/memory_test.go | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 data/store/memory/memory_test.go diff --git a/data/store/memory/memory.go b/data/store/memory/memory.go index 0e06eefa..16f68136 100644 --- a/data/store/memory/memory.go +++ b/data/store/memory/memory.go @@ -36,6 +36,11 @@ func (m *memoryStore) Dump() ([]*store.Record, error) { if d > time.Duration(0) && t > d { continue } + + // update expiry + v.r.Expiry -= t + v.c = time.Now() + values = append(values, v.r) } @@ -60,6 +65,10 @@ func (m *memoryStore) Read(key string) (*store.Record, error) { return nil, store.ErrNotFound } + // update expiry + v.r.Expiry -= t + v.c = time.Now() + return v.r, nil } diff --git a/data/store/memory/memory_test.go b/data/store/memory/memory_test.go new file mode 100644 index 00000000..44483869 --- /dev/null +++ b/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") + } +} From c8d57032bcdaf201ac09480bba2be5dc04bfd3c7 Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 11 Jul 2019 12:58:20 +0800 Subject: [PATCH 2/3] update expiry only if it is non-zero --- data/store/memory/memory.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/data/store/memory/memory.go b/data/store/memory/memory.go index 16f68136..75aafce0 100644 --- a/data/store/memory/memory.go +++ b/data/store/memory/memory.go @@ -38,8 +38,10 @@ func (m *memoryStore) Dump() ([]*store.Record, error) { } // update expiry - v.r.Expiry -= t - v.c = time.Now() + if d > time.Duration(0) { + v.r.Expiry -= t + v.c = time.Now() + } values = append(values, v.r) } @@ -66,8 +68,10 @@ func (m *memoryStore) Read(key string) (*store.Record, error) { } // update expiry - v.r.Expiry -= t - v.c = time.Now() + if d > time.Duration(0) { + v.r.Expiry -= t + v.c = time.Now() + } return v.r, nil } From bdeae91063d1a6c2639a01829fd81d548cbcef4f Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 11 Jul 2019 14:13:58 +0800 Subject: [PATCH 3/3] condense code --- data/store/memory/memory.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/data/store/memory/memory.go b/data/store/memory/memory.go index 75aafce0..bb7892e8 100644 --- a/data/store/memory/memory.go +++ b/data/store/memory/memory.go @@ -32,13 +32,12 @@ 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 - } - - // update expiry if d > time.Duration(0) { + // expired + if t > d { + continue + } + // update expiry v.r.Expiry -= t v.c = time.Now() } @@ -63,12 +62,11 @@ 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 - } - - // update expiry if d > time.Duration(0) { + if t > d { + return nil, store.ErrNotFound + } + // update expiry v.r.Expiry -= t v.c = time.Now() }