完善
This commit is contained in:
parent
927d2d6f9b
commit
a3c08edb71
|
@ -2,6 +2,7 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github/fthvgb1/wp-go/cache"
|
"github/fthvgb1/wp-go/cache"
|
||||||
"github/fthvgb1/wp-go/models"
|
"github/fthvgb1/wp-go/models"
|
||||||
|
@ -18,6 +19,7 @@ var PostContextCache sync.Map
|
||||||
var archivesCaches *Arch
|
var archivesCaches *Arch
|
||||||
var categoryCaches *cache.SliceCache[models.WpTermsMy]
|
var categoryCaches *cache.SliceCache[models.WpTermsMy]
|
||||||
var recentPostsCaches *cache.SliceCache[models.WpPosts]
|
var recentPostsCaches *cache.SliceCache[models.WpPosts]
|
||||||
|
var monthCaches *cache.MapCache[string, []models.WpPosts]
|
||||||
|
|
||||||
func InitCache() {
|
func InitCache() {
|
||||||
archivesCaches = &Arch{
|
archivesCaches = &Arch{
|
||||||
|
@ -26,6 +28,7 @@ func InitCache() {
|
||||||
}
|
}
|
||||||
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime)
|
||||||
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime)
|
||||||
|
monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Arch struct {
|
type Arch struct {
|
||||||
|
@ -59,6 +62,25 @@ type PostContext struct {
|
||||||
setTime time.Time
|
setTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMonthPost(ctx context.Context, year, month string) ([]models.WpPosts, error) {
|
||||||
|
return monthCaches.GetCache(ctx, fmt.Sprintf("%s%s", year, month), time.Second, year, month)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMonthPost(args ...any) ([]models.WpPosts, error) {
|
||||||
|
y := args[0].(string)
|
||||||
|
m := args[1].(string)
|
||||||
|
where := models.SqlBuilder{
|
||||||
|
{"post_type", "in", ""},
|
||||||
|
{"post_status", "in", ""},
|
||||||
|
{"month(post_date)", m},
|
||||||
|
{"year(post_date)", y},
|
||||||
|
}
|
||||||
|
return models.Find[models.WpPosts](where, "ID", "",
|
||||||
|
models.SqlBuilder{{"post_date", "asc"}},
|
||||||
|
nil, 0, []interface{}{"post"}, []interface{}{"publish"},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func GetContextPost(id uint64, t time.Time) (prev, next models.WpPosts, err error) {
|
func GetContextPost(id uint64, t time.Time) (prev, next models.WpPosts, err error) {
|
||||||
post, ok := PostContextCache.Load(id)
|
post, ok := PostContextCache.Load(id)
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -85,14 +107,14 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) {
|
||||||
{"post_status", "in", ""},
|
{"post_status", "in", ""},
|
||||||
{"post_type", "post"},
|
{"post_type", "post"},
|
||||||
}, "ID,post_title,post_password", nil, []interface{}{"publish", "private"})
|
}, "ID,post_title,post_password", nil, []interface{}{"publish", "private"})
|
||||||
if _, ok := PostsCache.Load(next.Id); !ok {
|
|
||||||
|
|
||||||
}
|
|
||||||
prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{
|
prev, err = models.FirstOne[models.WpPosts](models.SqlBuilder{
|
||||||
{"post_date", "<", t.Format("2006-01-02 15:04:05")},
|
{"post_date", "<", t.Format("2006-01-02 15:04:05")},
|
||||||
{"post_status", "in", ""},
|
{"post_status", "in", ""},
|
||||||
{"post_type", "post"},
|
{"post_type", "post"},
|
||||||
}, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []interface{}{"publish", "private"})
|
}, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []interface{}{"publish", "private"})
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,6 @@ func Detail(c *gin.Context) {
|
||||||
showComment = false
|
showComment = false
|
||||||
}
|
}
|
||||||
prev, next, err := common.GetContextPost(post.Id, post.PostDate)
|
prev, next, err := common.GetContextPost(post.Id, post.PostDate)
|
||||||
|
|
||||||
h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
|
h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"])
|
||||||
h["post"] = post
|
h["post"] = post
|
||||||
h["showComment"] = showComment
|
h["showComment"] = showComment
|
||||||
|
|
5
cache/map.go
vendored
5
cache/map.go
vendored
|
@ -22,6 +22,7 @@ func NewMapCache[K comparable, V any](fun func(...any) (V, error), duration time
|
||||||
mutex: &sync.Mutex{},
|
mutex: &sync.Mutex{},
|
||||||
setCacheFunc: fun,
|
setCacheFunc: fun,
|
||||||
expireTime: duration,
|
expireTime: duration,
|
||||||
|
data: make(map[K]V),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,10 +34,10 @@ func (c *MapCache[K, V]) FlushCache(k any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MapCache[K, V]) GetCache(ctx context.Context, key K, timeout time.Duration, params ...any) (V, error) {
|
func (c *MapCache[K, V]) GetCache(ctx context.Context, key K, timeout time.Duration, params ...any) (V, error) {
|
||||||
l := len(c.data)
|
_, ok := c.data[key]
|
||||||
var err error
|
var err error
|
||||||
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
||||||
if l < 1 || (l > 0 && c.expireTime >= 0 && expired) {
|
if !ok || (c.expireTime >= 0 && expired) {
|
||||||
t := c.incr
|
t := c.incr
|
||||||
call := func() {
|
call := func() {
|
||||||
c.mutex.Lock()
|
c.mutex.Lock()
|
||||||
|
|
4
cache/slice.go
vendored
4
cache/slice.go
vendored
|
@ -33,6 +33,7 @@ func (c *SliceCache[T]) FlushCache() {
|
||||||
|
|
||||||
func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) ([]T, error) {
|
func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) ([]T, error) {
|
||||||
l := len(c.data)
|
l := len(c.data)
|
||||||
|
data := c.data
|
||||||
var err error
|
var err error
|
||||||
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix())
|
||||||
if l < 1 || (l > 0 && c.expireTime >= 0 && expired) {
|
if l < 1 || (l > 0 && c.expireTime >= 0 && expired) {
|
||||||
|
@ -50,6 +51,7 @@ func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, par
|
||||||
}
|
}
|
||||||
c.setTime = time.Now()
|
c.setTime = time.Now()
|
||||||
c.data = r
|
c.data = r
|
||||||
|
data = r
|
||||||
c.incr++
|
c.incr++
|
||||||
}
|
}
|
||||||
if timeout > 0 {
|
if timeout > 0 {
|
||||||
|
@ -72,5 +74,5 @@ func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, par
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return c.data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user