package tick import "unsafe" import "sync" import "bytes" import "encoding/binary" import "tickserver/framework/store" import "log" type MarketSave struct { Market2 } //日志时间精确到秒 func (ms *MarketSave) GetTime() int32 { return int32(ms.Timestamp / 1000) } func (ms *MarketSave) GetId() int64 { return ms.InsId } //全局变量,可以提高性能 var sizeofmarketsave = int(unsafe.Sizeof(MarketSave{})) //减少垃圾回收器的使用次数 var fhBytes = sync.Pool{ New: func() interface{} { buf := make([]byte, sizeofmarketsave) return &buf }, } func (ms *MarketSave) GetData() []byte { bufp := fhBytes.Get().(*[]byte) defer fhBytes.Put(bufp) buf := bytes.NewBuffer(*bufp) buf.Reset() err := binary.Write(buf, binary.LittleEndian, ms) if err != nil { log.Println(err) return nil } return buf.Bytes() } func (ms *MarketSave) Size() int { return sizeofmarketsave } func (ms *MarketSave) LoadData(data []byte) store.LoggingSave { reader := bytes.NewBuffer(data) var ret MarketSave err := binary.Read(reader, binary.LittleEndian, &ret) if err != nil { return nil } return &ret }