now int64
pool *idpool.Idpool
kvt_map map[string]tmplK_item
- sync.Mutex
+ sync.RWMutex
}
type tmplK_item struct {
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()
}()
}
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 {
package part
import (
+ "fmt"
"time"
"testing"
)
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
now int64
pool *idpool.Idpool
kvt_map map[uintptr]tmplV_item
- sync.Mutex
+ sync.RWMutex
}
type tmplV_item struct {
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()
}()
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,
}
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 {