From f8160981f3c2630ac934cb65c6352c7a572d8e78 Mon Sep 17 00:00:00 2001 From: LawyZHENG Date: Sun, 25 Apr 2021 16:05:05 +0800 Subject: [PATCH] fix(document): fail to count times when cache is enabled. --- cache/cache.go | 2 +- controllers/DocumentController.go | 16 +++++++++------- models/DocumentModel.go | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index dadc485b..0e6f5402 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -7,8 +7,8 @@ import ( "errors" "time" - "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/client/cache" + "github.com/beego/beego/v2/core/logs" ) var bm cache.Cache diff --git a/controllers/DocumentController.go b/controllers/DocumentController.go index d4f4c65f..9633745a 100644 --- a/controllers/DocumentController.go +++ b/controllers/DocumentController.go @@ -145,21 +145,22 @@ func (c *DocumentController) Read() { } doc.IncrViewCount(doc.DocumentId) - c.Data["ViewCount"] = doc.ViewCount + 1 + doc.ViewCount = doc.ViewCount + 1 + doc.PutToCache() if c.IsAjax() { var data struct { - DocTitle string `json:"doc_title"` - Body string `json:"body"` - Title string `json:"title"` - Version int64 `json:"version"` - ViewCount int `json:"view_count"` + DocTitle string `json:"doc_title"` + Body string `json:"body"` + Title string `json:"title"` + Version int64 `json:"version"` + ViewCount int `json:"view_count"` } data.DocTitle = doc.DocumentName data.Body = doc.Release data.Title = doc.DocumentName + " - Powered by MinDoc" data.Version = doc.Version - data.ViewCount = doc.ViewCount + 1 + data.ViewCount = doc.ViewCount c.JsonResult(0, "ok", data) } @@ -178,6 +179,7 @@ func (c *DocumentController) Read() { c.Data["Result"] = template.HTML(tree) c.Data["Title"] = doc.DocumentName c.Data["Content"] = template.HTML(doc.Release) + c.Data["ViewCount"] = doc.ViewCount } // 编辑文档 diff --git a/models/DocumentModel.go b/models/DocumentModel.go index 4faac2c6..d05e15c8 100644 --- a/models/DocumentModel.go +++ b/models/DocumentModel.go @@ -335,7 +335,7 @@ func (item *Document) Processor() *Document { selector.First().AppendHtml(release) } } - cdnimg,_ := web.AppConfig.String("cdnimg") + cdnimg, _ := web.AppConfig.String("cdnimg") docQuery.Find("img").Each(func(i int, selection *goquery.Selection) {