mc.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package report
  2. import (
  3. "context"
  4. "time"
  5. mdlpgc "go-common/app/job/main/tv/model/pgc"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _report = "_report"
  11. _style = "style_label"
  12. _label = "label_data"
  13. )
  14. // SetReportCache set report cache .
  15. func (d *Dao) SetReportCache(c context.Context, val map[string]interface{}) (err error) {
  16. var (
  17. conn = d.mc.Get(c)
  18. key = _report
  19. )
  20. defer conn.Close()
  21. item := &memcache.Item{
  22. Key: key,
  23. Object: val,
  24. Flags: memcache.FlagJSON,
  25. Expiration: int32(time.Duration(d.conf.Report.Expire) / time.Second),
  26. }
  27. if err = conn.Set(item); err != nil {
  28. log.Error("conn.Set(%v) error(%v)", item, err)
  29. }
  30. return
  31. }
  32. // GetReportCache get report all data .
  33. func (d *Dao) GetReportCache(c context.Context) (res map[string]interface{}, err error) {
  34. var (
  35. conn = d.mc.Get(c)
  36. key = _report
  37. rp *memcache.Item
  38. )
  39. res = make(map[string]interface{})
  40. defer conn.Close()
  41. if rp, err = conn.Get(key); err != nil {
  42. if err == memcache.ErrNotFound {
  43. err = nil
  44. } else {
  45. log.Error("mc.Get(%s) error(%v)", key, err)
  46. }
  47. return
  48. }
  49. if err = conn.Scan(rp, &res); err != nil {
  50. log.Error("conn.Scan error(%v)", err)
  51. }
  52. return
  53. }
  54. // SetStyleCache style show .
  55. func (d *Dao) SetStyleCache(c context.Context, val map[int][]*mdlpgc.ParamStyle) (err error) {
  56. var (
  57. conn = d.mc.Get(c)
  58. key = _style
  59. )
  60. defer conn.Close()
  61. item := &memcache.Item{
  62. Key: key,
  63. Object: val,
  64. Flags: memcache.FlagJSON,
  65. Expiration: 0,
  66. }
  67. if err = conn.Set(item); err != nil {
  68. log.Error("conn.Set(%v) error(%v)", item, err)
  69. }
  70. return
  71. }
  72. // SetLabelCache label show .
  73. func (d *Dao) SetLabelCache(c context.Context, val map[int]map[string]int) (err error) {
  74. var (
  75. conn = d.mc.Get(c)
  76. key = _label
  77. )
  78. defer conn.Close()
  79. item := &memcache.Item{
  80. Key: key,
  81. Object: val,
  82. Flags: memcache.FlagJSON,
  83. Expiration: 0,
  84. }
  85. if err = conn.Set(item); err != nil {
  86. log.Error("conn.Set(%v) error(%v)", item, err)
  87. }
  88. return
  89. }
  90. //
  91. // GetLabelCache get label all data .
  92. func (d *Dao) GetLabelCache(c context.Context) (res map[int]map[string]int, err error) {
  93. var (
  94. conn = d.mc.Get(c)
  95. key = _label
  96. rp *memcache.Item
  97. )
  98. defer conn.Close()
  99. if rp, err = conn.Get(key); err != nil {
  100. if err == memcache.ErrNotFound {
  101. err = nil
  102. } else {
  103. log.Error("mc.Get(%s) error(%v)", key, err)
  104. }
  105. return
  106. }
  107. if err = conn.Scan(rp, &res); err != nil {
  108. log.Error("conn.Scan error(%v)", err)
  109. }
  110. return
  111. }