article_redis.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/interface/main/web/model"
  7. artmdl "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/cache/redis"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _keyArtList = "art_%d_%d"
  13. _keyArtUp = "art_u"
  14. )
  15. func keyArtList(rid int64, sort int) string {
  16. return fmt.Sprintf(_keyArtList, rid, sort)
  17. }
  18. // ArticleListCache get article list cache
  19. func (d *Dao) ArticleListCache(c context.Context, rid int64, sort int) (res []*artmdl.Meta, err error) {
  20. var (
  21. value []byte
  22. key = keyArtList(rid, sort)
  23. conn = d.redis.Get(c)
  24. )
  25. defer conn.Close()
  26. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  27. if err == redis.ErrNil {
  28. err = nil
  29. } else {
  30. log.Error("conn.Do(GET, %s) error(%v)", key, err)
  31. }
  32. return
  33. }
  34. res = []*artmdl.Meta{}
  35. if err = json.Unmarshal(value, &res); err != nil {
  36. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  37. }
  38. return
  39. }
  40. // SetArticleListCache set article list cache
  41. func (d *Dao) SetArticleListCache(c context.Context, rid int64, sort int, list []*artmdl.Meta) (err error) {
  42. var (
  43. bs []byte
  44. key = keyArtList(rid, sort)
  45. conn = d.redis.Get(c)
  46. )
  47. defer conn.Close()
  48. if bs, err = json.Marshal(list); err != nil {
  49. log.Error("json.Marshal(%v) error (%v)", list, err)
  50. return
  51. }
  52. if err = conn.Send("SET", key, bs); err != nil {
  53. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  54. return
  55. }
  56. if err = conn.Send("EXPIRE", key, d.redisArtBakExpire); err != nil {
  57. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisArtBakExpire, err)
  58. return
  59. }
  60. if err = conn.Flush(); err != nil {
  61. log.Error("conn.Flush error(%v)", err)
  62. return
  63. }
  64. for i := 0; i < 2; i++ {
  65. if _, err = conn.Receive(); err != nil {
  66. log.Error("conn.Receive() error(%v)", err)
  67. return
  68. }
  69. }
  70. return
  71. }
  72. // ArticleUpListCache get article up list cache.
  73. func (d *Dao) ArticleUpListCache(c context.Context) (res []*model.Info, err error) {
  74. var (
  75. value []byte
  76. conn = d.redis.Get(c)
  77. )
  78. defer conn.Close()
  79. if value, err = redis.Bytes(conn.Do("GET", _keyArtUp)); err != nil {
  80. if err == redis.ErrNil {
  81. err = nil
  82. } else {
  83. log.Error("conn.Do(GET, %s) error(%v)", _keyArtUp, err)
  84. }
  85. return
  86. }
  87. res = []*model.Info{}
  88. if err = json.Unmarshal(value, &res); err != nil {
  89. log.Error("json.Unmarshal(%v) error(%v)", value, err)
  90. }
  91. return
  92. }
  93. // SetArticleUpListCache set article up list cache.
  94. func (d *Dao) SetArticleUpListCache(c context.Context, list []*model.Info) (err error) {
  95. var (
  96. bs []byte
  97. key = _keyArtUp
  98. conn = d.redis.Get(c)
  99. )
  100. defer conn.Close()
  101. if bs, err = json.Marshal(list); err != nil {
  102. log.Error("json.Marshal(%v) error (%v)", list, err)
  103. return
  104. }
  105. if err = conn.Send("SET", key, bs); err != nil {
  106. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  107. return
  108. }
  109. if err = conn.Send("EXPIRE", key, d.redisArtBakExpire); err != nil {
  110. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisArtBakExpire, err)
  111. return
  112. }
  113. if err = conn.Flush(); err != nil {
  114. log.Error("conn.Flush error(%v)", err)
  115. return
  116. }
  117. for i := 0; i < 2; i++ {
  118. if _, err = conn.Receive(); err != nil {
  119. log.Error("conn.Receive() error(%v)", err)
  120. return
  121. }
  122. }
  123. return
  124. }