mirror of
				https://github.com/TeaOSLab/EdgeCommon.git
				synced 2025-11-04 05:00:24 +08:00 
			
		
		
		
	修复DataMap无法在多个节点之间共享的问题
This commit is contained in:
		@@ -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)]
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user