redis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/library/cache/redis"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _qidByTypeID = "v3_qus_tids_"
  10. _extraQidByTypeID = "v3_eq_t_"
  11. )
  12. func qusByType(tid int) string {
  13. return _qidByTypeID + strconv.FormatInt(int64(tid), 10)
  14. }
  15. func extraQidByType(tid int8) string {
  16. return _extraQidByTypeID + strconv.FormatInt(int64(tid), 10)
  17. }
  18. func (d *Dao) pingRedis(c context.Context) (err error) {
  19. conn := d.redis.Get(c)
  20. _, err = conn.Do("SET", "PING", "PONG")
  21. conn.Close()
  22. return
  23. }
  24. // QidByType get question by type.
  25. func (d *Dao) QidByType(c context.Context, tid int, num uint8) (ids []int64, err error) {
  26. key := qusByType(tid)
  27. conn := d.redis.Get(c)
  28. defer conn.Close()
  29. if ids, err = redis.Int64s(conn.Do("SRANDMEMBER", key, num)); err != nil {
  30. log.Error("RandBaseQs conn.Send('SRANDMEMBER', %s, %d) error(%v)", key, num, err)
  31. }
  32. return
  33. }
  34. // SetQids set question ids.
  35. func (d *Dao) SetQids(c context.Context, qs []int64, typeID int) (err error) {
  36. if len(qs) == 0 {
  37. return
  38. }
  39. key := qusByType(typeID)
  40. conn := d.redis.Get(c)
  41. defer conn.Close()
  42. args := make([]interface{}, 0, len(qs)+1)
  43. args = append(args, key)
  44. for _, q := range qs {
  45. args = append(args, q)
  46. }
  47. if _, err = conn.Do("SADD", args...); err != nil {
  48. log.Error("conn.Send(SADD, %v) error(%v)", args, err)
  49. }
  50. return
  51. }
  52. // SetExtraQids set extra question ids.
  53. func (d *Dao) SetExtraQids(c context.Context, qs []int64, ans int8) (err error) {
  54. if len(qs) == 0 {
  55. return
  56. }
  57. key := extraQidByType(ans)
  58. conn := d.redis.Get(c)
  59. defer conn.Close()
  60. args := make([]interface{}, 0, len(qs)+1)
  61. args = append(args, key)
  62. for _, q := range qs {
  63. args = append(args, q)
  64. }
  65. if _, err = conn.Do("SADD", args...); err != nil {
  66. log.Error("conn.Send(SADD, %v) error(%v)", args, err)
  67. }
  68. return
  69. }
  70. // DelQidsCache del qids cahce.
  71. func (d *Dao) DelQidsCache(c context.Context, typeID int) (err error) {
  72. key := qusByType(typeID)
  73. conn := d.redis.Get(c)
  74. defer conn.Close()
  75. if err = conn.Send("DEL", key); err != nil {
  76. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  77. }
  78. return
  79. }
  80. // DelExtraQidsCache del extra qids cahce.
  81. func (d *Dao) DelExtraQidsCache(c context.Context, ans int8) (err error) {
  82. key := extraQidByType(ans)
  83. conn := d.redis.Get(c)
  84. defer conn.Close()
  85. if err = conn.Send("DEL", key); err != nil {
  86. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  87. }
  88. return
  89. }
  90. // ExtraQidByType extra qis by type.
  91. func (d *Dao) ExtraQidByType(c context.Context, ans int8, num uint8) (ids []int64, err error) {
  92. key := extraQidByType(ans)
  93. conn := d.redis.Get(c)
  94. defer conn.Close()
  95. if ids, err = redis.Int64s(conn.Do("SRANDMEMBER", key, num)); err != nil {
  96. log.Error("ExtraQidByType conn.Send('SRANDMEMBER', %s, %d) error(%v)", key, num, err)
  97. }
  98. return
  99. }