From 995acfe1339340d0676ff143324209cf95e8a085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 19 Mar 2023 10:23:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DDataMap=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=9C=A8=E5=A4=9A=E4=B8=AA=E8=8A=82=E7=82=B9=E4=B9=8B=E9=97=B4?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/serverconfigs/shared/data_map.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/serverconfigs/shared/data_map.go b/pkg/serverconfigs/shared/data_map.go index 2b2dd7a..4ef69b3 100644 --- a/pkg/serverconfigs/shared/data_map.go +++ b/pkg/serverconfigs/shared/data_map.go @@ -6,6 +6,7 @@ import ( "bytes" "crypto/md5" "fmt" + "sync" ) var dataMapPrefix = []byte("GOEDGE_DATA_MAP:") @@ -14,6 +15,7 @@ var dataMapPrefix = []byte("GOEDGE_DATA_MAP:") // 用来减少相同数据占用的空间和内存 type DataMap struct { Map map[string][]byte + locker sync.Mutex } // NewDataMap 构建对象 @@ -23,6 +25,8 @@ func NewDataMap() *DataMap { // Put 放入数据 func (this *DataMap) Put(data []byte) (keyData []byte) { + this.locker.Lock() + defer this.locker.Unlock() var key = string(dataMapPrefix) + fmt.Sprintf("%x", md5.Sum(data)) this.Map[key] = data return []byte(key) @@ -30,6 +34,8 @@ func (this *DataMap) Put(data []byte) (keyData []byte) { // Read 读取数据 func (this *DataMap) Read(key []byte) []byte { + this.locker.Lock() + defer this.locker.Unlock() if bytes.HasPrefix(key, dataMapPrefix) { return this.Map[string(key)] }