Commit 8b222bcf authored by jingbo.wang's avatar jingbo.wang

变更缓存文件的序列化协议,把json改成gob。原因是json的int、float傻傻分不清

parent 39ae40ec
...@@ -85,7 +85,7 @@ func main() { ...@@ -85,7 +85,7 @@ func main() {
} }
}() }()
//初始化数据库(创建DB) //初始化数据库(创建DB,创建连续查询
dao.DbInit() dao.DbInit()
//处理消息 //处理消息
......
package dao package dao
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/gob"
"fmt"
"git.quantgroup.cn/DevOps/enoch/pkg/global" "git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog" "git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/influxdata/influxdb/client/v2" "github.com/influxdata/influxdb/client/v2"
...@@ -93,11 +95,17 @@ func (d *Dao) flashFileCache() { ...@@ -93,11 +95,17 @@ func (d *Dao) flashFileCache() {
} }
cachePointList := make([]CachePoint, 0) cachePointList := make([]CachePoint, 0)
if err := json.Unmarshal(data, &cachePointList); err != nil { buf := bytes.NewBuffer(data)
glog.Error("can not unmarshal file:", filePath) dec := gob.NewDecoder(buf)
if err := dec.Decode(&cachePointList); err != nil {
glog.Error("can not decode file:", filePath, err)
continue continue
} }
for _, v := range cachePointList {
fmt.Println(v)
}
pointList := make([]*client.Point, 0) pointList := make([]*client.Point, 0)
for _, cachePoint := range cachePointList { for _, cachePoint := range cachePointList {
point, err := client.NewPoint(cachePoint.Name, cachePoint.Tags, cachePoint.Fields, cachePoint.Time) point, err := client.NewPoint(cachePoint.Name, cachePoint.Tags, cachePoint.Fields, cachePoint.Time)
...@@ -108,10 +116,10 @@ func (d *Dao) flashFileCache() { ...@@ -108,10 +116,10 @@ func (d *Dao) flashFileCache() {
} }
if err := d.writeDb(pointList); err != nil { if err := d.writeDb(pointList); err != nil {
glog.Warn("flash file cache: can not write db") glog.Warn("flash file cache: can not write db", err)
continue continue
} else { } else {
glog.Info("文件缓存写入db成功:", filePath) glog.Info("文件缓存写入influxdb成功:", filePath)
} }
if err := os.Remove(filePath); err != nil { if err := os.Remove(filePath); err != nil {
...@@ -223,10 +231,12 @@ func (d *Dao) writeFile(pointList []*client.Point) error { ...@@ -223,10 +231,12 @@ func (d *Dao) writeFile(pointList []*client.Point) error {
cachePointList = append(cachePointList, NewPoint(point)) cachePointList = append(cachePointList, NewPoint(point))
} }
data, err := json.Marshal(cachePointList) buf := new(bytes.Buffer)
if err != nil { enc := gob.NewEncoder(buf)
if err := enc.Encode(cachePointList); err != nil {
return err return err
} }
data := buf.Bytes()
fileName := time.Now().Format("2006-01-02T15:04:05.999999999") fileName := time.Now().Format("2006-01-02T15:04:05.999999999")
writingFileName := path.Join(d.cacheFileDir, filePrefixWriting+":"+fileName) writingFileName := path.Join(d.cacheFileDir, filePrefixWriting+":"+fileName)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment