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