mc.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package anchorReward
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/log"
  7. "go-common/app/service/live/xrewardcenter/model/anchorTask"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. //NewRewardCache have new reward.
  12. NewRewardCache = "new_reward:v1:%d"
  13. //HasRewardCache have a reward
  14. HasRewardCache = "has_reward:v1:%d"
  15. )
  16. // HasNewReward .
  17. type HasNewReward struct {
  18. HasNewReward int64 `json:"has_new_reward"`
  19. }
  20. // pingMC .
  21. func (d *Dao) pingMC(c context.Context) (err error) {
  22. conn := d.mc.Get(c)
  23. defer conn.Close()
  24. if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
  25. err = errors.Wrap(err, "conn.Store(set,ping,1)")
  26. }
  27. return
  28. }
  29. func newRewardKey(uid int64) string {
  30. return fmt.Sprintf(NewRewardCache, uid)
  31. }
  32. // SetNewReward get MRRecord from mc
  33. func (d *Dao) SetNewReward(c context.Context, uid int64, hasNew int64) (err error) {
  34. var (
  35. conn = d.mc.Get(c)
  36. key = newRewardKey(uid)
  37. )
  38. defer conn.Close()
  39. log.Info("SetNewReward.mc.set(%v) (%v)", key, hasNew)
  40. if err = conn.Set(&memcache.Item{
  41. Key: key,
  42. Object: hasNew,
  43. Flags: memcache.FlagJSON,
  44. Expiration: 3600,
  45. }); err != nil {
  46. log.Error("SetNewReward.mc.set(%v %v) error(%v)", key, hasNew, err)
  47. return
  48. }
  49. return
  50. }
  51. // GetNewReward set MRRecord to mc
  52. func (d *Dao) GetNewReward(c context.Context, uid int64) (r int64, err error) {
  53. r = 0
  54. var (
  55. key = newRewardKey(uid)
  56. conn = d.mc.Get(c)
  57. )
  58. defer conn.Close()
  59. item, err := conn.Get(key)
  60. if err != nil {
  61. if err == memcache.ErrNotFound {
  62. return r, err
  63. }
  64. err = errors.Wrapf(err, "conn.get(%v,%v)", key, item)
  65. return r, err
  66. }
  67. if err = conn.Scan(item, &r); err != nil {
  68. log.Error("mc.Scan(%s) error(%v)", string(item.Value), err)
  69. }
  70. return
  71. }
  72. // GetHasReward .
  73. func (d *Dao) GetHasReward(c context.Context, uid int64) (r int64, err error) {
  74. var (
  75. key = hasRewardKey(uid)
  76. conn = d.mc.Get(c)
  77. )
  78. defer conn.Close()
  79. r = 0
  80. item, err := conn.Get(key)
  81. if err != nil {
  82. if err == memcache.ErrNotFound {
  83. return r, err
  84. }
  85. err = errors.Wrapf(err, "mc.get(%s,%v)", key, item)
  86. return r, err
  87. }
  88. if err = conn.Scan(item, &r); err != nil {
  89. log.Error("mc.Scan(%s) error(%v)", string(item.Value), err)
  90. }
  91. //spew.Dump(r, err)
  92. return
  93. }
  94. func hasRewardKey(uid int64) string {
  95. return fmt.Sprintf(HasRewardCache, uid)
  96. }
  97. // SetHasReward save whether one user have reward.
  98. func (d *Dao) SetHasReward(c context.Context, uid int64, isHave int64) (err error) {
  99. var (
  100. conn = d.mc.Get(c)
  101. key = hasRewardKey(uid)
  102. )
  103. defer conn.Close()
  104. log.Info("SetHasReward.mc.set(%v %v)", key, isHave)
  105. if err = conn.Set(&memcache.Item{
  106. Key: key,
  107. Object: isHave,
  108. Flags: memcache.FlagJSON,
  109. Expiration: 3600 * 24,
  110. }); err != nil {
  111. log.Error("SetHasReward.mc.set(%v %v) error(%v)", key, isHave, err)
  112. return
  113. }
  114. return
  115. }
  116. // GetOrder get order by id from cache.
  117. func (d *Dao) GetOrder(c context.Context, id string) (exists int64, err error) {
  118. conn := d.mc.Get(c)
  119. defer conn.Close()
  120. _, err = conn.Get(id)
  121. if err != nil {
  122. exists = 0
  123. return exists, err
  124. }
  125. exists = anchorTask.RewardExists
  126. return exists, err
  127. }
  128. // SaveOrder save orderid in cache.
  129. func (d *Dao) SaveOrder(c context.Context, id string) (err error) {
  130. conn := d.mc.Get(c)
  131. defer conn.Close()
  132. if err = conn.Set(&memcache.Item{
  133. Key: id,
  134. Value: []byte{1},
  135. Expiration: 3600,
  136. }); err != nil {
  137. log.Error("(%v) error(%v)", id, err)
  138. return
  139. }
  140. return
  141. }