缓存
This commit is contained in:
parent
0dfeaa5a7b
commit
f85cace016
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -15,3 +15,11 @@ mysql:
|
|||
maxIdleConn: 10
|
||||
# 连接的生命时长
|
||||
connMaxLifetime: 236
|
||||
# 最近文章缓存时间
|
||||
recentPostCacheTime: 30m
|
||||
# 分类缓存时间
|
||||
categoryCacheTime: 30m
|
||||
# 归档缓存时间
|
||||
archiveCacheTime: 24h
|
||||
# 上下篇缓存时间
|
||||
contextPostCacheTime: 1h
|
||||
|
|
2
main.go
2
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)
|
||||
|
|
|
@ -11,6 +11,10 @@ var Conf Config
|
|||
|
||||
type Config struct {
|
||||
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 {
|
Loading…
Reference in New Issue
Block a user