redis.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "go-common/library/xstr"
  9. )
  10. func hashStatsKey(businessID, originID int64) string {
  11. return fmt.Sprintf("stats_o_%d_b_%d", originID, businessID)
  12. }
  13. // ExpireHashStatsCache .
  14. func (d *Dao) ExpireHashStatsCache(c context.Context, businessID, originID int64) (ok bool, err error) {
  15. conn := d.redis.Get(c)
  16. defer conn.Close()
  17. key := hashStatsKey(businessID, originID)
  18. if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.redisStatsExpire)); err != nil {
  19. PromError("redis:计数缓存设定过期")
  20. log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, d.redisStatsExpire, err)
  21. }
  22. return
  23. }
  24. // DelHashStatsCache del hash cache
  25. func (d *Dao) DelHashStatsCache(c context.Context, businessID, originID int64) (err error) {
  26. conn := d.redis.Get(c)
  27. defer conn.Close()
  28. key := hashStatsKey(businessID, originID)
  29. if _, err = conn.Do("del", key); err != nil {
  30. PromError("redis:计数缓存删除")
  31. log.Error("conn.Do(DEL, %s) error(%v)", key, err)
  32. }
  33. return
  34. }
  35. // HashStatsCache .
  36. func (d *Dao) HashStatsCache(c context.Context, businessID, originID int64, messageIDs []int64) (res map[int64]*model.Stats, err error) {
  37. if len(messageIDs) == 0 {
  38. return
  39. }
  40. conn := d.redis.Get(c)
  41. defer conn.Close()
  42. key := hashStatsKey(businessID, originID)
  43. var ss []string
  44. var commonds []interface{}
  45. commonds = append(commonds, key)
  46. for _, m := range messageIDs {
  47. commonds = append(commonds, m)
  48. }
  49. if ss, err = redis.Strings(conn.Do("HMGET", commonds...)); err != nil {
  50. if err == redis.ErrNil {
  51. err = nil
  52. } else {
  53. log.Error("conn.Do(HMGET, %s, %v) error(%v)", key, messageIDs, err)
  54. PromError("redis:获取统计信息")
  55. }
  56. return
  57. }
  58. res = make(map[int64]*model.Stats)
  59. for i, id := range messageIDs {
  60. if ss[i] == "" {
  61. continue
  62. }
  63. stat := &model.Stats{ID: id, OriginID: originID}
  64. num, _ := xstr.SplitInts(ss[i])
  65. if len(num) > 1 {
  66. stat.Likes = num[0]
  67. stat.Dislikes = num[1]
  68. }
  69. res[id] = stat
  70. }
  71. return
  72. }
  73. // AddHashStatsCache .
  74. func (d *Dao) AddHashStatsCache(c context.Context, businessID, originID int64, stats ...*model.Stats) (err error) {
  75. if len(stats) == 0 {
  76. return
  77. }
  78. conn := d.redis.Get(c)
  79. defer conn.Close()
  80. key := hashStatsKey(businessID, originID)
  81. var commonds = []interface{}{key}
  82. for _, stat := range stats {
  83. commonds = append(commonds, stat.ID, xstr.JoinInts([]int64{stat.Likes, stat.Dislikes}))
  84. }
  85. if _, err = conn.Do("HMSET", commonds...); err != nil {
  86. PromError("redis:增加统计信息")
  87. log.Error("conn.DO(HMSET, %s, %v) error(%v)", key, commonds, err)
  88. }
  89. return
  90. }
  91. // AddHashStatsCacheMap .
  92. func (d *Dao) AddHashStatsCacheMap(c context.Context, businessID, originID int64, stats map[int64]*model.Stats) (err error) {
  93. var s []*model.Stats
  94. for _, v := range stats {
  95. s = append(s, v)
  96. }
  97. return d.AddHashStatsCache(c, businessID, originID, s...)
  98. }