item_likes_redis.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/thumbup/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. func itemLikesKey(businessID, messageID int64, state int8) string {
  11. return fmt.Sprintf("i2_m_%d_b_%d_%d", messageID, businessID, state)
  12. }
  13. // CacheItemLikeList .
  14. func (d *Dao) CacheItemLikeList(c context.Context, messageID, businessID int64, state int8, start, end int) (res []*model.UserLikeRecord, err error) {
  15. conn := d.redis.Get(c)
  16. defer conn.Close()
  17. key := itemLikesKey(businessID, messageID, state)
  18. items, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "withscores"))
  19. if err != nil {
  20. if err == redis.ErrNil {
  21. err = nil
  22. return
  23. }
  24. PromError("redis:CacheItemLikeList")
  25. log.Errorv(c, log.KV("CacheItemLikeList", fmt.Sprintf("%+v", err)))
  26. return
  27. }
  28. for len(items) > 0 {
  29. var id, t int64
  30. if items, err = redis.Scan(items, &id, &t); err != nil {
  31. PromError("redis:CacheItemLikeList")
  32. log.Errorv(c, log.KV("CacheItemLikeList", fmt.Sprintf("%+v", err)))
  33. return
  34. }
  35. res = append(res, &model.UserLikeRecord{Mid: id, Time: xtime.Time(t)})
  36. }
  37. return
  38. }
  39. // AddCacheItemLikeList .
  40. func (d *Dao) AddCacheItemLikeList(c context.Context, messageID int64, miss []*model.UserLikeRecord, businessID int64, state int8) (err error) {
  41. if len(miss) == 0 {
  42. return
  43. }
  44. limit := d.BusinessIDMap[businessID].MessageLikesLimit
  45. var count int
  46. conn := d.redis.Get(c)
  47. defer conn.Close()
  48. key := itemLikesKey(businessID, messageID, state)
  49. if err = conn.Send("DEL", key); err != nil {
  50. PromError("redis:项目点赞列表")
  51. log.Errorv(c, log.KV("AddCacheItemLikeList", fmt.Sprintf("AddCacheItemLikeList conn.Send(DEL, %s) error(%+v)", key, err)))
  52. return
  53. }
  54. count++
  55. for _, item := range miss {
  56. id := item.Mid
  57. score := int64(item.Time)
  58. if err = conn.Send("ZADD", key, "CH", score, id); err != nil {
  59. PromError("redis:项目点赞列表")
  60. log.Errorv(c, log.KV("log", fmt.Sprintf("AddCacheItemLikeList conn.Send(ZADD, %s, %d, %v) error(%v)", key, score, id, err)))
  61. return
  62. }
  63. count++
  64. }
  65. if err = conn.Send("ZREMRANGEBYRANK", key, 0, -(limit + 1)); err != nil {
  66. PromError("redis:项目点赞列表rm")
  67. log.Errorv(c, log.KV("log", fmt.Sprintf("AddCacheItemLikeList conn.Send(ZREMRANGEBYRANK, %s, 0, %d) error(%v)", key, -(limit+1), err)))
  68. return
  69. }
  70. count++
  71. if err = conn.Send("EXPIRE", key, d.redisItemLikesExpire); err != nil {
  72. PromError("redis:项目点赞列表过期")
  73. log.Errorv(c, log.KV("log", fmt.Sprintf("AddCacheItemLikeList conn.Send(EXPIRE, %s, %d) error(%v)", key, d.redisItemLikesExpire, err)))
  74. return
  75. }
  76. count++
  77. if err = conn.Flush(); err != nil {
  78. PromError("redis:项目点赞列表flush")
  79. log.Errorv(c, log.KV("log", fmt.Sprintf("AddCacheItemLikeList conn.Flush error(%v)", err)))
  80. return
  81. }
  82. for i := 0; i < count; i++ {
  83. if _, err = conn.Receive(); err != nil {
  84. PromError("redis:项目点赞列表receive")
  85. log.Errorv(c, log.KV("log", fmt.Sprintf("AddCacheItemLikeList conn.Receive error(%v)", err)), log.KV("miss", miss))
  86. return
  87. }
  88. }
  89. return
  90. }
  91. // ExpireItemLikesCache .
  92. func (d *Dao) ExpireItemLikesCache(c context.Context, messageID, businessID int64, state int8) (ok bool, err error) {
  93. conn := d.redis.Get(c)
  94. defer conn.Close()
  95. key := itemLikesKey(businessID, messageID, state)
  96. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisItemLikesExpire)); err != nil {
  97. PromError("redis:expire项目点赞列表")
  98. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(EXPIRE, %s) error(%v)", key, err)))
  99. }
  100. return
  101. }
  102. // ItemLikeExists .
  103. func (d *Dao) ItemLikeExists(c context.Context, messageID, businessID int64, mids []int64, state int8) (res map[int64]int64, err error) {
  104. conn := d.redis.Get(c)
  105. defer conn.Close()
  106. key := itemLikesKey(businessID, messageID, state)
  107. res = make(map[int64]int64)
  108. for _, name := range mids {
  109. conn.Send("ZSCORE", key, name)
  110. }
  111. if err = conn.Flush(); err != nil {
  112. PromError("redis:ItemLikeExists")
  113. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Flush() error(%v)", err)))
  114. return
  115. }
  116. for _, name := range mids {
  117. var s int64
  118. if s, err = redis.Int64(conn.Receive()); err == nil {
  119. res[name] = s
  120. } else if err == redis.ErrNil {
  121. err = nil
  122. } else {
  123. PromError("redis:ItemLikeExists")
  124. log.Errorv(c, log.KV("log", fmt.Sprintf("ItemLikeExists conn.Receive() error(%v)", err)))
  125. return
  126. }
  127. }
  128. return
  129. }
  130. // AppendCacheItemLikeList .
  131. func (d *Dao) AppendCacheItemLikeList(c context.Context, messageID int64, item *model.UserLikeRecord, businessID int64, state int8) (err error) {
  132. if item == nil {
  133. return
  134. }
  135. limit := d.BusinessIDMap[businessID].MessageLikesLimit
  136. var count int
  137. conn := d.redis.Get(c)
  138. defer conn.Close()
  139. key := itemLikesKey(businessID, messageID, state)
  140. id := item.Mid
  141. score := int64(item.Time)
  142. if err = conn.Send("ZADD", key, "CH", score, id); err != nil {
  143. PromError("redis:项目点赞列表")
  144. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZADD, %s, %d, %v) error(%v)", key, score, id, err)))
  145. return
  146. }
  147. count++
  148. if err = conn.Send("ZREMRANGEBYRANK", key, 0, -(limit + 1)); err != nil {
  149. PromError("redis:项目点赞列表rm")
  150. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZREMRANGEBYRANK, %s, 0, %d) error(%v)", key, -(limit+1), err)))
  151. return
  152. }
  153. count++
  154. if err = conn.Send("EXPIRE", key, d.redisItemLikesExpire); err != nil {
  155. PromError("redis:项目点赞列表过期")
  156. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(EXPIRE, %s, %d) error(%v)", key, d.redisItemLikesExpire, err)))
  157. return
  158. }
  159. count++
  160. if err = conn.Flush(); err != nil {
  161. PromError("redis:项目点赞列表flush")
  162. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Flush error(%v)", err)))
  163. return
  164. }
  165. for i := 0; i < count; i++ {
  166. if _, err = conn.Receive(); err != nil {
  167. PromError("redis:项目点赞列表receive")
  168. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Receive error(%v)", err)))
  169. return
  170. }
  171. }
  172. return
  173. }
  174. // DelItemLikeCache .
  175. func (d *Dao) DelItemLikeCache(c context.Context, messageID, businessID int64, mid int64, state int8) (err error) {
  176. conn := d.redis.Get(c)
  177. defer conn.Close()
  178. key := itemLikesKey(businessID, messageID, state)
  179. if _, err = conn.Do("ZREM", key, mid); err != nil {
  180. PromError("redis:zrem项目点赞列表")
  181. log.Errorv(c, log.KV("log", fmt.Sprintf("conn.Send(ZREM, %s, %v) error(%v)", key, mid, err)))
  182. }
  183. return
  184. }
  185. // ItemLikesCountCache .
  186. func (d *Dao) ItemLikesCountCache(c context.Context, businessID, messageID int64) (res int, err error) {
  187. conn := d.redis.Get(c)
  188. defer conn.Close()
  189. key := itemLikesKey(businessID, messageID, model.StateLike)
  190. res, err = redis.Int(conn.Do("ZCOUNT", key, "(0", "+inf"))
  191. if err != nil {
  192. log.Errorv(c, log.KV("log", fmt.Sprintf("dao.ItemLikesCountCache(%d, %d) err:%v", businessID, messageID, err)))
  193. PromError("redis:项目点赞总数")
  194. }
  195. return
  196. }