mc.cache.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
  2. /*
  3. Package manager is a generated mc cache package.
  4. It is generated from:
  5. type _mc interface {
  6. // mc: -key=upSpecialCacheKey -expire=d.upSpecialExpire -encode=pb
  7. AddCacheUpSpecial(c context.Context, mid int64, us *upgrpc.UpSpecial) (err error)
  8. // mc: -key=upSpecialCacheKey
  9. CacheUpSpecial(c context.Context, mid int64) (res *upgrpc.UpSpecial, err error)
  10. // mc: -key=upSpecialCacheKey
  11. DelCacheUpSpecial(c context.Context, mid int64) (err error)
  12. // mc: -key=upSpecialCacheKey -expire=d.upSpecialExpire -encode=pb
  13. AddCacheUpsSpecial(c context.Context, mu map[int64]*upgrpc.UpSpecial) (err error)
  14. // mc: -key=upSpecialCacheKey
  15. CacheUpsSpecial(c context.Context, mid []int64) (res map[int64]*upgrpc.UpSpecial, err error)
  16. // mc: -key=upSpecialCacheKey
  17. DelCacheUpsSpecial(c context.Context, mids []int64) (err error)
  18. }
  19. */
  20. package manager
  21. import (
  22. "context"
  23. "fmt"
  24. upgrpc "go-common/app/service/main/up/api/v1"
  25. "go-common/library/cache/memcache"
  26. "go-common/library/log"
  27. "go-common/library/stat/prom"
  28. )
  29. var _ _mc
  30. // AddCacheUpSpecial Set data to mc
  31. func (d *Dao) AddCacheUpSpecial(c context.Context, id int64, val *upgrpc.UpSpecial) (err error) {
  32. if val == nil {
  33. return
  34. }
  35. conn := d.mc.Get(c)
  36. defer conn.Close()
  37. key := upSpecialCacheKey(id)
  38. item := &memcache.Item{Key: key, Object: val, Expiration: d.upSpecialExpire, Flags: memcache.FlagProtobuf}
  39. if err = conn.Set(item); err != nil {
  40. prom.BusinessErrCount.Incr("mc:AddCacheUpSpecial")
  41. log.Errorv(c, log.KV("AddCacheUpSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  42. return
  43. }
  44. return
  45. }
  46. // CacheUpSpecial get data from mc
  47. func (d *Dao) CacheUpSpecial(c context.Context, id int64) (res *upgrpc.UpSpecial, err error) {
  48. conn := d.mc.Get(c)
  49. defer conn.Close()
  50. key := upSpecialCacheKey(id)
  51. reply, err := conn.Get(key)
  52. if err != nil {
  53. if err == memcache.ErrNotFound {
  54. err = nil
  55. return
  56. }
  57. prom.BusinessErrCount.Incr("mc:CacheUpSpecial")
  58. log.Errorv(c, log.KV("CacheUpSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  59. return
  60. }
  61. res = &upgrpc.UpSpecial{}
  62. err = conn.Scan(reply, res)
  63. if err != nil {
  64. prom.BusinessErrCount.Incr("mc:CacheUpSpecial")
  65. log.Errorv(c, log.KV("CacheUpSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  66. return
  67. }
  68. return
  69. }
  70. // DelCacheUpSpecial delete data from mc
  71. func (d *Dao) DelCacheUpSpecial(c context.Context, id int64) (err error) {
  72. conn := d.mc.Get(c)
  73. defer conn.Close()
  74. key := upSpecialCacheKey(id)
  75. if err = conn.Delete(key); err != nil {
  76. if err == memcache.ErrNotFound {
  77. err = nil
  78. return
  79. }
  80. prom.BusinessErrCount.Incr("mc:DelCacheUpSpecial")
  81. log.Errorv(c, log.KV("DelCacheUpSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  82. return
  83. }
  84. return
  85. }
  86. // AddCacheUpsSpecial Set data to mc
  87. func (d *Dao) AddCacheUpsSpecial(c context.Context, values map[int64]*upgrpc.UpSpecial) (err error) {
  88. if len(values) == 0 {
  89. return
  90. }
  91. conn := d.mc.Get(c)
  92. defer conn.Close()
  93. for id, val := range values {
  94. key := upSpecialCacheKey(id)
  95. item := &memcache.Item{Key: key, Object: val, Expiration: d.upSpecialExpire, Flags: memcache.FlagProtobuf}
  96. if err = conn.Set(item); err != nil {
  97. prom.BusinessErrCount.Incr("mc:AddCacheUpsSpecial")
  98. log.Errorv(c, log.KV("AddCacheUpsSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  99. return
  100. }
  101. }
  102. return
  103. }
  104. // CacheUpsSpecial get data from mc
  105. func (d *Dao) CacheUpsSpecial(c context.Context, ids []int64) (res map[int64]*upgrpc.UpSpecial, err error) {
  106. l := len(ids)
  107. if l == 0 {
  108. return
  109. }
  110. keysMap := make(map[string]int64, l)
  111. keys := make([]string, 0, l)
  112. for _, id := range ids {
  113. key := upSpecialCacheKey(id)
  114. keysMap[key] = id
  115. keys = append(keys, key)
  116. }
  117. conn := d.mc.Get(c)
  118. defer conn.Close()
  119. replies, err := conn.GetMulti(keys)
  120. if err != nil {
  121. prom.BusinessErrCount.Incr("mc:CacheUpsSpecial")
  122. log.Errorv(c, log.KV("CacheUpsSpecial", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
  123. return
  124. }
  125. for key, reply := range replies {
  126. var v *upgrpc.UpSpecial
  127. v = &upgrpc.UpSpecial{}
  128. err = conn.Scan(reply, v)
  129. if err != nil {
  130. prom.BusinessErrCount.Incr("mc:CacheUpsSpecial")
  131. log.Errorv(c, log.KV("CacheUpsSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  132. return
  133. }
  134. if res == nil {
  135. res = make(map[int64]*upgrpc.UpSpecial, len(keys))
  136. }
  137. res[keysMap[key]] = v
  138. }
  139. return
  140. }
  141. // DelCacheUpsSpecial delete data from mc
  142. func (d *Dao) DelCacheUpsSpecial(c context.Context, ids []int64) (err error) {
  143. if len(ids) == 0 {
  144. return
  145. }
  146. conn := d.mc.Get(c)
  147. defer conn.Close()
  148. for _, id := range ids {
  149. key := upSpecialCacheKey(id)
  150. if err = conn.Delete(key); err != nil {
  151. if err == memcache.ErrNotFound {
  152. err = nil
  153. continue
  154. }
  155. prom.BusinessErrCount.Incr("mc:DelCacheUpsSpecial")
  156. log.Errorv(c, log.KV("DelCacheUpsSpecial", fmt.Sprintf("%+v", err)), log.KV("key", key))
  157. return
  158. }
  159. }
  160. return
  161. }