]> 127.0.0.1 Git - part/.git/commitdiff
109 v0.5.1
authorqydysky <qydysky@foxmail.com>
Thu, 1 Apr 2021 13:31:32 +0000 (21:31 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 1 Apr 2021 13:31:32 +0000 (21:31 +0800)
tmplKV/tmplKV.go
tmplKV/tmplKV_test.go

index 8e130d3870cde47890bd63aaf3f21d6629fefa62..df65806d87c6857ec50ef5e18bbed643bd635d75 100644 (file)
@@ -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)
        }
index aa7b244d562e53193db4e1b88ab820c72d36d208..472ec1a68658190eb95a57dfea3359551dd4cf64 100644 (file)
@@ -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`)}
 }