redis.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package redis
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/library/cache/redis"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _failList = "f_list"
  11. _clickSort = "c_sort"
  12. _prefixMonitor = "va_sc_set" // key of monitor on videoup job second round hit cache set
  13. _expire = 30 * 24 * 60 * 60 // 30 days
  14. _clickExpire = 10 * 24 * 60 * 60 // 10 days
  15. )
  16. var (
  17. _twepoch = time.Date(2017, time.Month(9), 1, 0, 0, 0, 0, time.Local).Unix()
  18. )
  19. // PushFail rpush fail item to redis
  20. func (d *Dao) PushFail(c context.Context, a interface{}) (err error) {
  21. var (
  22. conn = d.redis.Get(c)
  23. bs []byte
  24. )
  25. defer conn.Close()
  26. if bs, err = json.Marshal(a); err != nil {
  27. log.Error("json.Marshal(%v) error(%v)", a, err)
  28. return
  29. }
  30. if _, err = conn.Do("RPUSH", _failList, bs); err != nil {
  31. log.Error("conn.Do(RPUSH, %s, %s) error(%v)")
  32. }
  33. return
  34. }
  35. // PopFail lpop fail item from redis
  36. func (d *Dao) PopFail(c context.Context) (bs []byte, err error) {
  37. var conn = d.redis.Get(c)
  38. defer conn.Close()
  39. if bs, err = redis.Bytes(conn.Do("LPOP", _failList)); err != nil && err != redis.ErrNil {
  40. log.Error("redis.Bytes(conn.Do(LPOP, %s)) error(%v)", _failList, err)
  41. return
  42. }
  43. return
  44. }
  45. // PushQueue rpush fail item to redis
  46. func (d *Dao) PushQueue(c context.Context, a interface{}, queue string) (err error) {
  47. var (
  48. conn = d.redis.Get(c)
  49. bs []byte
  50. )
  51. defer conn.Close()
  52. if bs, err = json.Marshal(a); err != nil {
  53. log.Error("json.Marshal(%v) error(%v)", a, err)
  54. return
  55. }
  56. if _, err = conn.Do("RPUSH", queue, bs); err != nil {
  57. log.Error("conn.Do(RPUSH, %s, %s) error(%v)")
  58. }
  59. return
  60. }
  61. // PopQueue lpop fail item from redis
  62. func (d *Dao) PopQueue(c context.Context, queue string) (bs []byte, err error) {
  63. var conn = d.redis.Get(c)
  64. defer conn.Close()
  65. if bs, err = redis.Bytes(conn.Do("LPOP", queue)); err != nil && err != redis.ErrNil {
  66. log.Error("redis.Bytes(conn.Do(LPOP, %s)) error(%v)", queue, err)
  67. return
  68. }
  69. return
  70. }
  71. // AddFilename set filename expire time
  72. func (d *Dao) AddFilename(c context.Context, filename string) (err error) {
  73. var conn = d.redis.Get(c)
  74. defer conn.Close()
  75. if _, err = conn.Do("SETEX", filename, _expire, time.Now().Unix()); err != nil {
  76. log.Error("conn.Do(SETEX, %s, %d, %d) error(%v)", filename, _expire, time.Now().Unix(), err)
  77. }
  78. return
  79. }
  80. // DelFilename set filename expire time
  81. func (d *Dao) DelFilename(c context.Context, filename string) (err error) {
  82. var conn = d.redis.Get(c)
  83. defer conn.Close()
  84. if _, err = conn.Do("DEL", filename); err != nil {
  85. log.Error("conn.Do(DEL, %s) error(%v)", filename, err)
  86. }
  87. return
  88. }
  89. // AddArcClick add archive click into redis
  90. func (d *Dao) AddArcClick(c context.Context, aid int64, click int) (err error) {
  91. var conn = d.redis.Get(c)
  92. defer conn.Close()
  93. now := time.Now().Unix() - _twepoch
  94. if _, err = conn.Do("ZADD", _clickSort, now<<26|int64(click), aid); err != nil {
  95. log.Error("conn.Do(ZADD,%s,%d)", _clickSort, aid)
  96. return
  97. }
  98. conn.Do("ZREMRANGEBYSCORE", _clickSort, "-inf", (now-_clickExpire)<<26)
  99. return
  100. }
  101. // ArcClick find archive click from redis
  102. func (d *Dao) ArcClick(c context.Context, aid int64) (click int, err error) {
  103. var (
  104. conn = d.redis.Get(c)
  105. score int64
  106. )
  107. defer conn.Close()
  108. if score, err = redis.Int64(conn.Do("ZSCORE", _clickSort, aid)); err != nil {
  109. if err == redis.ErrNil {
  110. err = nil
  111. } else {
  112. log.Error("conn.Do(ZSCORE, %d) error(%v)", aid, err)
  113. }
  114. return
  115. }
  116. click = int(score & 0x3ffffff) // 0x3ffffff = 26bit
  117. return
  118. }
  119. func monitorKey() string {
  120. return _prefixMonitor
  121. }
  122. // SetMonitorCache set monitor cache
  123. func (d *Dao) SetMonitorCache(c context.Context, aid int64) (had bool, err error) {
  124. var (
  125. key = monitorKey()
  126. conn = d.redis.Get(c)
  127. firstAdd bool
  128. )
  129. defer conn.Close()
  130. if firstAdd, err = redis.Bool(conn.Do("SADD", key, aid)); err != nil {
  131. log.Error("SADD conn.Do error(%v)", err)
  132. had = false
  133. return
  134. }
  135. had = !firstAdd
  136. return
  137. }
  138. // DelMonitorCache del monitor cache
  139. func (d *Dao) DelMonitorCache(c context.Context, aid int64) (err error) {
  140. var (
  141. key = monitorKey()
  142. conn = d.redis.Get(c)
  143. )
  144. defer conn.Close()
  145. if _, err = redis.Int64(conn.Do("SREM", key, aid)); err != nil {
  146. log.Error("SREM conn.Do(%s,%d) err(%v)", key, aid, err)
  147. }
  148. return
  149. }