memcache.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package data
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/creative/model/data"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefix = "s_"
  11. _upBasePrefix = "sup_"
  12. )
  13. func keyStat(mid int64) string {
  14. return _prefix + strconv.FormatInt(mid, 10)
  15. }
  16. func keyUpStat(mid int64, date string) string {
  17. return _upBasePrefix + date + strconv.FormatInt(mid, 10)
  18. }
  19. // statCache get stat cache.
  20. func (d *Dao) statCache(c context.Context, mid int64) (st *data.Stat, err error) {
  21. var (
  22. conn = d.mc.Get(c)
  23. r *memcache.Item
  24. )
  25. defer conn.Close()
  26. r, err = conn.Get(keyStat(mid))
  27. if err != nil {
  28. if err == memcache.ErrNotFound {
  29. err = nil
  30. } else {
  31. log.Error("conn.Get2(%d) error(%v)", mid, err)
  32. }
  33. return
  34. }
  35. if err = conn.Scan(r, &st); err != nil {
  36. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  37. st = nil
  38. }
  39. return
  40. }
  41. // addStatCache add stat cache.
  42. func (d *Dao) addStatCache(c context.Context, mid int64, st *data.Stat) (err error) {
  43. var (
  44. key = keyStat(mid)
  45. )
  46. conn := d.mc.Get(c)
  47. defer conn.Close()
  48. if err = conn.Set(&memcache.Item{Key: key, Object: st, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  49. log.Error("memcache.Set(%v) error(%v)", key, err)
  50. }
  51. return
  52. }
  53. // upBaseStatCache get stat cache.
  54. func (d *Dao) upBaseStatCache(c context.Context, mid int64, dt string) (st *data.UpBaseStat, err error) {
  55. var (
  56. conn = d.mc.Get(c)
  57. r *memcache.Item
  58. )
  59. defer conn.Close()
  60. r, err = conn.Get(keyUpStat(mid, dt))
  61. if err != nil {
  62. if err == memcache.ErrNotFound {
  63. log.Error("upBaseStatCache conn.Get2(%d) key not found", mid)
  64. err = nil
  65. } else {
  66. log.Error("upBaseStatCache conn.Get2(%d) error(%v)", mid, err)
  67. }
  68. return
  69. }
  70. if err = conn.Scan(r, &st); err != nil {
  71. log.Error("upBaseStatCache json.Unmarshal(%s) error(%v)", r.Value, err)
  72. st = nil
  73. }
  74. return
  75. }
  76. // addUpBaseStatCache add stat cache.
  77. func (d *Dao) addUpBaseStatCache(c context.Context, mid int64, dt string, st *data.UpBaseStat) (err error) {
  78. key := keyUpStat(mid, dt)
  79. conn := d.mc.Get(c)
  80. defer conn.Close()
  81. if err = conn.Set(&memcache.Item{Key: key, Object: st, Flags: memcache.FlagJSON, Expiration: d.mcIdxExpire}); err != nil {
  82. log.Error("memcache.Set(%v) error(%v)", key, err)
  83. }
  84. return
  85. }
  86. // DelUpBaseStatCache fn
  87. func (d *Dao) DelUpBaseStatCache(c context.Context, mid int64, dt string) (err error) {
  88. key := keyUpStat(mid, dt)
  89. conn := d.mc.Get(c)
  90. defer conn.Close()
  91. if err = conn.Delete(key); err != nil {
  92. log.Error("memcache.del(%v) error(%v)", key, err)
  93. }
  94. return
  95. }