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"
"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
}

View File

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

View File

@ -51,7 +51,7 @@ func InitActionsCommonCache() {
c := config.GetConfig()
archivesCaches = &Arch{
mutex: &sync.Mutex{},
setCacheFunc: dao.Archives,
fn: dao.Archives,
}
searchPostIdsCache = cache.NewMemoryMapCacheByFn[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
@ -136,25 +136,25 @@ func Archives(ctx context.Context) (r []models.PostArchive) {
type Arch struct {
data []models.PostArchive
mutex *sync.Mutex
setCacheFunc func(context.Context) ([]models.PostArchive, error)
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 {