缓存设置

This commit is contained in:
xing 2022-09-19 17:39:00 +08:00
parent dd4560d4f8
commit 0dfeaa5a7b
4 changed files with 198 additions and 134 deletions

View File

@ -2,13 +2,19 @@ package common
import ( import (
"fmt" "fmt"
"github/fthvgb1/wp-go/cache"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"strings" "strings"
"sync" "sync"
"time"
) )
var PostsCache sync.Map 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) { func GetPostFromCache(Id uint64) (r models.WpPosts) {
p, ok := PostsCache.Load(Id) p, ok := PostsCache.Load(Id)
if ok { if ok {
@ -74,14 +80,21 @@ func QueryAndSetPostCache(postIds []models.WpPosts) (err error) {
return return
} }
func Archives() (r []models.PostArchive, err error) { func archives() ([]models.PostArchive, error) {
r, err = models.Find[models.PostArchive](models.SqlBuilder{ return models.Find[models.PostArchive](models.SqlBuilder{
{"post_type", "post"}, {"post_status", "publish"}, {"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) }, "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"} var in = []interface{}{"category"}
terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{ terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{
{"tt.count", ">", "0", "int"}, {"tt.count", ">", "0", "int"},
@ -102,7 +115,10 @@ func Categories() (terms []models.WpTermsMy, err error) {
return 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{{ r, err = models.Find[models.WpPosts](models.SqlBuilder{{
"post_type", "post", "post_type", "post",
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5)

View File

@ -174,14 +174,14 @@ func Index(c *gin.Context) {
} }
postIds[i] = px postIds[i] = px
} }
recent, err := common.RecentPosts() recent := common.RecentPosts()
for i, post := range recent { for i, post := range recent {
if post.PostPassword != "" && pw != post.PostPassword { if post.PostPassword != "" && pw != post.PostPassword {
common.PasswdProjectContent(&recent[i]) common.PasswdProjectContent(&recent[i])
} }
} }
archive, err := common.Archives() archive := common.Archives()
categoryItems, err := common.Categories() categoryItems := common.Categories()
q := c.Request.URL.Query().Encode() q := c.Request.URL.Query().Encode()
ginH = gin.H{ ginH = gin.H{
"posts": postIds, "posts": postIds,

44
cache/cache.go vendored Normal file
View File

@ -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
}

View File

@ -1,5 +1,6 @@
{{template "layout/base" .}} {{template "layout/base" .}}
{{define "content"}} {{define "content"}}
{{ if .post.PostContent}}
<div id="primary" class="content-area"> <div id="primary" class="content-area">
<main id="main" class="site-main"> <main id="main" class="site-main">
<article id="post-{{.post.Id}}" <article id="post-{{.post.Id}}"
@ -149,4 +150,7 @@
</nav> </nav>
</main><!-- .site-main --> </main><!-- .site-main -->
</div> </div>
{{else}}
{{template "layout/empty"}}
{{end }}
{{end}} {{end}}