From f85cace0165eb712e772a00ec226ff376a6a0666 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 19 Sep 2022 19:11:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 58 +++++++++++++++++++++++++++++++-- actions/detail.go | 57 +++++++++++--------------------- config.example.yaml | 8 +++++ main.go | 2 ++ vars/{dbconfig.go => config.go} | 6 +++- 5 files changed, 89 insertions(+), 42 deletions(-) rename vars/{dbconfig.go => config.go} (77%) diff --git a/actions/common/common.go b/actions/common/common.go index c1073c3..6d52bab 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -4,16 +4,68 @@ import ( "fmt" "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" + "github/fthvgb1/wp-go/vars" "strings" "sync" "time" ) var PostsCache sync.Map +var PostContextCache 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) +var archivesCaches *cache.SliceCache[models.PostArchive] +var categoryCaches *cache.SliceCache[models.WpTermsMy] +var recentPostsCaches *cache.SliceCache[models.WpPosts] + +func InitCache() { + archivesCaches = cache.NewSliceCache[models.PostArchive](archives, vars.Conf.ArchiveCacheTime) + categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) + recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) +} + +type PostContext struct { + Prev models.WpPosts + Next models.WpPosts + expireTime time.Duration + setTime time.Time +} + +func GetContextPost(id uint64, t time.Time) (prev, next models.WpPosts, err error) { + post, ok := PostContextCache.Load(id) + if ok { + c := post.(PostContext) + isExp := c.expireTime/time.Second+time.Duration(c.setTime.Unix()) < time.Duration(time.Now().Unix()) + if !isExp && (c.Prev.Id > 0 || c.Next.Id > 0) { + return c.Prev, c.Next, nil + } + } + prev, next, err = getPostContext(t) + post = PostContext{ + Prev: prev, + Next: next, + expireTime: vars.Conf.ContextPostCacheTime, + setTime: time.Now(), + } + PostContextCache.Store(id, post) + return +} + +func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { + next, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ + {"post_date", ">", t.Format("2006-01-02 15:04:05")}, + {"post_status", "publish"}, + {"post_type", "post"}, + }, "ID,post_title,post_password") + if _, ok := PostsCache.Load(next.Id); !ok { + + } + prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{ + {"post_date", "<", t.Format("2006-01-02 15:04:05")}, + {"post_status", "publish"}, + {"post_type", "post"}, + }, "ID,post_title") + return +} func GetPostFromCache(Id uint64) (r models.WpPosts) { p, ok := PostsCache.Load(Id) diff --git a/actions/detail.go b/actions/detail.go index 2fc7950..3477961 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -11,16 +11,24 @@ import ( ) func Detail(c *gin.Context) { - id := c.Param("id") - var h = gin.H{} var err error + recent := common.RecentPosts() + archive := common.Archives() + categoryItems := common.Categories() + var h = gin.H{ + "title": models.Options["blogname"], + "options": models.Options, + "recentPosts": recent, + "archives": archive, + "categories": categoryItems, + } defer func() { c.HTML(http.StatusOK, "posts/detail.gohtml", h) if err != nil { c.Error(err) } }() - + id := c.Param("id") Id := 0 if id != "" { Id, err = strconv.Atoi(id) @@ -48,43 +56,16 @@ func Detail(c *gin.Context) { common.PasswdProjectContent(&post) showComment = false } - recent, err := common.RecentPosts() - archive, err := common.Archives() - categoryItems, err := common.Categories() canComment := false if post.CommentStatus == "open" { canComment = true } - prev, err := models.FirstOne[models.WpPosts](models.SqlBuilder{ - {"post_date", "<", post.PostDate.Format("2006-01-02 15:04:05")}, - {"post_status", "publish"}, - {"post_type", "post"}, - }, "ID,post_title") - if prev.Id > 0 { - if _, ok := common.PostsCache.Load(prev.Id); !ok { - common.QueryAndSetPostCache([]models.WpPosts{prev}) - } - } - next, err := models.FirstOne[models.WpPosts](models.SqlBuilder{ - {"post_date", ">", post.PostDate.Format("2006-01-02 15:04:05")}, - {"post_status", "publish"}, - {"post_type", "post"}, - }, "ID,post_title,post_password") - if prev.Id > 0 { - if _, ok := common.PostsCache.Load(next.Id); !ok { - common.QueryAndSetPostCache([]models.WpPosts{next}) - } - } - h = gin.H{ - "title": fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]), - "post": post, - "options": models.Options, - "recentPosts": recent, - "archives": archive, - "categories": categoryItems, - "comment": showComment, - "canComment": canComment, - "prev": prev, - "next": next, - } + prev, next, err := common.GetContextPost(post.Id, post.PostDate) + + h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]) + h["post"] = post + h["comment"] = showComment + h["canComment"] = canComment + h["prev"] = prev + h["next"] = next } diff --git a/config.example.yaml b/config.example.yaml index 4a52f4b..e25bfbc 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -15,3 +15,11 @@ mysql: maxIdleConn: 10 # 连接的生命时长 connMaxLifetime: 236 +# 最近文章缓存时间 +recentPostCacheTime: 30m +# 分类缓存时间 +categoryCacheTime: 30m +# 归档缓存时间 +archiveCacheTime: 24h +# 上下篇缓存时间 +contextPostCacheTime: 1h diff --git a/main.go b/main.go index e93316d..f428150 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/route" @@ -12,6 +13,7 @@ func init() { if err != nil { panic(err) } + common.InitCache() err = db.InitDb() if err != nil { panic(err) diff --git a/vars/dbconfig.go b/vars/config.go similarity index 77% rename from vars/dbconfig.go rename to vars/config.go index 6b71ca4..5200401 100644 --- a/vars/dbconfig.go +++ b/vars/config.go @@ -10,7 +10,11 @@ import ( var Conf Config type Config struct { - Mysql Mysql `yaml:"mysql"` + Mysql Mysql `yaml:"mysql"` + RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` + CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` + ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` + ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` } type Mysql struct {