From a3c08edb71c99141ec263ce8f4031395d63f914f Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 20 Sep 2022 16:55:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 28 +++++++++++++++++++++++++--- actions/detail.go | 1 - cache/map.go | 5 +++-- cache/slice.go | 4 +++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/actions/common/common.go b/actions/common/common.go index c926d66..4954e20 100644 --- a/actions/common/common.go +++ b/actions/common/common.go @@ -2,6 +2,7 @@ package common import ( "context" + "database/sql" "fmt" "github/fthvgb1/wp-go/cache" "github/fthvgb1/wp-go/models" @@ -18,6 +19,7 @@ var PostContextCache sync.Map var archivesCaches *Arch var categoryCaches *cache.SliceCache[models.WpTermsMy] var recentPostsCaches *cache.SliceCache[models.WpPosts] +var monthCaches *cache.MapCache[string, []models.WpPosts] func InitCache() { archivesCaches = &Arch{ @@ -26,6 +28,7 @@ func InitCache() { } categoryCaches = cache.NewSliceCache[models.WpTermsMy](categories, vars.Conf.CategoryCacheTime) recentPostsCaches = cache.NewSliceCache[models.WpPosts](recentPosts, vars.Conf.RecentPostCacheTime) + monthCaches = cache.NewMapCache[string, []models.WpPosts](getMonthPost, 30*time.Minute) } type Arch struct { @@ -59,6 +62,25 @@ type PostContext struct { 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) { post, ok := PostContextCache.Load(id) if ok { @@ -85,14 +107,14 @@ func getPostContext(t time.Time) (prev, next models.WpPosts, err error) { {"post_status", "in", ""}, {"post_type", "post"}, }, "ID,post_title,post_password", nil, []interface{}{"publish", "private"}) - 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", "in", ""}, {"post_type", "post"}, }, "ID,post_title", models.SqlBuilder{{"post_date", "desc"}}, []interface{}{"publish", "private"}) + if err == sql.ErrNoRows { + err = nil + } return } diff --git a/actions/detail.go b/actions/detail.go index f4ef509..9baf140 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -62,7 +62,6 @@ func Detail(c *gin.Context) { showComment = false } prev, next, err := common.GetContextPost(post.Id, post.PostDate) - h["title"] = fmt.Sprintf("%s-%s", post.PostTitle, models.Options["blogname"]) h["post"] = post h["showComment"] = showComment diff --git a/cache/map.go b/cache/map.go index ca1a490..4516997 100644 --- a/cache/map.go +++ b/cache/map.go @@ -22,6 +22,7 @@ func NewMapCache[K comparable, V any](fun func(...any) (V, error), duration time mutex: &sync.Mutex{}, setCacheFunc: fun, 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) { - l := len(c.data) + _, ok := c.data[key] var err error 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 call := func() { c.mutex.Lock() diff --git a/cache/slice.go b/cache/slice.go index e847469..6be73f5 100644 --- a/cache/slice.go +++ b/cache/slice.go @@ -33,6 +33,7 @@ func (c *SliceCache[T]) FlushCache() { func (c *SliceCache[T]) GetCache(ctx context.Context, timeout time.Duration, params ...any) ([]T, error) { l := len(c.data) + data := c.data var err error expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) 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.data = r + data = r c.incr++ } 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 }