sk-matrix-project/backend/golang/matrix-dnslog-service/pkg/cache/data.go
2023-03-24 15:46:54 +08:00

51 lines
768 B
Go

package cache
import "sync"
var cache = sync.Map{}
var lock = sync.RWMutex{}
type Data struct {
Seq map[string]bool
Data string
}
func NewData() *Data {
return &Data{
Seq: make(map[string]bool),
Data: "",
}
}
func Append(id string, seq string, data string) {
lock.Lock()
defer lock.Unlock()
preData, ok := cache.Load(id)
if !ok {
datum := NewData()
datum.Seq[seq] = true
datum.Data = data
cache.Store(id, datum)
} else {
datum := preData.(*Data)
if _, ok := datum.Seq[seq]; ok {
return
}
datum.Seq[seq] = true
datum.Data = datum.Data + data
cache.Store(id, datum)
}
}
func Get(id string) string {
lock.Lock()
defer lock.Unlock()
data, ok := cache.LoadAndDelete(id)
if !ok {
return ""
}
return data.(*Data).Data
}