缓存设置
This commit is contained in:
parent
dd4560d4f8
commit
0dfeaa5a7b
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
44
cache/cache.go
vendored
Normal file
44
cache/cache.go
vendored
Normal 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
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
{{template "layout/base" .}}
|
||||
{{define "content"}}
|
||||
{{ if .post.PostContent}}
|
||||
<div id="primary" class="content-area">
|
||||
<main id="main" class="site-main">
|
||||
<article id="post-{{.post.Id}}"
|
||||
|
@ -149,4 +150,7 @@
|
|||
</nav>
|
||||
</main><!-- .site-main -->
|
||||
</div>
|
||||
{{else}}
|
||||
{{template "layout/empty"}}
|
||||
{{end }}
|
||||
{{end}}
|
||||
|
|
Loading…
Reference in New Issue
Block a user