mc.cache.go 5.7 KB

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