recent_redis.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/library/cache/redis"
  6. )
  7. // AddRctFollower is
  8. func (d *Dao) AddRctFollower(c context.Context, mid, fid int64) error {
  9. key := recentFollower(fid)
  10. conn := d.redis.Get(c)
  11. defer conn.Close()
  12. if err := conn.Send("ZADD", key, time.Now().Unix(), mid); err != nil {
  13. return err
  14. }
  15. if err := conn.Send("EXPIRE", key, d.UnreadDuration); err != nil {
  16. return err
  17. }
  18. if err := conn.Flush(); err != nil {
  19. return err
  20. }
  21. return nil
  22. }
  23. // DelRctFollower is
  24. func (d *Dao) DelRctFollower(c context.Context, mid, fid int64) error {
  25. key := recentFollower(fid)
  26. conn := d.redis.Get(c)
  27. defer conn.Close()
  28. _, err := conn.Do("ZREM", key, mid)
  29. return err
  30. }
  31. // RctFollowerCount is
  32. func (d *Dao) RctFollowerCount(ctx context.Context, fid int64) (int64, error) {
  33. key := recentFollower(fid)
  34. conn := d.redis.Get(ctx)
  35. defer conn.Close()
  36. count, err := redis.Int64(conn.Do("ZCARD", key))
  37. if err != nil {
  38. return 0, err
  39. }
  40. return count, nil
  41. }
  42. // EmptyRctFollower is
  43. func (d *Dao) EmptyRctFollower(ctx context.Context, fid int64) error {
  44. key := recentFollower(fid)
  45. conn := d.redis.Get(ctx)
  46. defer conn.Close()
  47. _, err := conn.Do("DEL", key)
  48. return err
  49. }
  50. // RctFollowerNotify is
  51. func (d *Dao) RctFollowerNotify(c context.Context, fid int64) (bool, error) {
  52. key := recentFollowerNotify(fid)
  53. conn := d.redis.Get(c)
  54. defer conn.Close()
  55. flagi, err := redis.Int64(conn.Do("HGET", key, fid))
  56. if err != nil {
  57. if err == redis.ErrNil {
  58. return false, nil
  59. }
  60. return false, err
  61. }
  62. flag := false
  63. if flagi > 0 {
  64. flag = true
  65. }
  66. return flag, err
  67. }
  68. // SetRctFollowerNotify is
  69. func (d *Dao) SetRctFollowerNotify(c context.Context, fid int64, flag bool) error {
  70. key := recentFollowerNotify(fid)
  71. flagi := 0
  72. if flag {
  73. flagi = 1
  74. }
  75. conn := d.redis.Get(c)
  76. defer conn.Close()
  77. if err := conn.Send("HSET", key, fid, flagi); err != nil {
  78. return err
  79. }
  80. if err := conn.Send("EXPIRE", key, d.UnreadDuration); err != nil {
  81. return err
  82. }
  83. if err := conn.Flush(); err != nil {
  84. return err
  85. }
  86. return nil
  87. }