mc.cache.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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=tokenKey -type=get
  7. TokenCache(c context.Context, key string) (*model.Report, error)
  8. //mc: -key=tokenKey -expire=d.mcReportExpire
  9. AddTokenCache(c context.Context, key string, value *model.Report) error
  10. //mc: -key=tokenKey -expire=d.mcReportExpire
  11. AddTokensCache(c context.Context, values map[string]*model.Report) error
  12. //mc: -key=tokenKey
  13. DelTokenCache(c context.Context, key string) error
  14. }
  15. */
  16. package dao
  17. import (
  18. "context"
  19. "fmt"
  20. "go-common/app/service/main/push/model"
  21. "go-common/library/cache/memcache"
  22. "go-common/library/log"
  23. "go-common/library/stat/prom"
  24. )
  25. var _ _mc
  26. // TokenCache get data from mc
  27. func (d *Dao) TokenCache(c context.Context, id string) (res *model.Report, err error) {
  28. conn := d.mc.Get(c)
  29. defer conn.Close()
  30. key := tokenKey(id)
  31. reply, err := conn.Get(key)
  32. if err != nil {
  33. if err == memcache.ErrNotFound {
  34. err = nil
  35. return
  36. }
  37. prom.BusinessErrCount.Incr("mc:TokenCache")
  38. log.Errorv(c, log.KV("TokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  39. return
  40. }
  41. res = &model.Report{}
  42. err = conn.Scan(reply, res)
  43. if err != nil {
  44. prom.BusinessErrCount.Incr("mc:TokenCache")
  45. log.Errorv(c, log.KV("TokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  46. return
  47. }
  48. return
  49. }
  50. // AddTokenCache Set data to mc
  51. func (d *Dao) AddTokenCache(c context.Context, id string, val *model.Report) (err error) {
  52. if val == nil {
  53. return
  54. }
  55. conn := d.mc.Get(c)
  56. defer conn.Close()
  57. key := tokenKey(id)
  58. item := &memcache.Item{Key: key, Object: val, Expiration: d.mcReportExpire, Flags: memcache.FlagJSON}
  59. if err = conn.Set(item); err != nil {
  60. prom.BusinessErrCount.Incr("mc:AddTokenCache")
  61. log.Errorv(c, log.KV("AddTokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  62. return
  63. }
  64. return
  65. }
  66. // AddTokensCache Set data to mc
  67. func (d *Dao) AddTokensCache(c context.Context, values map[string]*model.Report) (err error) {
  68. if len(values) == 0 {
  69. return
  70. }
  71. conn := d.mc.Get(c)
  72. defer conn.Close()
  73. for id, val := range values {
  74. key := tokenKey(id)
  75. item := &memcache.Item{Key: key, Object: val, Expiration: d.mcReportExpire, Flags: memcache.FlagJSON}
  76. if err = conn.Set(item); err != nil {
  77. prom.BusinessErrCount.Incr("mc:AddTokensCache")
  78. log.Errorv(c, log.KV("AddTokensCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  79. return
  80. }
  81. }
  82. return
  83. }
  84. // DelTokenCache delete data from mc
  85. func (d *Dao) DelTokenCache(c context.Context, id string) (err error) {
  86. conn := d.mc.Get(c)
  87. defer conn.Close()
  88. key := tokenKey(id)
  89. if err = conn.Delete(key); err != nil {
  90. if err == memcache.ErrNotFound {
  91. err = nil
  92. return
  93. }
  94. prom.BusinessErrCount.Incr("mc:DelTokenCache")
  95. log.Errorv(c, log.KV("DelTokenCache", fmt.Sprintf("%+v", err)), log.KV("key", key))
  96. return
  97. }
  98. return
  99. }