From: qydysky Date: Thu, 1 Apr 2021 13:31:32 +0000 (+0800) Subject: 109 X-Git-Tag: v0.5.1 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=00864b0afae080c3be4725bae995f222b04111d5;p=part%2F.git 109 --- diff --git a/tmplKV/tmplKV.go b/tmplKV/tmplKV.go index 8e130d3..df65806 100644 --- a/tmplKV/tmplKV.go +++ b/tmplKV/tmplKV.go @@ -30,11 +30,12 @@ func New_tmplKV() (*tmplKV) { return s } -// 设置Key Value Exp(有效秒数) +// 设置Key Value Exp(有效秒数,<0永久) func (s *tmplKV) Set(key,value interface{},exp int64) { + if exp >= 0 {exp = s.now+exp} s.kvt_map.Store(key, tmplKV_item{ kv: value, - kt: s.now+exp, + kt: exp, }) } @@ -45,7 +46,7 @@ func (s *tmplKV) Get(key interface{}) (isLive bool,value interface{}){ item,_ := tmp.(tmplKV_item) value = item.kv - isLive = ok && s.now <= item.kt + isLive = ok && item.kt < 0 || s.now <= item.kt if !isLive && ok { s.kvt_map.Delete(key) } diff --git a/tmplKV/tmplKV_test.go b/tmplKV/tmplKV_test.go index aa7b244..472ec1a 100644 --- a/tmplKV/tmplKV_test.go +++ b/tmplKV/tmplKV_test.go @@ -9,8 +9,8 @@ func Test_tmplKV(t *testing.T) { s := New_tmplKV() s.Set("a",`a`,1) if !s.Check("a",`a`) {t.Error(`no match1`)} - s.Set("a",`b`,1) + s.Set("a",`b`,-1) if !s.Check("a",`b`) {t.Error(`no match2`)} time.Sleep(time.Second*time.Duration(1)) - if s.Check("a",`b`) {t.Error(`no TO1`)} + if !s.Check("a",`b`) {t.Error(`no TO1`)} }