From 0dfeaa5a7b3f3b35e65b9af6e9e11da1aa5d13ec Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 19 Sep 2022 17:39:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 26 +++- actions/index.go | 6 +- cache/cache.go | 44 ++++++ templates/posts/detail.gohtml | 256 +++++++++++++++++----------------- 4 files changed, 198 insertions(+), 134 deletions(-) create mode 100644 cache/cache.go diff --git a/actions/common/common.go b/actions/common/common.go index 7cf012c..c1073c3 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -2,13 +2,19 @@ package common import ( "fmt" + "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" "strings" "sync" + "time" ) var PostsCache sync.Map +var archivesCaches = cache.NewSliceCache[models.PostArchive](archives, time.Hour*24) +var categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, time.Minute*30) +var recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, time.Minute*30) + func GetPostFromCache(Id uint64) (r models.WpPosts) { p, ok := PostsCache.Load(Id) if ok { @@ -74,14 +80,21 @@ func QueryAndSetPostCache(postIds []models.WpPosts) (err error) { return } -func Archives() (r []models.PostArchive, err error) { - r, err = models.Find[models.PostArchive](models.SqlBuilder{ +func archives() ([]models.PostArchive, error) { + return models.Find[models.PostArchive](models.SqlBuilder{ {"post_type", "post"}, {"post_status", "publish"}, }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0) - return } -func Categories() (terms []models.WpTermsMy, err error) { +func Archives() (r []models.PostArchive) { + return archivesCaches.GetCache() +} + +func Categories() []models.WpTermsMy { + return categoryCaches.GetCache() +} + +func categories() (terms []models.WpTermsMy, err error) { var in = []interface{}{"category"} terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{ {"tt.count", ">", "0", "int"}, @@ -102,7 +115,10 @@ func Categories() (terms []models.WpTermsMy, err error) { return } -func RecentPosts() (r []models.WpPosts, err error) { +func RecentPosts() (r []models.WpPosts) { + return recentPostsCaches.GetCache() +} +func recentPosts() (r []models.WpPosts, err error) { r, err = models.Find[models.WpPosts](models.SqlBuilder{{ "post_type", "post", }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) diff --git a/actions/index.go b/actions/index.go index f099180..c194ee6 100644 --- a/actions/index.go +++ b/actions/index.go @@ -174,14 +174,14 @@ func Index(c *gin.Context) { } postIds[i] = px } - recent, err := common.RecentPosts() + recent := common.RecentPosts() for i, post := range recent { if post.PostPassword != "" && pw != post.PostPassword { common.PasswdProjectContent(&recent[i]) } } - archive, err := common.Archives() - categoryItems, err := common.Categories() + archive := common.Archives() + categoryItems := common.Categories() q := c.Request.URL.Query().Encode() ginH = gin.H{ "posts": postIds, diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..0ff85e6 --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,44 @@ +package cache + +import ( + "log" + "sync" + "time" +) + +type SliceCache[T any] struct { + data []T + mutex *sync.Mutex + setCacheFunc func() ([]T, error) + expireTime time.Duration + setTime time.Time +} + +func NewSliceCache[T any](fun func() ([]T, error), duration time.Duration) *SliceCache[T] { + return &SliceCache[T]{ + mutex: &sync.Mutex{}, + setCacheFunc: fun, + expireTime: duration, + } +} + +func (c *SliceCache[T]) FlushCache() { + c.mutex.Lock() + defer c.mutex.Unlock() + c.data = nil +} + +func (c *SliceCache[T]) GetCache() []T { + l := len(c.data) + expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) + if l > 0 && expired || l < 1 { + r, err := c.setCacheFunc() + if err != nil { + log.Printf("set cache err[%s]", err) + return nil + } + c.setTime = time.Now() + c.data = r + } + return c.data +} diff --git a/templates/posts/detail.gohtml b/templates/posts/detail.gohtml index a5df592..9d9f129 100644 --- a/templates/posts/detail.gohtml +++ b/templates/posts/detail.gohtml @@ -1,18 +1,19 @@ {{template "layout/base" .}} {{define "content"}} -
-
-
+ {{ if .post.PostContent}} +
+
+
-
-

{{.post.PostTitle}}

+
+

{{.post.PostTitle}}

-
- {{.post.PostContent|unescaped}} -
+
+ {{.post.PostContent|unescaped}} +
-
+
发布于 @@ -21,132 +22,135 @@ - {{if .post.CategoriesHtml}} - + {{if .post.CategoriesHtml}} + 分类 {{.post.CategoriesHtml|unescaped}} - {{end}} + {{end}} - {{if .post.TagsHtml}} - + {{if .post.TagsHtml}} + 标签 {{.post.TagsHtml|unescaped}} + {{end}} +
+ + +
+ +
+ +

+ 《{{.post.PostTitle}}》上有1条评论

+ + +
    +
  1. + +
  2. +
+ + {{if .canComment}} +
+

发表回复 + + + +

+
+

+ 您的电子邮箱地址不会被公开。 + +

+

+ +

+

+ +

+ +

+

+ +

+ + + +

+
+
{{end}} - - +
-
+
-
+ + + + + {{else}} + {{template "layout/empty"}} + {{end }} {{end}}