redis.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/workflow/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _listKeyFormat = "wf_%d_%d"
  11. _fbRound = 11
  12. _auditFlowDealType = 0
  13. _feedbackFlowDealType = 1
  14. _adminOnlineHash = "wkf_online_hash"
  15. _prefixAdminOnlineField = "wkf_online_uid_%d"
  16. )
  17. func (d *Dao) pingRedis(c context.Context) (err error) {
  18. conn := d.redis.Get(c)
  19. _, err = conn.Do("SET", "PING", "PONG")
  20. conn.Close()
  21. return
  22. }
  23. // redis list key wf_business_dealtype like wf_2_1
  24. // dealtype=0 audit dealtype=1 feedback
  25. // RedisRPOPCids returns cids from a list
  26. func (d *Dao) RedisRPOPCids(c context.Context, business int8, round int64, num int8) (cids []int64, err error) {
  27. var (
  28. key string
  29. conn = d.redis.Get(c)
  30. flow int
  31. cid int64
  32. chall *model.Chall
  33. )
  34. defer conn.Close()
  35. cids = make([]int64, 0)
  36. if round == _fbRound {
  37. flow = _feedbackFlowDealType
  38. } else {
  39. flow = _auditFlowDealType
  40. }
  41. key = fmt.Sprintf(_listKeyFormat, business, flow)
  42. for {
  43. exist := false
  44. if exist, err = redis.Bool(conn.Do("EXISTS", key)); err != nil {
  45. log.Error("redis.Bool key(%s) err(%v)", key, err)
  46. return
  47. }
  48. if !exist {
  49. log.Warn("key(%s) not exist", key)
  50. return
  51. }
  52. if cid, err = redis.Int64(conn.Do("RPOP", key)); err != nil {
  53. log.Error("conn.Do(RPOP,%s) error(%v)", key, err)
  54. return
  55. }
  56. // judge if business_state is queue state
  57. if chall, err = d.Chall(c, cid); err != nil {
  58. return
  59. }
  60. chall.FromState()
  61. if chall.BusinessState == model.QueueState {
  62. cids = append(cids, cid)
  63. }
  64. if len(cids) >= int(num) {
  65. break
  66. }
  67. }
  68. return
  69. }
  70. // IsOnline judge if admin is online
  71. func (d *Dao) IsOnline(c context.Context, assigneeAdminID int64) (online bool, err error) {
  72. conn := d.redis.Get(c)
  73. defer conn.Close()
  74. key := _adminOnlineHash
  75. field := d.fieldOnlineList(assigneeAdminID)
  76. return redis.Bool(conn.Do("HEXISTS", key, field))
  77. }
  78. // AddOnline checkin if start subscribe mission in platform, set key
  79. func (d *Dao) AddOnline(c context.Context, assigneeAdminID int64) (err error) {
  80. conn := d.redis.Get(c)
  81. defer conn.Close()
  82. key := _adminOnlineHash
  83. field := d.fieldOnlineList(assigneeAdminID)
  84. _, err = conn.Do("HSET", key, field, assigneeAdminID)
  85. return
  86. }
  87. // DelOnline checkout if exit subscribe mission in platform, delete key
  88. func (d *Dao) DelOnline(c context.Context, assigneeAdminID int64) (err error) {
  89. conn := d.redis.Get(c)
  90. defer conn.Close()
  91. key := _adminOnlineHash
  92. field := d.fieldOnlineList(assigneeAdminID)
  93. _, err = conn.Do("HDEL", key, field)
  94. return
  95. }
  96. // ListOnline list online admin
  97. func (d *Dao) ListOnline(c context.Context) (ids []int64, err error) {
  98. conn := d.redis.Get(c)
  99. defer conn.Close()
  100. key := _adminOnlineHash
  101. return redis.Int64s(conn.Do("HVALS", key))
  102. }
  103. // LogInOutTime show last online or offline time
  104. func (d *Dao) LogInOutTime(c context.Context, uids []int64) {
  105. }
  106. // uid field in key wkf_online_hash
  107. func (d *Dao) fieldOnlineList(assigneeAdminID int64) string {
  108. return fmt.Sprintf(_prefixAdminOnlineField, assigneeAdminID)
  109. }
  110. func (d *Dao) keyChallCount(assigneeAdminID int64) string {
  111. return fmt.Sprintf(_prefixChallPendingCount, assigneeAdminID)
  112. }