]> 127.0.0.1 Git - part/.git/commitdiff
104 v0.3.12
authorqydysky <qydysky@foxmail.com>
Sun, 7 Feb 2021 11:27:22 +0000 (19:27 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 7 Feb 2021 11:27:22 +0000 (19:27 +0800)
tmplKV/tmplK.go
tmplKV/tmplK_test.go
tmplKV/tmplV.go

index d476c8ee20fbf2b856c89adababa0627162a8aae..c910ab383075b5037cb7e5cf9c207f43cf2d195e 100644 (file)
@@ -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 {
index 3c654650d9e639cca63ec4b88b486bc03c6be647..80b7ab20ec821bd9c0d7dc62727d6af58bcbcb2b 100644 (file)
@@ -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
index f89cb2fc2c455bc606ebcd134eacd402f5fb12c5..102f964b2b5f5b94334c911924b4d45a0df4e80d 100644 (file)
@@ -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 {