mc.cache.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
  2. /*
  3. Package archive is a generated mc cache package.
  4. It is generated from:
  5. type _mc interface {
  6. // mc: -key=staffKey
  7. CacheStaffData(c context.Context, key int64) ([]*archive.Staff, error)
  8. // 这里也支持自定义注释 会替换默认的注释
  9. // mc: -key=staffKey -expire=3 -encode=json|gzip
  10. AddCacheStaffData(c context.Context, key int64, value []*archive.Staff) error
  11. // mc: -key=staffKey
  12. DelCacheStaffData(c context.Context, key int64) error
  13. }
  14. */
  15. package archive
  16. import (
  17. "context"
  18. "fmt"
  19. "go-common/app/service/main/videoup/model/archive"
  20. "go-common/library/cache/memcache"
  21. "go-common/library/log"
  22. "go-common/library/stat/prom"
  23. )
  24. var _ _mc
  25. // CacheStaffData get data from mc
  26. func (d *Dao) CacheStaffData(c context.Context, id int64) (res []*archive.Staff, err error) {
  27. conn := d.mc.Get(c)
  28. defer conn.Close()
  29. key := staffKey(id)
  30. reply, err := conn.Get(key)
  31. if err != nil {
  32. if err == memcache.ErrNotFound {
  33. err = nil
  34. return
  35. }
  36. prom.BusinessErrCount.Incr("mc:CacheStaffData")
  37. log.Errorv(c, log.KV("CacheStaffData", fmt.Sprintf("%+v", err)), log.KV("key", key))
  38. return
  39. }
  40. res = []*archive.Staff{}
  41. err = conn.Scan(reply, &res)
  42. if err != nil {
  43. prom.BusinessErrCount.Incr("mc:CacheStaffData")
  44. log.Errorv(c, log.KV("CacheStaffData", fmt.Sprintf("%+v", err)), log.KV("key", key))
  45. return
  46. }
  47. return
  48. }
  49. // AddCacheStaffData 这里也支持自定义注释 会替换默认的注释
  50. func (d *Dao) AddCacheStaffData(c context.Context, id int64, val []*archive.Staff) (err error) {
  51. if len(val) == 0 {
  52. return
  53. }
  54. conn := d.mc.Get(c)
  55. defer conn.Close()
  56. key := staffKey(id)
  57. item := &memcache.Item{Key: key, Object: val, Expiration: 3, Flags: memcache.FlagJSON | memcache.FlagGzip}
  58. if err = conn.Set(item); err != nil {
  59. prom.BusinessErrCount.Incr("mc:AddCacheStaffData")
  60. log.Errorv(c, log.KV("AddCacheStaffData", fmt.Sprintf("%+v", err)), log.KV("key", key))
  61. return
  62. }
  63. return
  64. }
  65. // DelCacheStaffData delete data from mc
  66. func (d *Dao) DelCacheStaffData(c context.Context, id int64) (err error) {
  67. conn := d.mc.Get(c)
  68. defer conn.Close()
  69. key := staffKey(id)
  70. if err = conn.Delete(key); err != nil {
  71. if err == memcache.ErrNotFound {
  72. err = nil
  73. return
  74. }
  75. prom.BusinessErrCount.Incr("mc:DelCacheStaffData")
  76. log.Errorv(c, log.KV("DelCacheStaffData", fmt.Sprintf("%+v", err)), log.KV("key", key))
  77. return
  78. }
  79. return
  80. }