redis_rct.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/job/main/dm2/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _prefixRecent = "dm_rct_"
  11. )
  12. func keyRecent(mid int64) string {
  13. return _prefixRecent + strconv.FormatInt(mid, 10)
  14. }
  15. // AddRecentDM add recent dm of up to redis.
  16. func (d *Dao) AddRecentDM(c context.Context, mid int64, dm *model.DM) (count int64, err error) {
  17. var (
  18. conn = d.dmRctRds.Get(c)
  19. key = keyRecent(mid)
  20. value []byte
  21. )
  22. defer conn.Close()
  23. if value, err = dm.Marshal(); err != nil {
  24. log.Error("dm.Marshal(%v) error(%v)", dm, err)
  25. return
  26. }
  27. if err = conn.Send("ZREMRANGEBYSCORE", key, dm.ID, dm.ID); err != nil {
  28. log.Error("conn.Do(ZREMRANGEBYSCORE %s) error(%v)", key, err)
  29. return
  30. }
  31. if err = conn.Send("ZADD", key, dm.ID, value); err != nil {
  32. log.Error("conn.Send(ZADD %v) error(%v)", dm, err)
  33. return
  34. }
  35. if err = conn.Send("EXPIRE", key, d.dmRctExpire); err != nil {
  36. log.Error("conn.Send(EXPIRE %s) error(%v)", key, err)
  37. return
  38. }
  39. if err = conn.Send("ZCARD", key); err != nil {
  40. log.Error("conn.Send(ZCARD %s) error(%v)", key, err)
  41. return
  42. }
  43. if err = conn.Flush(); err != nil {
  44. log.Error("conn.Flush() error(%v)", err)
  45. return
  46. }
  47. for i := 0; i < 3; i++ {
  48. if _, err = conn.Receive(); err != nil {
  49. log.Error("conn.Receive() error(%v)", err)
  50. return
  51. }
  52. }
  53. if count, err = redis.Int64(conn.Receive()); err != nil {
  54. log.Error("conn.Receive() error(%v)", err)
  55. return
  56. }
  57. return
  58. }
  59. // ZRemRecentDM remove recent dm of up.
  60. func (d *Dao) ZRemRecentDM(c context.Context, mid, dmid int64) (err error) {
  61. var (
  62. conn = d.dmRctRds.Get(c)
  63. key = keyRecent(mid)
  64. )
  65. defer conn.Close()
  66. if _, err = conn.Do("ZREMRANGEBYSCORE", key, dmid, dmid); err != nil {
  67. log.Error("conn.Do(ZREMRANGEBYSCORE %s) error(%v)", key, dmid)
  68. }
  69. return
  70. }
  71. // TrimRecentDM zrange remove recent dm of up.
  72. func (d *Dao) TrimRecentDM(c context.Context, mid, count int64) (err error) {
  73. var (
  74. conn = d.dmRctRds.Get(c)
  75. key = keyRecent(mid)
  76. )
  77. defer conn.Close()
  78. if _, err = conn.Do("ZREMRANGEBYRANK", key, 0, count-1); err != nil {
  79. log.Error("conn.Do(ZREMRANGEBYRANK %s) error(%v)", key, err)
  80. }
  81. return
  82. }