redis.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/job/main/credit/model"
  9. "go-common/library/cache/redis"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _voteOpIdx = "vo_%d_%d"
  14. _caseOpIdx = "caseop_"
  15. _blockIdx = "bl_%d_%d"
  16. _grantCaseKey = "gr_ca_li_v2"
  17. )
  18. func voteIndexKey(cid int64, otype int8) string {
  19. return fmt.Sprintf(_voteOpIdx, otype, cid)
  20. }
  21. func caseIndexKey(cid int64) string {
  22. return _caseOpIdx + strconv.FormatInt(cid, 10)
  23. }
  24. func blockIndexKey(otype, btype int64) string {
  25. return fmt.Sprintf(_blockIdx, otype, btype)
  26. }
  27. // DelCaseIdx DEL case opinion idx.
  28. func (d *Dao) DelCaseIdx(c context.Context, cid int64) (err error) {
  29. conn := d.redis.Get(c)
  30. defer conn.Close()
  31. if _, err = conn.Do("DEL", caseIndexKey(cid)); err != nil {
  32. log.Error("del case idx err(%v)", err)
  33. return
  34. }
  35. return
  36. }
  37. // DelBlockedInfoIdx ZREM block info idx.
  38. func (d *Dao) DelBlockedInfoIdx(c context.Context, bl *model.BlockedInfo) (err error) {
  39. conn := d.redis.Get(c)
  40. defer conn.Close()
  41. conn.Send("ZREM", blockIndexKey(bl.OriginType, bl.BlockedType), bl.ID)
  42. conn.Send("ZREM", blockIndexKey(0, -1), bl.ID)
  43. conn.Send("ZREM", blockIndexKey(0, bl.BlockedType), bl.ID)
  44. conn.Send("ZREM", blockIndexKey(bl.OriginType, -1), bl.ID)
  45. if err = conn.Flush(); err != nil {
  46. log.Error("conn.Flush err(%v)", err)
  47. return
  48. }
  49. for i := 0; i < 4; i++ {
  50. if _, err = conn.Receive(); err != nil {
  51. log.Error("conn.Receive() error(%v)", err)
  52. }
  53. }
  54. return
  55. }
  56. // AddBlockInfoIdx ZADD block info idx.
  57. func (d *Dao) AddBlockInfoIdx(c context.Context, bl *model.BlockedInfo) (err error) {
  58. conn := d.redis.Get(c)
  59. defer conn.Close()
  60. var mtime time.Time
  61. if mtime, err = time.ParseInLocation("2006-01-02 15:04:05", bl.MTime, time.Local); err != nil {
  62. log.Error("time.ParseInLocation err(%v)", err)
  63. return
  64. }
  65. conn.Send("ZADD", blockIndexKey(bl.OriginType, bl.BlockedType), mtime.Unix(), bl.ID)
  66. conn.Send("ZADD", blockIndexKey(0, -1), mtime.Unix(), bl.ID)
  67. conn.Send("ZADD", blockIndexKey(0, bl.BlockedType), mtime.Unix(), bl.ID)
  68. conn.Send("ZADD", blockIndexKey(bl.OriginType, -1), mtime.Unix(), bl.ID)
  69. if err = conn.Flush(); err != nil {
  70. log.Error("conn.Flush err(%v)", err)
  71. return
  72. }
  73. for i := 0; i < 4; i++ {
  74. if _, err = conn.Receive(); err != nil {
  75. log.Error("conn.Receive() error(%v)", err)
  76. }
  77. }
  78. return
  79. }
  80. // DelVoteIdx DEL case opinion idx.
  81. func (d *Dao) DelVoteIdx(c context.Context, cid int64) (err error) {
  82. conn := d.redis.Get(c)
  83. defer conn.Close()
  84. if err = conn.Send("DEL", voteIndexKey(cid, 1)); err != nil {
  85. log.Error("del case idx err(%v)", err)
  86. return
  87. }
  88. if err = conn.Send("DEL", voteIndexKey(cid, 2)); err != nil {
  89. log.Error("del case idx err(%v)", err)
  90. return
  91. }
  92. conn.Flush()
  93. for i := 0; i < 2; i++ {
  94. conn.Receive()
  95. }
  96. return
  97. }
  98. // SetGrantCase set grant case ids.
  99. func (d *Dao) SetGrantCase(c context.Context, mcases map[int64]*model.SimCase) (err error) {
  100. conn := d.redis.Get(c)
  101. defer conn.Close()
  102. args := redis.Args{}.Add(_grantCaseKey)
  103. for cid, mcase := range mcases {
  104. var bs []byte
  105. bs, err = json.Marshal(mcase)
  106. if err != nil {
  107. log.Error("json.Marshal(%+v) error(%v)", mcase, err)
  108. err = nil
  109. continue
  110. }
  111. args = args.Add(cid).Add(string(bs))
  112. }
  113. if _, err = conn.Do("HMSET", args...); err != nil {
  114. log.Error("conn.Send(HMSET,%v) error(%v)", args, err)
  115. }
  116. return
  117. }
  118. // DelGrantCase del grant case id.
  119. func (d *Dao) DelGrantCase(c context.Context, cids []int64) (err error) {
  120. var args = []interface{}{_grantCaseKey}
  121. conn := d.redis.Get(c)
  122. defer conn.Close()
  123. for _, cid := range cids {
  124. args = append(args, cid)
  125. }
  126. if _, err = conn.Do("HDEL", args...); err != nil {
  127. log.Error("conn.Send(HDEL,%s) err(%v)", _grantCaseKey, err)
  128. }
  129. return
  130. }
  131. // TotalGrantCase get length of grant case ids.
  132. func (d *Dao) TotalGrantCase(c context.Context) (count int, err error) {
  133. conn := d.redis.Get(c)
  134. defer conn.Close()
  135. if count, err = redis.Int(conn.Do("HLEN", _grantCaseKey)); err != nil {
  136. if err != redis.ErrNil {
  137. log.Error("conn.Do(HLEN, %s) error(%v)", _grantCaseKey, err)
  138. return
  139. }
  140. err = nil
  141. }
  142. return
  143. }
  144. // GrantCases get granting case cids.
  145. func (d *Dao) GrantCases(c context.Context) (cids []int64, err error) {
  146. conn := d.redis.Get(c)
  147. defer conn.Close()
  148. var ms map[string]string
  149. if ms, err = redis.StringMap(conn.Do("HGETALL", _grantCaseKey)); err != nil {
  150. if err == redis.ErrNil {
  151. err = nil
  152. }
  153. return
  154. }
  155. for m, s := range ms {
  156. if s == "" {
  157. continue
  158. }
  159. cid, err := strconv.ParseInt(m, 10, 64)
  160. if err != nil {
  161. log.Error("strconv.ParseInt(%s) error(%v)", m, err)
  162. err = nil
  163. continue
  164. }
  165. cids = append(cids, cid)
  166. }
  167. return
  168. }