save_interface.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package tick
  2. import "unsafe"
  3. import "sync"
  4. import "bytes"
  5. import "encoding/binary"
  6. import "tickserver/framework/store"
  7. import "log"
  8. type MarketSave struct {
  9. Market2
  10. }
  11. //日志时间精确到秒
  12. func (ms *MarketSave) GetTime() int32 {
  13. return int32(ms.Timestamp / 1000)
  14. }
  15. func (ms *MarketSave) GetId() int64 {
  16. return ms.InsId
  17. }
  18. //全局变量,可以提高性能
  19. var sizeofmarketsave = int(unsafe.Sizeof(MarketSave{}))
  20. //减少垃圾回收器的使用次数
  21. var fhBytes = sync.Pool{
  22. New: func() interface{} {
  23. buf := make([]byte, sizeofmarketsave)
  24. return &buf
  25. },
  26. }
  27. func (ms *MarketSave) GetData() []byte {
  28. bufp := fhBytes.Get().(*[]byte)
  29. defer fhBytes.Put(bufp)
  30. buf := bytes.NewBuffer(*bufp)
  31. buf.Reset()
  32. err := binary.Write(buf, binary.LittleEndian, ms)
  33. if err != nil {
  34. log.Println(err)
  35. return nil
  36. }
  37. return buf.Bytes()
  38. }
  39. func (ms *MarketSave) Size() int {
  40. return sizeofmarketsave
  41. }
  42. func (ms *MarketSave) LoadData(data []byte) store.LoggingSave {
  43. reader := bytes.NewBuffer(data)
  44. var ret MarketSave
  45. err := binary.Read(reader, binary.LittleEndian, &ret)
  46. if err != nil {
  47. return nil
  48. }
  49. return &ret
  50. }