redis.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "go-common/app/service/main/history/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const _deleteDuration = 3600 * 12
  12. // keyIndex return history index key.
  13. func keyIndex(business string, mid int64) string {
  14. return fmt.Sprintf("i_%d_%s", mid, business)
  15. }
  16. // keyHistory return history key.
  17. func keyHistory(business string, mid int64) string {
  18. return fmt.Sprintf("h_%d_%s", mid, business)
  19. }
  20. // HistoriesCache return the user histories from redis.
  21. func (d *Dao) HistoriesCache(c context.Context, merges []*model.Merge) (res []*model.History, err error) {
  22. conn := d.redis.Get(c)
  23. defer conn.Close()
  24. for _, merge := range merges {
  25. key := keyHistory(d.BusinessesMap[merge.Bid].Name, merge.Mid)
  26. if err = conn.Send("HGET", key, merge.Kid); err != nil {
  27. log.Error("conn.Send(HGET %v %v) error(%v)", key, merge.Kid, err)
  28. return
  29. }
  30. }
  31. if err = conn.Flush(); err != nil {
  32. log.Error("conn.Flush() error(%v)", err)
  33. return
  34. }
  35. for i := 0; i < len(merges); i++ {
  36. var value []byte
  37. if value, err = redis.Bytes(conn.Receive()); err != nil {
  38. if err == redis.ErrNil {
  39. err = nil
  40. continue
  41. }
  42. log.Error("conn.Receive error(%v)", err)
  43. return
  44. }
  45. h := &model.History{}
  46. if err = json.Unmarshal(value, h); err != nil {
  47. log.Error("json.Unmarshal(%s) error(%v)", value, err)
  48. err = nil
  49. continue
  50. }
  51. h.BusinessID = d.BusinessNames[h.Business].ID
  52. res = append(res, h)
  53. }
  54. return
  55. }
  56. // TrimCache trim history.
  57. func (d *Dao) TrimCache(c context.Context, business string, mid int64, limit int) (err error) {
  58. conn := d.redis.Get(c)
  59. defer conn.Close()
  60. aids, err := redis.Int64s(conn.Do("ZRANGE", keyIndex(business, mid), 0, -limit-1))
  61. if err != nil {
  62. log.Error("conn.Do(ZRANGE %v) error(%v)", keyIndex(business, mid), err)
  63. return
  64. }
  65. if len(aids) == 0 {
  66. return
  67. }
  68. return d.DelCache(c, business, mid, aids)
  69. }
  70. // DelCache delete the history redis.
  71. func (d *Dao) DelCache(c context.Context, business string, mid int64, aids []int64) (err error) {
  72. var (
  73. key1 = keyIndex(business, mid)
  74. key2 = keyHistory(business, mid)
  75. args1 = []interface{}{key1}
  76. args2 = []interface{}{key2}
  77. )
  78. for _, aid := range aids {
  79. args1 = append(args1, aid)
  80. args2 = append(args2, aid)
  81. }
  82. conn := d.redis.Get(c)
  83. defer conn.Close()
  84. if err = conn.Send("ZREM", args1...); err != nil {
  85. log.Error("conn.Send(ZREM %s,%v) error(%v)", key1, aids, err)
  86. return
  87. }
  88. if err = conn.Send("HDEL", args2...); err != nil {
  89. log.Error("conn.Send(HDEL %s,%v) error(%v)", key2, aids, err)
  90. return
  91. }
  92. if err = conn.Flush(); err != nil {
  93. log.Error("conn.Flush() error(%v)", err)
  94. return
  95. }
  96. for i := 0; i < 2; i++ {
  97. if _, err = conn.Receive(); err != nil {
  98. log.Error("conn.Receive() error(%v)", err)
  99. return
  100. }
  101. }
  102. return
  103. }
  104. // DelLock delete proc lock
  105. func (d *Dao) DelLock(c context.Context) (ok bool, err error) {
  106. conn := d.redis.Get(c)
  107. defer conn.Close()
  108. key := "his_job_del_proc"
  109. if err = conn.Send("SETNX", key, time.Now().Unix()); err != nil {
  110. log.Error("DelLock conn.SETNX() error(%v)", err)
  111. return
  112. }
  113. if err = conn.Send("EXPIRE", key, _deleteDuration); err != nil {
  114. log.Error("DelLock conn.Expire() error(%v)", err)
  115. return
  116. }
  117. if err = conn.Flush(); err != nil {
  118. log.Error("DelLock conn.Flush() error(%v)", err)
  119. return
  120. }
  121. if ok, err = redis.Bool(conn.Receive()); err != nil {
  122. log.Error("conn.Receive() error(%v)", err)
  123. return
  124. }
  125. if _, err = conn.Receive(); err != nil {
  126. log.Error("conn.Receive() error(%v)", err)
  127. }
  128. return
  129. }