From: qydysky Date: Fri, 2 Apr 2021 07:24:07 +0000 (+0800) Subject: 109 X-Git-Tag: v0.5.2~1 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=2e9feb3f8ff6b283d11cfec25cf8677d9189d757;p=part%2F.git 109 --- diff --git a/tmplKV/tmplKV.go b/tmplKV/tmplKV.go index df65806..26008d5 100644 --- a/tmplKV/tmplKV.go +++ b/tmplKV/tmplKV.go @@ -40,7 +40,7 @@ func (s *tmplKV) Set(key,value interface{},exp int64) { } //获取Value 及是否有效 -func (s *tmplKV) Get(key interface{}) (isLive bool,value interface{}){ +func (s *tmplKV) Get(key interface{}) (value interface{},isLive bool){ tmp, ok := s.kvt_map.Load(key) item,_ := tmp.(tmplKV_item) @@ -53,6 +53,21 @@ func (s *tmplKV) Get(key interface{}) (isLive bool,value interface{}){ return } +//获取Value 及是否有效 +func (s *tmplKV) GetV(key interface{}) (value interface{}){ + tmp, ok := s.kvt_map.Load(key) + + item,_ := tmp.(tmplKV_item) + value = item.kv + + isLive := ok && item.kt < 0 || s.now <= item.kt + if !isLive && ok { + value = nil + s.kvt_map.Delete(key) + } + return +} + //检查Key Value是否对应及有效 func (s *tmplKV) Check(key,value interface{}) bool { ok,v := s.Get(key) diff --git a/tmplKV/tmplKV_test.go b/tmplKV/tmplKV_test.go index 472ec1a..ce09e48 100644 --- a/tmplKV/tmplKV_test.go +++ b/tmplKV/tmplKV_test.go @@ -12,5 +12,10 @@ func Test_tmplKV(t *testing.T) { 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 v,ok := s.Get("a");!ok { + t.Error(`no TO1`) + }else if vv,ok := v.(string);!ok{ + t.Error(`no string`) + } + if v,ok := s.GetV("a").(string);!ok || v != `a` {t.Error(`no 2`)} }