This commit is contained in:
xing 2023-02-06 15:59:44 +08:00
parent 404f27f693
commit dc203f87b8
3 changed files with 20 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/logs" "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/plugins"
"github.com/fthvgb1/wp-go/internal/theme" "github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
@ -19,6 +20,7 @@ type detailHandler struct {
func Detail(c *gin.Context) { func Detail(c *gin.Context) {
var err error var err error
var post models.Posts
recent := cache.RecentPosts(c, 5) recent := cache.RecentPosts(c, 5)
archive := cache.Archives(c) archive := cache.Archives(c)
categoryItems := cache.CategoriesTags(c, plugins.Category) categoryItems := cache.CategoriesTags(c, plugins.Category)
@ -29,6 +31,7 @@ func Detail(c *gin.Context) {
"archives": archive, "archives": archive,
"categories": categoryItems, "categories": categoryItems,
"recentComments": recentComments, "recentComments": recentComments,
"post": post,
} }
isApproveComment := false isApproveComment := false
status := plugins.Ok status := plugins.Ok
@ -54,7 +57,7 @@ func Detail(c *gin.Context) {
if ID > maxId || ID <= 0 || err != nil { if ID > maxId || ID <= 0 || err != nil {
return return
} }
post, err := cache.GetPostById(c, ID) post, err = cache.GetPostById(c, ID)
if post.Id == 0 || err != nil || post.PostStatus != "publish" { if post.Id == 0 || err != nil || post.PostStatus != "publish" {
return return
} }

View File

@ -231,6 +231,7 @@ func Index(c *gin.Context) {
"search": h.search, "search": h.search,
"header": h.header, "header": h.header,
"recentComments": recentComments, "recentComments": recentComments,
"posts": posts,
} }
defer func() { defer func() {
code := http.StatusOK code := http.StatusOK

View File

@ -50,8 +50,8 @@ var ctx context.Context
func InitActionsCommonCache() { func InitActionsCommonCache() {
c := config.GetConfig() c := config.GetConfig()
archivesCaches = &Arch{ archivesCaches = &Arch{
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
setCacheFunc: dao.Archives, fn: dao.Archives,
} }
searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime) searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
@ -134,27 +134,27 @@ func Archives(ctx context.Context) (r []models.PostArchive) {
} }
type Arch struct { type Arch struct {
data []models.PostArchive data []models.PostArchive
mutex *sync.Mutex mutex *sync.Mutex
setCacheFunc func(context.Context) ([]models.PostArchive, error) fn func(context.Context) ([]models.PostArchive, error)
month time.Month month time.Month
} }
func (c *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
l := len(c.data) l := len(a.data)
m := time.Now().Month() m := time.Now().Month()
if l > 0 && c.month != m || l < 1 { if l > 0 && a.month != m || l < 1 {
r, err := c.setCacheFunc(ctx) r, err := a.fn(ctx)
if err != nil { if err != nil {
logs.ErrPrintln(err, "set cache err[%s]") logs.ErrPrintln(err, "set cache err[%s]")
return nil return nil
} }
c.mutex.Lock() a.mutex.Lock()
defer c.mutex.Unlock() defer a.mutex.Unlock()
c.month = m a.month = m
c.data = r a.data = r
} }
return c.data return a.data
} }
func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy { func CategoriesTags(ctx context.Context, t ...int) []models.TermsMy {