去掉页眉缓存时间配置

This commit is contained in:
xing 2023-03-29 23:31:16 +08:00
parent 49e4210365
commit 96368234de
5 changed files with 94 additions and 106 deletions

View File

@ -58,8 +58,6 @@ cacheTime:
userInfoCacheTime: 24h
# 单独评论缓存时间
commentsCacheTime: 24h
# 主题的页眉图片缓存时间
themeHeaderImagCacheTime: 5m
# 随机sleep时间
sleepTime: [ 1s,3s ]
# 摘要字数

View File

@ -42,8 +42,6 @@ var newCommentCache *cache.MapCache[string, string]
var allUsernameCache *cache.VarCache[map[string]struct{}]
var headerImagesCache *cache.MapCache[string, []models.PostThumbnail]
func InitActionsCommonCache() {
c := config.GetConfig()
archivesCaches = &Arch{
@ -81,8 +79,6 @@ func InitActionsCommonCache() {
allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime)
headerImagesCache = cachemanager.MapCacheBy[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime)
feedCache = cache.NewVarCache(feed, time.Hour)
postFeedCache = cachemanager.MapCacheBy[string](postFeed, time.Hour)

View File

@ -1,81 +0,0 @@
package cache
import (
"context"
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
"time"
)
func GetHeaderImages(ctx context.Context, theme string) (r []models.PostThumbnail, err error) {
r, err = headerImagesCache.GetCache(ctx, theme, time.Second, ctx, theme)
return
}
func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) {
ctx := a[0].(context.Context)
theme := a[1].(string)
meta, err := wpconfig.GetThemeMods(theme)
if err != nil || meta.HeaderImage == "" {
return
}
if "random-uploaded-image" != meta.HeaderImage {
m, er := GetPostById(ctx, uint64(meta.HeaderImagData.AttachmentId))
if er != nil {
err = er
return
}
m.Thumbnail = thumb(m, theme)
r = []models.PostThumbnail{m.Thumbnail}
return
}
headers, er := model.Finds[models.Posts](ctx, model.Conditions(
model.Where(model.SqlBuilder{
{"post_type", "attachment"},
{"post_status", "inherit"},
{"meta_value", theme},
{"meta_key", "_wp_attachment_is_custom_header"},
}),
model.Fields("a.ID"),
model.Group("a.ID"),
model.Join(model.SqlBuilder{
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
}),
))
if er != nil {
err = er
return
}
if len(headers) > 0 {
posts, er := GetPostsByIds(ctx, slice.Map(headers, func(t models.Posts) uint64 {
return t.Id
}))
if er != nil {
err = er
return
}
r = slice.Map(posts, func(m models.Posts) models.PostThumbnail {
return thumb(m, theme)
})
}
return
}
func thumb(m models.Posts, theme string) models.PostThumbnail {
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
m.Thumbnail.Width = m.AttachmentMetadata.Width
m.Thumbnail.Height = m.AttachmentMetadata.Height
if m.Thumbnail.Path != "" {
if len(m.AttachmentMetadata.Sizes) > 0 {
m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset)
}
}
return m.Thumbnail
}

View File

@ -38,24 +38,23 @@ type Config struct {
}
type CacheTime struct {
CacheControl time.Duration `yaml:"cacheControl"`
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"`
SleepTime []time.Duration `yaml:"sleepTime"`
CacheControl time.Duration `yaml:"cacheControl"`
RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"`
CategoryCacheTime time.Duration `yaml:"categoryCacheTime"`
ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"`
ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"`
RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"`
DigestCacheTime time.Duration `yaml:"digestCacheTime"`
PostListCacheTime time.Duration `yaml:"postListCacheTime"`
SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"`
MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"`
PostDataCacheTime time.Duration `yaml:"postDataCacheTime"`
PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"`
CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"`
MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"`
UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"`
CommentsCacheTime time.Duration `yaml:"commentsCacheTime"`
SleepTime []time.Duration `yaml:"sleepTime"`
}
type Ssl struct {

View File

@ -1,10 +1,15 @@
package wp
import (
"fmt"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"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/wpconfig"
"github.com/fthvgb1/wp-go/model"
)
func (h *Handle) DisplayHeaderText() bool {
@ -12,11 +17,20 @@ func (h *Handle) DisplayHeaderText() bool {
}
func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
hs, err := cache.GetHeaderImages(h.C, h.theme)
var err error
hs := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail {
hs, er := h.GetHeaderImages(h.theme)
if er != nil {
err = er
return nil
}
return hs
})
if err != nil {
logs.ErrPrintln(err, "获取页眉背景图失败")
return
}
if len(hs) < 1 {
return
}
@ -26,3 +40,65 @@ func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
r, _ = slice.RandPop(&hs)
return
}
func (h *Handle) GetHeaderImages(theme string) (r []models.PostThumbnail, err error) {
meta, err := wpconfig.GetThemeMods(theme)
if err != nil || meta.HeaderImage == "" {
return
}
if "random-uploaded-image" != meta.HeaderImage {
m, er := cache.GetPostById(h.C, uint64(meta.HeaderImagData.AttachmentId))
if er != nil {
err = er
return
}
m.Thumbnail = thumb(m, theme)
r = []models.PostThumbnail{m.Thumbnail}
return
}
headers, er := model.Finds[models.Posts](h.C, model.Conditions(
model.Where(model.SqlBuilder{
{"post_type", "attachment"},
{"post_status", "inherit"},
{"meta_value", theme},
{"meta_key", "_wp_attachment_is_custom_header"},
}),
model.Fields("a.ID"),
model.Group("a.ID"),
model.Join(model.SqlBuilder{
{" a", "left join", "wp_postmeta b", "a.ID=b.post_id"},
}),
))
if er != nil {
err = er
return
}
if len(headers) > 0 {
posts, er := cache.GetPostsByIds(h.C, slice.Map(headers, func(t models.Posts) uint64 {
return t.Id
}))
if er != nil {
err = er
return
}
r = slice.Map(posts, func(m models.Posts) models.PostThumbnail {
return thumb(m, theme)
})
}
return
}
func thumb(m models.Posts, theme string) models.PostThumbnail {
m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme))
m.Thumbnail.Width = m.AttachmentMetadata.Width
m.Thumbnail.Height = m.AttachmentMetadata.Height
if m.Thumbnail.Path != "" {
if len(m.AttachmentMetadata.Sizes) > 0 {
m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset)
}
}
return m.Thumbnail
}