mc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/saga/model"
  5. "go-common/library/cache/memcache"
  6. "github.com/pkg/errors"
  7. )
  8. func (d *Dao) pingMC(c context.Context) (err error) {
  9. conn := d.mc.Get(c)
  10. defer conn.Close()
  11. if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
  12. err = errors.Wrap(err, "conn.Store(set,ping,1)")
  13. }
  14. return
  15. }
  16. // SetData set data info to memcache
  17. func (d *Dao) SetData(c context.Context, key string, dataMap map[string]*model.TeamDataResp) (err error) {
  18. var (
  19. conn = d.mc.Get(c)
  20. item *memcache.Item
  21. //dataMap = make(map[string]*model.TeamDataResp)
  22. )
  23. defer conn.Close()
  24. item = &memcache.Item{Key: key, Object: dataMap, Expiration: 0, Flags: memcache.FlagJSON}
  25. if err = conn.Set(item); err != nil {
  26. err = errors.Wrapf(err, "conn.Set(%s,%v)", key, dataMap)
  27. return
  28. }
  29. return
  30. }
  31. // GetData get data info from memcache
  32. func (d *Dao) GetData(c context.Context, key string, dataMap *map[string]*model.TeamDataResp) (err error) {
  33. var (
  34. conn = d.mc.Get(c)
  35. reply *memcache.Item
  36. )
  37. defer conn.Close()
  38. reply, err = conn.Get(key)
  39. if err != nil {
  40. if err == memcache.ErrNotFound {
  41. return
  42. }
  43. err = errors.Wrapf(err, "conn.Get(%s)", key)
  44. return
  45. }
  46. if err = conn.Scan(reply, dataMap); err != nil {
  47. err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
  48. return
  49. }
  50. return
  51. }
  52. // DeleteData delete data info in memcache
  53. func (d *Dao) DeleteData(c context.Context, key string) (err error) {
  54. var (
  55. conn = d.mc.Get(c)
  56. )
  57. defer conn.Close()
  58. err = conn.Delete(key)
  59. if err != nil {
  60. if err == memcache.ErrNotFound {
  61. err = nil
  62. return
  63. }
  64. err = errors.Wrapf(err, "conn.Delete(%s)", key)
  65. }
  66. return
  67. }
  68. // SetPipeline set pipeline info info to memcache
  69. func (d *Dao) SetPipeline(c context.Context, key string, pipeline *model.PipelineDataResp) (err error) {
  70. var (
  71. conn = d.mc.Get(c)
  72. item *memcache.Item
  73. )
  74. defer conn.Close()
  75. item = &memcache.Item{Key: key, Object: pipeline, Expiration: 0, Flags: memcache.FlagJSON}
  76. if err = conn.Set(item); err != nil {
  77. err = errors.Wrapf(err, "conn.Set(%s,%v)", key, pipeline)
  78. return
  79. }
  80. return
  81. }
  82. // GetPipeline get pipeline info from memcache
  83. func (d *Dao) GetPipeline(c context.Context, key string) (pipeline *model.PipelineDataResp, err error) {
  84. var (
  85. conn = d.mc.Get(c)
  86. reply *memcache.Item
  87. )
  88. defer conn.Close()
  89. reply, err = conn.Get(key)
  90. if err != nil {
  91. if err == memcache.ErrNotFound {
  92. return
  93. }
  94. err = errors.Wrapf(err, "conn.Get(%s,%v)", key, pipeline)
  95. return
  96. }
  97. pipeline = new(model.PipelineDataResp)
  98. if err = conn.Scan(reply, pipeline); err != nil {
  99. err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
  100. }
  101. return
  102. }