This commit is contained in:
xing 2022-09-19 19:11:36 +08:00
parent 0dfeaa5a7b
commit f85cace016
5 changed files with 89 additions and 42 deletions

View File

@ -4,16 +4,68 @@ import (
"fmt" "fmt"
"github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/cache"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/vars"
"strings" "strings"
"sync" "sync"
"time" "time"
) )
var PostsCache sync.Map var PostsCache sync.Map
var PostContextCache sync.Map
var archivesCaches = cache.NewSliceCache[models.PostArchive](archives, time.Hour*24) var archivesCaches *cache.SliceCache[models.PostArchive]
var categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, time.Minute*30) var categoryCaches *cache.SliceCache[models.WpTermsMy]
var recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, time.Minute*30) 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) { func GetPostFromCache(Id uint64) (r models.WpPosts) {
p, ok := PostsCache.Load(Id) p, ok := PostsCache.Load(Id)

View File

@ -11,16 +11,24 @@ import (
) )
func Detail(c *gin.Context) { func Detail(c *gin.Context) {
id := c.Param("id")
var h = gin.H{}
var err error 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() { defer func() {
c.HTML(http.StatusOK, "posts/detail.gohtml", h) c.HTML(http.StatusOK, "posts/detail.gohtml", h)
if err != nil { if err != nil {
c.Error(err) c.Error(err)
} }
}() }()
id := c.Param("id")
Id := 0 Id := 0
if id != "" { if id != "" {
Id, err = strconv.Atoi(id) Id, err = strconv.Atoi(id)
@ -48,43 +56,16 @@ func Detail(c *gin.Context) {
common.PasswdProjectContent(&post) common.PasswdProjectContent(&post)
showComment = false showComment = false
} }
recent, err := common.RecentPosts()
archive, err := common.Archives()
categoryItems, err := common.Categories()
canComment := false canComment := false
if post.CommentStatus == "open" { if post.CommentStatus == "open" {
canComment = true canComment = true
} }
prev, err := models.FirstOne[models.WpPosts](models.SqlBuilder{ prev, next, err := common.GetContextPost(post.Id, post.PostDate)
{"post_date", "<", post.PostDate.Format("2006-01-02 15:04:05")},
{"post_status", "publish"}, h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
{"post_type", "post"}, h["post"] = post
}, "ID,post_title") h["comment"] = showComment
if prev.Id > 0 { h["canComment"] = canComment
if _, ok := common.PostsCache.Load(prev.Id); !ok { h["prev"] = prev
common.QueryAndSetPostCache([]models.WpPosts{prev}) h["next"] = next
}
}
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,
}
} }

View File

@ -15,3 +15,11 @@ mysql:
maxIdleConn: 10 maxIdleConn: 10
# 连接的生命时长 # 连接的生命时长
connMaxLifetime: 236 connMaxLifetime: 236
# 最近文章缓存时间
recentPostCacheTime: 30m
# 分类缓存时间
categoryCacheTime: 30m
# 归档缓存时间
archiveCacheTime: 24h
# 上下篇缓存时间
contextPostCacheTime: 1h

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"github/fthvgb1/wp-go/actions/common"
"github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/db"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"github/fthvgb1/wp-go/route" "github/fthvgb1/wp-go/route"
@ -12,6 +13,7 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
common.InitCache()
err = db.InitDb() err = db.InitDb()
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -11,6 +11,10 @@ var Conf Config
type Config struct { 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 { type Mysql struct {