小优
This commit is contained in:
parent
404f27f693
commit
dc203f87b8
|
@ -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
|
||||
}
|
||||
|
|
|
@ -231,6 +231,7 @@ func Index(c *gin.Context) {
|
|||
"search": h.search,
|
||||
"header": h.header,
|
||||
"recentComments": recentComments,
|
||||
"posts": posts,
|
||||
}
|
||||
defer func() {
|
||||
code := http.StatusOK
|
||||
|
|
22
internal/pkg/cache/cache.go
vendored
22
internal/pkg/cache/cache.go
vendored
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user