mc.cache.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
  2. /*
  3. Package testdata is a generated mc cache package.
  4. It is generated from:
  5. type _mc interface {
  6. // mc: -key=articleKey
  7. CacheArticles(c context.Context, keys []int64) (map[int64]*Article, error)
  8. // mc: -key=articleKey
  9. CacheArticle(c context.Context, key int64) (*Article, error)
  10. // mc: -key=keyMid
  11. CacheArticle1(c context.Context, key int64, mid int64) (*Article, error)
  12. // mc: -key=noneKey
  13. CacheNone(c context.Context) (*Article, error)
  14. // mc: -key=articleKey
  15. CacheString(c context.Context, key int64) (string, error)
  16. // mc: -key=articleKey -expire=d.articleExpire -encode=json
  17. AddCacheArticles(c context.Context, values map[int64]*Article) error
  18. // 这里也支持自定义注释 会替换默认的注释
  19. // mc: -key=articleKey -expire=d.articleExpire -encode=json|gzip
  20. AddCacheArticle(c context.Context, key int64, value *Article) error
  21. // mc: -key=keyMid -expire=d.articleExpire -encode=gob
  22. AddCacheArticle1(c context.Context, key int64, value *Article, mid int64) error
  23. // mc: -key=noneKey
  24. AddCacheNone(c context.Context, value *Article) error
  25. // mc: -key=articleKey -expire=d.articleExpire
  26. AddCacheString(c context.Context, key int64, value string) error
  27. // mc: -key=articleKey
  28. DelCacheArticles(c context.Context, keys []int64) error
  29. // mc: -key=articleKey
  30. DelCacheArticle(c context.Context, key int64) error
  31. // mc: -key=keyMid
  32. DelCacheArticle1(c context.Context, key int64, mid int64) error
  33. // mc: -key=noneKey
  34. DelCacheNone(c context.Context) error
  35. }
  36. */
  37. package testdata
  38. import (
  39. "context"
  40. "fmt"
  41. "go-common/library/cache/memcache"
  42. "go-common/library/log"
  43. "go-common/library/stat/prom"
  44. )
  45. var _ _mc
  46. // CacheArticles get data from mc
  47. func (d *Dao) CacheArticles(c context.Context, ids []int64) (res map[int64]*Article, err error) {
  48. l := len(ids)
  49. if l == 0 {
  50. return
  51. }
  52. keysMap := make(map[string]int64, l)
  53. keys := make([]string, 0, l)
  54. for _, id := range ids {
  55. key := articleKey(id)
  56. keysMap[key] = id
  57. keys = append(keys, key)
  58. }
  59. conn := d.mc.Get(c)
  60. defer conn.Close()
  61. replies, err := conn.GetMulti(keys)
  62. if err != nil {
  63. prom.BusinessErrCount.Incr("mc:CacheArticles")
  64. log.Errorv(c, log.KV("CacheArticles", fmt.Sprintf("%+v", err)), log.KV("keys", keys))
  65. return
  66. }
  67. for key, reply := range replies {
  68. var v *Article
  69. v = &Article{}
  70. err = conn.Scan(reply, v)
  71. if err != nil {
  72. prom.BusinessErrCount.Incr("mc:CacheArticles")
  73. log.Errorv(c, log.KV("CacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
  74. return
  75. }
  76. if res == nil {
  77. res = make(map[int64]*Article, len(keys))
  78. }
  79. res[keysMap[key]] = v
  80. }
  81. return
  82. }
  83. // CacheArticle get data from mc
  84. func (d *Dao) CacheArticle(c context.Context, id int64) (res *Article, err error) {
  85. conn := d.mc.Get(c)
  86. defer conn.Close()
  87. key := articleKey(id)
  88. reply, err := conn.Get(key)
  89. if err != nil {
  90. if err == memcache.ErrNotFound {
  91. err = nil
  92. return
  93. }
  94. prom.BusinessErrCount.Incr("mc:CacheArticle")
  95. log.Errorv(c, log.KV("CacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
  96. return
  97. }
  98. res = &Article{}
  99. err = conn.Scan(reply, res)
  100. if err != nil {
  101. prom.BusinessErrCount.Incr("mc:CacheArticle")
  102. log.Errorv(c, log.KV("CacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
  103. return
  104. }
  105. return
  106. }
  107. // CacheArticle1 get data from mc
  108. func (d *Dao) CacheArticle1(c context.Context, id int64, mid int64) (res *Article, err error) {
  109. conn := d.mc.Get(c)
  110. defer conn.Close()
  111. key := keyMid(id, mid)
  112. reply, err := conn.Get(key)
  113. if err != nil {
  114. if err == memcache.ErrNotFound {
  115. err = nil
  116. return
  117. }
  118. prom.BusinessErrCount.Incr("mc:CacheArticle1")
  119. log.Errorv(c, log.KV("CacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
  120. return
  121. }
  122. res = &Article{}
  123. err = conn.Scan(reply, res)
  124. if err != nil {
  125. prom.BusinessErrCount.Incr("mc:CacheArticle1")
  126. log.Errorv(c, log.KV("CacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
  127. return
  128. }
  129. return
  130. }
  131. // CacheNone get data from mc
  132. func (d *Dao) CacheNone(c context.Context) (res *Article, err error) {
  133. conn := d.mc.Get(c)
  134. defer conn.Close()
  135. key := noneKey()
  136. reply, err := conn.Get(key)
  137. if err != nil {
  138. if err == memcache.ErrNotFound {
  139. err = nil
  140. return
  141. }
  142. prom.BusinessErrCount.Incr("mc:CacheNone")
  143. log.Errorv(c, log.KV("CacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
  144. return
  145. }
  146. res = &Article{}
  147. err = conn.Scan(reply, res)
  148. if err != nil {
  149. prom.BusinessErrCount.Incr("mc:CacheNone")
  150. log.Errorv(c, log.KV("CacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
  151. return
  152. }
  153. return
  154. }
  155. // CacheString get data from mc
  156. func (d *Dao) CacheString(c context.Context, id int64) (res string, err error) {
  157. conn := d.mc.Get(c)
  158. defer conn.Close()
  159. key := articleKey(id)
  160. reply, err := conn.Get(key)
  161. if err != nil {
  162. if err == memcache.ErrNotFound {
  163. err = nil
  164. return
  165. }
  166. prom.BusinessErrCount.Incr("mc:CacheString")
  167. log.Errorv(c, log.KV("CacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
  168. return
  169. }
  170. err = conn.Scan(reply, &res)
  171. if err != nil {
  172. prom.BusinessErrCount.Incr("mc:CacheString")
  173. log.Errorv(c, log.KV("CacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
  174. return
  175. }
  176. return
  177. }
  178. // AddCacheArticles Set data to mc
  179. func (d *Dao) AddCacheArticles(c context.Context, values map[int64]*Article) (err error) {
  180. if len(values) == 0 {
  181. return
  182. }
  183. conn := d.mc.Get(c)
  184. defer conn.Close()
  185. for id, val := range values {
  186. key := articleKey(id)
  187. item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON}
  188. if err = conn.Set(item); err != nil {
  189. prom.BusinessErrCount.Incr("mc:AddCacheArticles")
  190. log.Errorv(c, log.KV("AddCacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
  191. return
  192. }
  193. }
  194. return
  195. }
  196. // AddCacheArticle 这里也支持自定义注释 会替换默认的注释
  197. func (d *Dao) AddCacheArticle(c context.Context, id int64, val *Article) (err error) {
  198. if val == nil {
  199. return
  200. }
  201. conn := d.mc.Get(c)
  202. defer conn.Close()
  203. key := articleKey(id)
  204. item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON | memcache.FlagGzip}
  205. if err = conn.Set(item); err != nil {
  206. prom.BusinessErrCount.Incr("mc:AddCacheArticle")
  207. log.Errorv(c, log.KV("AddCacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
  208. return
  209. }
  210. return
  211. }
  212. // AddCacheArticle1 Set data to mc
  213. func (d *Dao) AddCacheArticle1(c context.Context, id int64, val *Article, mid int64) (err error) {
  214. if val == nil {
  215. return
  216. }
  217. conn := d.mc.Get(c)
  218. defer conn.Close()
  219. key := keyMid(id, mid)
  220. item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagGOB}
  221. if err = conn.Set(item); err != nil {
  222. prom.BusinessErrCount.Incr("mc:AddCacheArticle1")
  223. log.Errorv(c, log.KV("AddCacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
  224. return
  225. }
  226. return
  227. }
  228. // AddCacheNone Set data to mc
  229. func (d *Dao) AddCacheNone(c context.Context, val *Article) (err error) {
  230. if val == nil {
  231. return
  232. }
  233. conn := d.mc.Get(c)
  234. defer conn.Close()
  235. key := noneKey()
  236. item := &memcache.Item{Key: key, Object: val, Expiration: d.articleExpire, Flags: memcache.FlagJSON}
  237. if err = conn.Set(item); err != nil {
  238. prom.BusinessErrCount.Incr("mc:AddCacheNone")
  239. log.Errorv(c, log.KV("AddCacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
  240. return
  241. }
  242. return
  243. }
  244. // AddCacheString Set data to mc
  245. func (d *Dao) AddCacheString(c context.Context, id int64, val string) (err error) {
  246. if len(val) == 0 {
  247. return
  248. }
  249. conn := d.mc.Get(c)
  250. defer conn.Close()
  251. key := articleKey(id)
  252. bs := []byte(val)
  253. item := &memcache.Item{Key: key, Value: bs, Expiration: d.articleExpire, Flags: memcache.FlagRAW}
  254. if err = conn.Set(item); err != nil {
  255. prom.BusinessErrCount.Incr("mc:AddCacheString")
  256. log.Errorv(c, log.KV("AddCacheString", fmt.Sprintf("%+v", err)), log.KV("key", key))
  257. return
  258. }
  259. return
  260. }
  261. // DelCacheArticles delete data from mc
  262. func (d *Dao) DelCacheArticles(c context.Context, ids []int64) (err error) {
  263. if len(ids) == 0 {
  264. return
  265. }
  266. conn := d.mc.Get(c)
  267. defer conn.Close()
  268. for _, id := range ids {
  269. key := articleKey(id)
  270. if err = conn.Delete(key); err != nil {
  271. if err == memcache.ErrNotFound {
  272. err = nil
  273. continue
  274. }
  275. prom.BusinessErrCount.Incr("mc:DelCacheArticles")
  276. log.Errorv(c, log.KV("DelCacheArticles", fmt.Sprintf("%+v", err)), log.KV("key", key))
  277. return
  278. }
  279. }
  280. return
  281. }
  282. // DelCacheArticle delete data from mc
  283. func (d *Dao) DelCacheArticle(c context.Context, id int64) (err error) {
  284. conn := d.mc.Get(c)
  285. defer conn.Close()
  286. key := articleKey(id)
  287. if err = conn.Delete(key); err != nil {
  288. if err == memcache.ErrNotFound {
  289. err = nil
  290. return
  291. }
  292. prom.BusinessErrCount.Incr("mc:DelCacheArticle")
  293. log.Errorv(c, log.KV("DelCacheArticle", fmt.Sprintf("%+v", err)), log.KV("key", key))
  294. return
  295. }
  296. return
  297. }
  298. // DelCacheArticle1 delete data from mc
  299. func (d *Dao) DelCacheArticle1(c context.Context, id int64, mid int64) (err error) {
  300. conn := d.mc.Get(c)
  301. defer conn.Close()
  302. key := keyMid(id, mid)
  303. if err = conn.Delete(key); err != nil {
  304. if err == memcache.ErrNotFound {
  305. err = nil
  306. return
  307. }
  308. prom.BusinessErrCount.Incr("mc:DelCacheArticle1")
  309. log.Errorv(c, log.KV("DelCacheArticle1", fmt.Sprintf("%+v", err)), log.KV("key", key))
  310. return
  311. }
  312. return
  313. }
  314. // DelCacheNone delete data from mc
  315. func (d *Dao) DelCacheNone(c context.Context) (err error) {
  316. conn := d.mc.Get(c)
  317. defer conn.Close()
  318. key := noneKey()
  319. if err = conn.Delete(key); err != nil {
  320. if err == memcache.ErrNotFound {
  321. err = nil
  322. return
  323. }
  324. prom.BusinessErrCount.Incr("mc:DelCacheNone")
  325. log.Errorv(c, log.KV("DelCacheNone", fmt.Sprintf("%+v", err)), log.KV("key", key))
  326. return
  327. }
  328. return
  329. }