memcache.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package account
  2. import (
  3. "context"
  4. "strconv"
  5. accmdl "go-common/app/interface/main/creative/model/account"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _upInfoPrefix = "upinfo_"
  11. _addMidHalfMinPrefix = "add_midhafmin_"
  12. )
  13. func limitMidHafMin(mid int64) string {
  14. return _addMidHalfMinPrefix + strconv.FormatInt(mid, 10)
  15. }
  16. func keyUpInfo(mid int64) string {
  17. return _upInfoPrefix + strconv.FormatInt(mid, 10)
  18. }
  19. // HalfMin fn
  20. func (d *Dao) HalfMin(c context.Context, mid int64) (exist bool, ts uint64, err error) {
  21. var (
  22. conn = d.mc.Get(c)
  23. rp *memcache.Item
  24. )
  25. defer conn.Close()
  26. key := limitMidHafMin(mid)
  27. rp, err = conn.Get(key)
  28. if err != nil {
  29. if err == memcache.ErrNotFound {
  30. err = nil
  31. } else {
  32. log.Error("conn.Get error(%v) | key(%s) mid(%d)", err, key, mid)
  33. }
  34. return
  35. }
  36. if err = conn.Scan(rp, &ts); err != nil {
  37. log.Error("conn.Scan(%s) error(%v)", rp.Value, err)
  38. return
  39. }
  40. log.Info("HalfMin (%d) | key(%s) ts(%d)", mid, key, ts)
  41. if ts != 0 {
  42. exist = true
  43. }
  44. return
  45. }
  46. // UpInfoCache get stat cache.
  47. func (d *Dao) UpInfoCache(c context.Context, mid int64) (st *accmdl.UpInfo, err error) {
  48. var (
  49. conn = d.mc.Get(c)
  50. r *memcache.Item
  51. )
  52. defer conn.Close()
  53. r, err = conn.Get(keyUpInfo(mid))
  54. if err != nil {
  55. if err == memcache.ErrNotFound {
  56. err = nil
  57. } else {
  58. log.Error("conn.Get2(%d) error(%v)", mid, err)
  59. }
  60. return
  61. }
  62. if err = conn.Scan(r, &st); err != nil {
  63. log.Error("json.Unmarshal(%s) error(%v)", r.Value, err)
  64. st = nil
  65. }
  66. return
  67. }
  68. // AddUpInfoCache add stat cache.
  69. func (d *Dao) AddUpInfoCache(c context.Context, mid int64, st *accmdl.UpInfo) (err error) {
  70. var (
  71. key = keyUpInfo(mid)
  72. )
  73. conn := d.mc.Get(c)
  74. defer conn.Close()
  75. if err = conn.Set(&memcache.Item{Key: key, Object: st, Flags: memcache.FlagJSON, Expiration: d.mcExpire}); err != nil {
  76. log.Error("memcache.Set(%v) error(%v)", key, err)
  77. }
  78. return
  79. }