memcache_seg.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/dm2/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _keySegMC = "sg_%d_%d_%d_%d"
  11. )
  12. func keySegMC(tp int32, oid, total, num int64) string {
  13. return fmt.Sprintf(_keySegMC, tp, oid, total, num)
  14. }
  15. func keyXMLSeg(tp int32, oid, cnt, num int64) string {
  16. return fmt.Sprintf("%d_%d_%d_%d", tp, oid, cnt, num)
  17. }
  18. // DelXMLSegCache delete segment xml content.
  19. func (d *Dao) DelXMLSegCache(c context.Context, tp int32, oid, cnt, num int64) (err error) {
  20. conn := d.mc.Get(c)
  21. key := keyXMLSeg(tp, oid, cnt, num)
  22. if err = conn.Delete(key); err != nil {
  23. if err == memcache.ErrNotFound {
  24. err = nil
  25. } else {
  26. log.Error("conn.Delete(%s) error(%v)", key, err)
  27. }
  28. }
  29. conn.Close()
  30. return
  31. }
  32. // SetXMLSegCache set dm xml content into memcache.
  33. func (d *Dao) SetXMLSegCache(c context.Context, tp int32, oid, cnt, num int64, value []byte) (err error) {
  34. key := keyXMLSeg(tp, oid, cnt, num)
  35. conn := d.mc.Get(c)
  36. item := memcache.Item{
  37. Key: key,
  38. Value: value,
  39. Expiration: d.mcExpire,
  40. Flags: memcache.FlagRAW,
  41. }
  42. if err = conn.Set(&item); err != nil {
  43. log.Error("mc.Set(%v) error(%v)", item, err)
  44. }
  45. conn.Close()
  46. return
  47. }
  48. // SetDMSegCache set segment dm to cache.
  49. func (d *Dao) SetDMSegCache(c context.Context, tp int32, oid, total, num int64, dmSeg *model.DMSeg) (err error) {
  50. key := keySegMC(tp, oid, total, num)
  51. conn := d.dmSegMC.Get(c)
  52. item := memcache.Item{
  53. Key: key,
  54. Object: dmSeg,
  55. Expiration: d.mcExpire,
  56. Flags: memcache.FlagProtobuf | memcache.FlagGzip,
  57. }
  58. if err = conn.Set(&item); err != nil {
  59. log.Error("conn.Set(%v) error(%v)", item, err)
  60. }
  61. conn.Close()
  62. return
  63. }
  64. // DMSegCache dm segment pb cache.
  65. func (d *Dao) DMSegCache(c context.Context, tp int32, oid, total, num int64) (dmSeg *model.DMSeg, err error) {
  66. var (
  67. key = keySegMC(tp, oid, total, num)
  68. conn = d.dmSegMC.Get(c)
  69. item *memcache.Item
  70. )
  71. dmSeg = new(model.DMSeg)
  72. defer conn.Close()
  73. if item, err = conn.Get(key); err != nil {
  74. if err == memcache.ErrNotFound {
  75. err = nil
  76. dmSeg = nil
  77. } else {
  78. log.Error("mc.Get(%s) error(%v)", key, err)
  79. }
  80. return
  81. }
  82. if err = conn.Scan(item, dmSeg); err != nil {
  83. log.Error("conn.Scan() error(%v)", err)
  84. }
  85. return
  86. }