wechat.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "go-common/app/interface/main/web/model"
  7. "go-common/library/cache/redis"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. const (
  13. _wxHotURI = "hot-weixin.json"
  14. _wxCacheKey = "wx_hot"
  15. _wxBkCacheKey = _keyBakPrefix + _wxCacheKey
  16. )
  17. // WxHot get wx hot aids.
  18. func (d *Dao) WxHot(c context.Context) (aids []int64, err error) {
  19. ip := metadata.String(c, metadata.RemoteIP)
  20. var res struct {
  21. Code int `json:"code"`
  22. List []*model.NewArchive `json:"list"`
  23. }
  24. if err = d.httpBigData.Get(c, d.wxHotURL, ip, url.Values{}, &res); err != nil {
  25. log.Error("d.httpBigData.Get(%s) error(%v)", d.wxHotURL, err)
  26. return
  27. }
  28. if res.Code != ecode.OK.Code() {
  29. log.Error("d.httpBigData.Get(%s) error(%v)", d.wxHotURL, err)
  30. err = ecode.Int(res.Code)
  31. return
  32. }
  33. for _, v := range res.List {
  34. if v.Aid > 0 {
  35. aids = append(aids, v.Aid)
  36. }
  37. }
  38. return
  39. }
  40. // WxHotCache get wx hot cache.
  41. func (d *Dao) WxHotCache(c context.Context) (arcs []*model.WxArchive, err error) {
  42. key := _wxCacheKey
  43. conn := d.redis.Get(c)
  44. defer conn.Close()
  45. arcs, err = wxHotCache(conn, key)
  46. return
  47. }
  48. // WxHotBakCache get wx hot bak cache.
  49. func (d *Dao) WxHotBakCache(c context.Context) (arcs []*model.WxArchive, err error) {
  50. key := _wxBkCacheKey
  51. conn := d.redisBak.Get(c)
  52. defer conn.Close()
  53. arcs, err = wxHotCache(conn, key)
  54. return
  55. }
  56. func wxHotCache(conn redis.Conn, key string) (res []*model.WxArchive, err error) {
  57. var value []byte
  58. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  59. if err == redis.ErrNil {
  60. err = nil
  61. } else {
  62. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  63. }
  64. return
  65. }
  66. res = []*model.WxArchive{}
  67. if err = json.Unmarshal(value, &res); err != nil {
  68. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  69. }
  70. return
  71. }
  72. // SetWxHotCache set wx hot to cache.
  73. func (d *Dao) SetWxHotCache(c context.Context, arcs []*model.WxArchive) (err error) {
  74. key := _wxCacheKey
  75. conn := d.redis.Get(c)
  76. defer conn.Close()
  77. if err = d.setWxHotCache(c, conn, key, d.redisWxHotExpire, arcs); err != nil {
  78. return
  79. }
  80. key = _wxBkCacheKey
  81. connBak := d.redisBak.Get(c)
  82. err = d.setWxHotCache(c, connBak, key, d.redisWxHotBakExpire, arcs)
  83. connBak.Close()
  84. return
  85. }
  86. func (d *Dao) setWxHotCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.WxArchive) (err error) {
  87. var bs []byte
  88. if bs, err = json.Marshal(arcs); err != nil {
  89. log.Error("json.Marshal(%v) error (%v)", arcs, err)
  90. return
  91. }
  92. if err = conn.Send("SET", key, bs); err != nil {
  93. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  94. return
  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. if err = conn.Flush(); err != nil {
  101. log.Error("conn.Flush error(%v)", err)
  102. return
  103. }
  104. for i := 0; i < 2; i++ {
  105. if _, err = conn.Receive(); err != nil {
  106. log.Error("conn.Receive() error(%v)", err)
  107. return
  108. }
  109. }
  110. return
  111. }