Skip to content

Commit

Permalink
#11 取消本地浏览计数
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jan 19, 2020
1 parent f42a688 commit 7555f3a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion controller/articlectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func showArticleAction(c *gin.Context) {

c.HTML(http.StatusOK, getTheme(c)+"/article.html", dataModel)

go service.Article.IncArticleViewCount(articleModel)
service.Article.IncArticleViewCount(articleModel)
}

func fillPreviousArticle(c *gin.Context, article *model.Article, dataModel *DataModel) {
Expand Down
3 changes: 2 additions & 1 deletion controller/blogmidware.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func resolveBlog(c *gin.Context) {
c.Set("userBlog", userBlog)

fillCommon(c)
go service.Statistic.IncViewCount(userBlog.ID)

service.Statistic.IncViewCount(userBlog.ID)

path := strings.Split(c.Request.RequestURI, username)[1]
path = strings.TrimSpace(path)
Expand Down
16 changes: 9 additions & 7 deletions service/articlesrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,15 @@ func (srv *articleService) UpdateArticle(article *model.Article) (err error) {
}

func (srv *articleService) IncArticleViewCount(article *model.Article) error {
srv.mutex.Lock()
defer srv.mutex.Unlock()

article.ViewCount = article.ViewCount + 1
if err := db.Model(&model.Article{}).Where("`id` = ?", article.ID).Select("view_count").Updates(article).Error; nil != err {
return err
}
// 浏览计数插件化 https://github.com/88250/pipe/issues/11

//srv.mutex.Lock()
//defer srv.mutex.Unlock()
//
//article.ViewCount = article.ViewCount + 1
//if err := db.Model(&model.Article{}).Where("`id` = ?", article.ID).Select("view_count").Updates(article).Error; nil != err {
// return err
//}

return nil
}
Expand Down
16 changes: 9 additions & 7 deletions service/statisticsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ func (srv *statisticService) GetStatistics(blogID uint, statisticNames ...string
}

func (srv *statisticService) IncViewCount(blogID uint64) error {
tx := db.Begin()
if err := srv.IncViewCountWithoutTx(tx, blogID); nil != err {
tx.Rollback()

return err
}
tx.Commit()
// 浏览计数插件化 https://github.com/88250/pipe/issues/11

//tx := db.Begin()
//if err := srv.IncViewCountWithoutTx(tx, blogID); nil != err {
// tx.Rollback()
//
// return err
//}
//tx.Commit()

return nil
}
Expand Down

0 comments on commit 7555f3a

Please sign in to comment.