From: qydysky Date: Sun, 7 Feb 2021 11:27:22 +0000 (+0800) Subject: 104 X-Git-Tag: v0.3.12 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=88d91b88f0dc2b86fe1334d7bade0f38832efeda;p=part%2F.git 104 --- diff --git a/tmplKV/tmplK.go b/tmplKV/tmplK.go index d476c8e..c910ab3 100644 --- a/tmplKV/tmplK.go +++ b/tmplKV/tmplK.go @@ -12,7 +12,7 @@ type tmplK struct { now int64 pool *idpool.Idpool kvt_map map[string]tmplK_item - sync.Mutex + sync.RWMutex } type tmplK_item struct { @@ -35,8 +35,8 @@ func New_tmplK(SumInDruation,Druation int64) (*tmplK) { s.now = (<- ticker.C).Unix() go func(){ tmp := make(map[string]tmplK_item) - for k,v := range s.kvt_map {tmp[k] = v} s.Lock() + for k,v := range s.kvt_map {tmp[k] = v} s.kvt_map = tmp s.Unlock() }() @@ -47,38 +47,39 @@ func New_tmplK(SumInDruation,Druation int64) (*tmplK) { } func (s *tmplK) Set(key string) (id uintptr) { - + s.Lock() + defer s.Unlock() + if tmp, oks := s.kvt_map[key];oks { defer s.pool.Put(tmp.uid)//在取得新Id后才put回 } else if s.SumInDruation >= 0 && s.pool.Len() >= uint(s.SumInDruation){//不为无限&&达到限额 随机替代 for oldkey,item := range s.kvt_map { - s.Lock() s.kvt_map[key] = tmplK_item{ kv: item.kv, kt: s.now, uid: item.uid, } delete(s.kvt_map,oldkey) - s.Unlock() return item.kv } } Uid := s.pool.Get() - s.Lock() s.kvt_map[key] = tmplK_item{ kv: Uid.Id, kt: s.now, uid: Uid, } - s.Unlock() return Uid.Id } func (s *tmplK) Get(key string) (isLive bool,id uintptr){ + s.RLock() tmp, ok := s.kvt_map[key] + s.RUnlock() + id = tmp.kv isLive = ok && s.Druation < 0 || s.now - tmp.kt <= s.Druation if !isLive && ok { diff --git a/tmplKV/tmplK_test.go b/tmplKV/tmplK_test.go index 3c65465..80b7ab2 100644 --- a/tmplKV/tmplK_test.go +++ b/tmplKV/tmplK_test.go @@ -1,6 +1,7 @@ package part import ( + "fmt" "time" "testing" ) @@ -21,3 +22,24 @@ func Test_tmplK(t *testing.T) { if o,p := s.Buf();p != 0 || o - time.Now().Unix() > 1{t.Error(`sum/time no match2`)} } + +func Test_tmplK2(t *testing.T) { + s := New_tmplK(1e6, 5) + getchan := make(chan uintptr,100) + setchan := make(chan uintptr,100) + go func(){ + for i := 0; i < 1e6; i++ { + getchan<-s.Set("a") + } + }() + go func(){ + for i := 0; i < 1e6; i++ { + s.Check("b",<-setchan) + } + }() + for i := 0; i < 1e6; i++ { + s.Check("a",<-getchan) + setchan<-s.Set("b") + fmt.Print("\r",i) + } +} \ No newline at end of file diff --git a/tmplKV/tmplV.go b/tmplKV/tmplV.go index f89cb2f..102f964 100644 --- a/tmplKV/tmplV.go +++ b/tmplKV/tmplV.go @@ -12,7 +12,7 @@ type tmplV struct { now int64 pool *idpool.Idpool kvt_map map[uintptr]tmplV_item - sync.Mutex + sync.RWMutex } type tmplV_item struct { @@ -35,8 +35,8 @@ func New_tmplV(SumInDruation,Druation int64) (*tmplV) { s.now = (<- ticker.C).Unix() go func(){ tmp := make(map[uintptr]tmplV_item) - for k,v := range s.kvt_map {tmp[k] = v} s.Lock() + for k,v := range s.kvt_map {tmp[k] = v} s.kvt_map = tmp s.Unlock() }() @@ -49,8 +49,8 @@ func New_tmplV(SumInDruation,Druation int64) (*tmplV) { func (s *tmplV) Set(contect string) (key uintptr) { if s.SumInDruation >= 0 && s.pool.Len() >= uint(s.SumInDruation) {//不为无限&&达到限额 随机替代 + s.Lock() for key,item := range s.kvt_map { - s.Lock() s.kvt_map[key] = tmplV_item{ kv: contect, kt: s.now, @@ -75,7 +75,9 @@ func (s *tmplV) Set(contect string) (key uintptr) { } func (s *tmplV) Get(key uintptr) (isLive bool,contect string){ + s.RLock() K, ok := s.kvt_map[key] + s.RUnlock() contect = K.kv isLive = ok && s.Druation < 0 || s.now - K.kt <= s.Druation if !isLive && ok {