memcache.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "fmt"
  5. model "go-common/app/interface/main/creative/model/weeklyhonor"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _statKey = "hs_%d_%s"
  11. _honorKey = "ho_%d_%s"
  12. _clickKey = "hc_%d"
  13. )
  14. func statKey(mid int64, date string) string {
  15. return fmt.Sprintf(_statKey, mid, date)
  16. }
  17. func honorKey(mid int64, date string) string {
  18. return fmt.Sprintf(_honorKey, mid, date)
  19. }
  20. func honorClickKey(mid int64) string {
  21. return fmt.Sprint(_clickKey, mid)
  22. }
  23. // StatMC get stat cache.
  24. func (d *Dao) StatMC(c context.Context, mid int64, date string) (hs *model.HonorStat, err error) {
  25. var (
  26. key = statKey(mid, date)
  27. conn = d.mc.Get(c)
  28. )
  29. defer conn.Close()
  30. r, err := conn.Get(key)
  31. if err != nil {
  32. if err == memcache.ErrNotFound {
  33. err = nil
  34. } else {
  35. log.Error("conn.Get(%s) error(%v)", key, err)
  36. }
  37. return
  38. }
  39. if err = conn.Scan(r, &hs); err != nil {
  40. log.Error("conn.Scan(%s) error(%v)", r.Value, err)
  41. hs = nil
  42. }
  43. return
  44. }
  45. // HonorMC get honor cache.
  46. func (d *Dao) HonorMC(c context.Context, mid int64, date string) (res *model.HonorLog, err error) {
  47. var (
  48. key = honorKey(mid, date)
  49. conn = d.mc.Get(c)
  50. )
  51. defer conn.Close()
  52. r, err := conn.Get(key)
  53. if err != nil {
  54. if err == memcache.ErrNotFound {
  55. err = nil
  56. } else {
  57. log.Error("conn.Get(%s) error(%v)", key, err)
  58. }
  59. return
  60. }
  61. if err = conn.Scan(r, &res); err != nil {
  62. log.Error("conn.Scan(%s) error(%v)", r.Value, err)
  63. res = nil
  64. }
  65. return
  66. }
  67. // SetStatMC add stat cache.
  68. func (d *Dao) SetStatMC(c context.Context, mid int64, date string, hs *model.HonorStat) (err error) {
  69. var (
  70. key = statKey(mid, date)
  71. conn = d.mc.Get(c)
  72. )
  73. defer conn.Close()
  74. if err = conn.Set(&memcache.Item{Key: key, Object: hs, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  75. log.Error("memcache.Set(%s) error(%v)", key, err)
  76. }
  77. return
  78. }
  79. // SetHonorMC add honor cache.
  80. func (d *Dao) SetHonorMC(c context.Context, mid int64, date string, hs *model.HonorLog) (err error) {
  81. var (
  82. key = honorKey(mid, date)
  83. conn = d.mc.Get(c)
  84. )
  85. defer conn.Close()
  86. if err = conn.Set(&memcache.Item{Key: key, Object: hs, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  87. log.Error("memcache.Set(%s) error(%v)", key, err)
  88. }
  89. return
  90. }
  91. // SetClickMC add click cache
  92. func (d *Dao) SetClickMC(c context.Context, mid int64) (err error) {
  93. key := honorClickKey(mid)
  94. conn := d.mc.Get(c)
  95. defer conn.Close()
  96. if err = conn.Set(&memcache.Item{Key: key, Value: []byte{1}, Expiration: d.mcClickExpire}); err != nil {
  97. log.Error("memcache.Set(%s) error(%v)", key, err)
  98. }
  99. return
  100. }
  101. // ClickMC get click cache
  102. func (d *Dao) ClickMC(c context.Context, mid int64) (err error) {
  103. key := honorClickKey(mid)
  104. conn := d.mc.Get(c)
  105. defer conn.Close()
  106. _, err = conn.Get(key)
  107. return
  108. }