newlist_redis.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/service/main/archive/api"
  7. "go-common/library/cache/redis"
  8. "go-common/library/log"
  9. "go-common/library/time"
  10. )
  11. const (
  12. // new list
  13. _keyNl = "n_%d_%d"
  14. )
  15. func keyNl(rid int32, tp int8) string {
  16. return fmt.Sprintf(_keyNl, rid, tp)
  17. }
  18. func keyNlBak(rid int32, tp int8) string {
  19. return _keyBakPrefix + keyNl(rid, tp)
  20. }
  21. // NewListCache get region rank list from cache.
  22. func (d *Dao) NewListCache(c context.Context, rid int32, tp int8, start, end int) (arcs []*api.Arc, count int, err error) {
  23. key := keyNl(rid, tp)
  24. conn := d.redis.Get(c)
  25. defer conn.Close()
  26. arcs, count, err = d.nlCache(c, conn, key, start, end)
  27. return
  28. }
  29. // NewListBakCache get region rank list from bak cache.
  30. func (d *Dao) NewListBakCache(c context.Context, rid int32, tp int8, start, end int) (arcs []*api.Arc, count int, err error) {
  31. d.cacheProm.Incr("newlist_remote_cache")
  32. key := keyNlBak(rid, tp)
  33. conn := d.redisBak.Get(c)
  34. defer conn.Close()
  35. arcs, count, err = d.nlCache(c, conn, key, start, end)
  36. if len(arcs) == 0 {
  37. log.Error("NewlistBakCache(%s,%d,%d) is nil", key, start, end)
  38. }
  39. return
  40. }
  41. func (d *Dao) nlCache(c context.Context, conn redis.Conn, key string, start, end int) (arcs []*api.Arc, count int, err error) {
  42. values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
  43. if err != nil {
  44. log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
  45. return
  46. }
  47. if len(values) == 0 {
  48. return
  49. }
  50. var num int64
  51. for len(values) > 0 {
  52. bs := []byte{}
  53. if values, err = redis.Scan(values, &bs, &num); err != nil {
  54. log.Error("redis.Scan(%v) error(%v)", values, err)
  55. return
  56. }
  57. arc := &api.Arc{}
  58. if err = json.Unmarshal(bs, arc); err != nil {
  59. log.Error("json.Unmarshal(%v) error(%v)", bs, err)
  60. return
  61. }
  62. arcs = append(arcs, arc)
  63. }
  64. count = from(num)
  65. return
  66. }
  67. // SetNewListCache set region cache.
  68. func (d *Dao) SetNewListCache(c context.Context, rid int32, tp int8, arcs []*api.Arc, count int) (err error) {
  69. key := keyNl(rid, tp)
  70. conn := d.redis.Get(c)
  71. defer conn.Close()
  72. if err = d.setNlCache(c, conn, key, d.redisNlExpire, arcs, count); err != nil {
  73. return
  74. }
  75. key = keyNlBak(rid, tp)
  76. connBak := d.redisBak.Get(c)
  77. err = d.setNlCache(c, connBak, key, d.redisNlBakExpire, arcs, count)
  78. connBak.Close()
  79. return
  80. }
  81. func (d *Dao) setNlCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*api.Arc, num int) (err error) {
  82. count := 0
  83. if err = conn.Send("DEL", key); err != nil {
  84. log.Error("conn.Send(DEL, %s) error(%v)", key, err)
  85. return
  86. }
  87. count++
  88. for _, arc := range arcs {
  89. bs, _ := json.Marshal(arc)
  90. if err = conn.Send("ZADD", key, combine(arc.PubDate, num), bs); err != nil {
  91. log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
  92. return
  93. }
  94. count++
  95. }
  96. if err = conn.Send("EXPIRE", key, expire); err != nil {
  97. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  98. return
  99. }
  100. count++
  101. if err = conn.Flush(); err != nil {
  102. log.Error("conn.Flush error(%v)", err)
  103. return
  104. }
  105. for i := 0; i < count; i++ {
  106. if _, err = conn.Receive(); err != nil {
  107. log.Error("conn.Receive() error(%v)", err)
  108. return
  109. }
  110. }
  111. return
  112. }
  113. func from(i int64) int {
  114. return int(i & 0xffffff)
  115. }
  116. func combine(pubdate time.Time, count int) int64 {
  117. return pubdate.Time().Unix()<<24 | int64(count)
  118. }