From dc203f87b8d1a2dd3e587aa2acafc12be53b5ff1 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 6 Feb 2023 15:59:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BC=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/actions/detail.go | 5 ++++- internal/actions/index.go | 1 + internal/pkg/cache/cache.go | 30 +++++++++++++++--------------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/internal/actions/detail.go b/internal/actions/detail.go index c40647b..f7652f4 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -5,6 +5,7 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/logs" + "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" "github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/wpconfig" @@ -19,6 +20,7 @@ type detailHandler struct { func Detail(c *gin.Context) { var err error + var post models.Posts recent := cache.RecentPosts(c, 5) archive := cache.Archives(c) categoryItems := cache.CategoriesTags(c, plugins.Category) @@ -29,6 +31,7 @@ func Detail(c *gin.Context) { "archives": archive, "categories": categoryItems, "recentComments": recentComments, + "post": post, } isApproveComment := false status := plugins.Ok @@ -54,7 +57,7 @@ func Detail(c *gin.Context) { if ID > maxId || ID <= 0 || err != nil { return } - post, err := cache.GetPostById(c, ID) + post, err = cache.GetPostById(c, ID) if post.Id == 0 || err != nil || post.PostStatus != "publish" { return } diff --git a/internal/actions/index.go b/internal/actions/index.go index 852d758..c4c34c7 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -231,6 +231,7 @@ func Index(c *gin.Context) { "search": h.search, "header": h.header, "recentComments": recentComments, + "posts": posts, } defer func() { code := http.StatusOK diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 6fb8d46..67d6ab0 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -50,8 +50,8 @@ var ctx context.Context func InitActionsCommonCache() { c := config.GetConfig() archivesCaches = &Arch{ - mutex: &sync.Mutex{}, - setCacheFunc: dao.Archives, + mutex: &sync.Mutex{}, + fn: dao.Archives, } searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime) @@ -134,27 +134,27 @@ func Archives(ctx context.Context) (r []models.PostArchive) { } type Arch struct { - data []models.PostArchive - mutex *sync.Mutex - setCacheFunc func(context.Context) ([]models.PostArchive, error) - month time.Month + data []models.PostArchive + mutex *sync.Mutex + fn func(context.Context) ([]models.PostArchive, error) + month time.Month } -func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { - l := len(c.data) +func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { + l := len(a.data) m := time.Now().Month() - if l > 0 && c.month != m || l < 1 { - r, err := c.setCacheFunc(ctx) + if l > 0 && a.month != m || l < 1 { + r, err := a.fn(ctx) if err != nil { logs.ErrPrintln(err, "set cache err[%s]") return nil } - c.mutex.Lock() - defer c.mutex.Unlock() - c.month = m - c.data = r + a.mutex.Lock() + defer a.mutex.Unlock() + a.month = m + a.data = r } - return c.data + return a.data } func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {