sort.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/openplatform/article/dao"
  6. artmdl "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. // UpdateSortCache update sort cache
  11. func (s *Service) UpdateSortCache(c context.Context, aid int64, changed [][2]int64, ip string) (err error) {
  12. var meta *artmdl.Meta
  13. if meta, err = s.ArticleMeta(c, aid); (err != nil) || (meta == nil) {
  14. return
  15. }
  16. cids := []int64{meta.Category.ID, meta.Category.ParentID}
  17. var exist bool
  18. for _, cid := range cids {
  19. for _, ch := range changed {
  20. field := int(ch[0])
  21. value := ch[1]
  22. if exist, err = s.dao.ExpireSortCache(c, cid, field); err != nil {
  23. dao.PromError("sort:更新文章排序缓存")
  24. return
  25. }
  26. if exist && s.shouldAddSort(meta.PublishTime, field) {
  27. if err = s.dao.AddSortCache(c, cid, field, aid, value); err != nil {
  28. return
  29. }
  30. }
  31. }
  32. }
  33. return
  34. }
  35. func (s *Service) shouldAddSort(t xtime.Time, field int) bool {
  36. if (field != artmdl.FieldLike) && (field != artmdl.FieldReply) && (field != artmdl.FieldFav) && (field != artmdl.FieldView) {
  37. return true
  38. }
  39. limitTime := time.Now().Unix() - s.sortLimitTime
  40. return int64(t) > limitTime
  41. }
  42. func (s *Service) delArtSortCache(c context.Context, aid int64) (err error) {
  43. var (
  44. root, cid int64
  45. )
  46. if root, cid, err = s.RootCategory(c, aid); err != nil {
  47. dao.PromError("sort:删除文章缓存查找分类")
  48. log.Error("s.RootCategory(%d,%d) error(%+v)", aid, cid, err)
  49. return
  50. }
  51. cids := []int64{root, cid, _recommendCategory}
  52. err = s.delArtSortCacheFromCid(c, aid, cids...)
  53. return
  54. }
  55. func (s *Service) delArtSortCacheFromCid(c context.Context, aid int64, cids ...int64) (err error) {
  56. for _, cid := range cids {
  57. for _, field := range artmdl.SortFields {
  58. if err = s.dao.DelSortCache(c, cid, field, aid); err != nil {
  59. return
  60. }
  61. }
  62. }
  63. return
  64. }
  65. func (s *Service) addArtSortCache(c context.Context, art *artmdl.Meta) (err error) {
  66. if art == nil {
  67. return
  68. }
  69. cids := []int64{art.Category.ID, _recommendCategory}
  70. var oldRoot int64
  71. if oldRoot, err = s.CategoryToRoot(art.Category.ID); err != nil {
  72. dao.PromError("sort:增加文章缓存查找分类")
  73. log.Error("s.CategoryToRoot(%d,%d) error(%+v)", art.ID, art.Category.ID, err)
  74. err = nil
  75. } else {
  76. cids = append(cids, oldRoot)
  77. }
  78. if art.Stats == nil {
  79. if stat, e := s.stat(c, art.ID); (e == nil) && (stat != nil) {
  80. art.Stats = stat
  81. } else {
  82. art.Stats = new(artmdl.Stats)
  83. }
  84. }
  85. var exist bool
  86. for _, cid := range cids {
  87. for _, field := range artmdl.SortFields {
  88. if exist, err = s.dao.ExpireSortCache(c, cid, field); err != nil {
  89. dao.PromError("sort:增加最新文章")
  90. return
  91. }
  92. if !exist || !s.shouldAddSort(art.PublishTime, field) {
  93. continue
  94. }
  95. var value int64
  96. switch field {
  97. case artmdl.FieldNew:
  98. value = int64(art.PublishTime)
  99. case artmdl.FieldLike:
  100. value = art.Stats.Like
  101. case artmdl.FieldReply:
  102. value = art.Stats.Reply
  103. case artmdl.FieldFav:
  104. value = art.Stats.Favorite
  105. case artmdl.FieldView:
  106. value = art.Stats.View
  107. default:
  108. dao.PromError("sort:新增最新文章-排序分类错误")
  109. log.Error("addArtSortCache sort field error: %v", field)
  110. }
  111. if err = s.dao.AddSortCache(c, cid, field, art.ID, value); err != nil {
  112. dao.PromError("sort:新增最新文章")
  113. }
  114. }
  115. }
  116. return
  117. }