user_likes_redis.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/thumbup/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. func userLikesKey(businessID, mid int64, state int8) string {
  10. return fmt.Sprintf("u_m_%d_b_%d_%d", mid, businessID, state)
  11. }
  12. // ExpireUserLikesCache .
  13. func (d *Dao) ExpireUserLikesCache(c context.Context, mid, businessID int64, state int8) (ok bool, err error) {
  14. conn := d.redis.Get(c)
  15. defer conn.Close()
  16. key := userLikesKey(businessID, mid, state)
  17. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisUserLikesExpire)); err != nil {
  18. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(EXPIRE, %s) error(%v)", key, err)))
  19. }
  20. return
  21. }
  22. // AppendCacheUserLikeList .
  23. func (d *Dao) AppendCacheUserLikeList(c context.Context, mid int64, item *model.ItemLikeRecord, businessID int64, state int8, limit int) (err error) {
  24. if item == nil {
  25. return
  26. }
  27. var count int
  28. conn := d.redis.Get(c)
  29. defer conn.Close()
  30. key := userLikesKey(businessID, mid, state)
  31. id := item.MessageID
  32. score := int64(item.Time)
  33. if err = conn.Send("ZADD", key, "CH", score, id); err != nil {
  34. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZADD, %s, %d, %v) error(%v)", key, score, id, err)))
  35. return
  36. }
  37. count++
  38. if err = conn.Send("ZREMRANGEBYRANK", key, 0, -(limit + 1)); err != nil {
  39. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZREMRANGEBYRANK, %s, 0, %d) error(%v)", key, -(limit+1), err)))
  40. return
  41. }
  42. count++
  43. if err = conn.Send("EXPIRE", key, d.redisUserLikesExpire); err != nil {
  44. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(EXPIRE, %s, %d) error(%v)", key, d.redisUserLikesExpire, err)))
  45. return
  46. }
  47. count++
  48. if err = conn.Flush(); err != nil {
  49. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Flush error(%v)", err)))
  50. return
  51. }
  52. for i := 0; i < count; i++ {
  53. if _, err = conn.Receive(); err != nil {
  54. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Receive error(%v)", err)))
  55. return
  56. }
  57. }
  58. return
  59. }
  60. // AddUserLikesCache .
  61. func (d *Dao) AddUserLikesCache(c context.Context, mid, businessID int64, items []*model.ItemLikeRecord, typ int8, limit int) (err error) {
  62. if len(items) == 0 {
  63. return
  64. }
  65. conn := d.redis.Get(c)
  66. defer conn.Close()
  67. key := userLikesKey(businessID, mid, typ)
  68. if err = conn.Send("DEL", key); err != nil {
  69. log.Errorv(c, log.KV("AddCacheUserLikeList", fmt.Sprintf("conn.Send(DEL, %s) error(%+v)", key, err)))
  70. return
  71. }
  72. args := redis.Args{}.Add(key).Add("CH")
  73. for _, item := range items {
  74. args = args.Add(int64(item.Time)).Add(item.MessageID)
  75. }
  76. if err = conn.Send("ZADD", args...); err != nil {
  77. log.Error("zadd key(%s) args(%v) error(%v)", key, args, err)
  78. return
  79. }
  80. if err = conn.Send("ZREMRANGEBYRANK", key, 0, -(limit + 1)); err != nil {
  81. log.Error("zremrangebyrank error(%v)", err)
  82. return
  83. }
  84. if err = conn.Send("EXPIRE", key, d.redisUserLikesExpire); err != nil {
  85. log.Error("expire key(%s) error(%v)", key, err)
  86. return
  87. }
  88. if err = conn.Flush(); err != nil {
  89. log.Error("redis flush error(%v)", err)
  90. return
  91. }
  92. for i := 0; i < 4; i++ {
  93. if _, err = conn.Receive(); err != nil {
  94. log.Error("redis receive error(%v)", err)
  95. return
  96. }
  97. }
  98. return
  99. }
  100. // DelUserLikeCache .
  101. func (d *Dao) DelUserLikeCache(c context.Context, mid, businessID int64, messageID int64, state int8) (err error) {
  102. conn := d.redis.Get(c)
  103. defer conn.Close()
  104. key := userLikesKey(businessID, mid, state)
  105. if _, err = conn.Do("ZREM", key, messageID); err != nil {
  106. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZREM, %s, %v) error(%v)", key, messageID, err)))
  107. }
  108. return
  109. }