From e7e643f5d33330e28264e27514def453ab2751a5 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 30 Mar 2023 19:50:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcustomlogo=E5=92=8C=E5=BD=92?= =?UTF-8?q?=E6=A1=A3=E7=9A=84=E5=B9=B6=E5=8F=91=E8=AF=BB=E5=86=99=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/html/html.go | 3 ++ internal/pkg/cache/cache.go | 28 +++++++------------ internal/theme/wp/components/widget/search.go | 5 ++-- internal/theme/wp/customlogo.go | 5 ++++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/helper/html/html.go b/helper/html/html.go index e26318e..32ace0c 100644 --- a/helper/html/html.go +++ b/helper/html/html.go @@ -32,6 +32,9 @@ const ( ) func SpecialChars(text string, flag ...int) string { + if len(text) < 1 { + return "" + } flags := EntQuotes if len(flag) > 0 { flags = flag[0] diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 445446d..1ef3a92 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -10,12 +10,10 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/safety" - "sync" "time" ) var postContextCache *cache.MapCache[uint64, dao.PostContext] -var archivesCaches *Arch var categoryAndTagsCaches *cache.MapCache[int, []models.TermsMy] var recentPostsCaches *cache.VarCache[[]models.Posts] var recentCommentsCaches *cache.VarCache[[]models.Comments] @@ -45,11 +43,6 @@ var allUsernameCache *cache.VarCache[map[string]struct{}] func InitActionsCommonCache() { c := config.GetConfig() - archivesCaches = &Arch{ - mutex: &sync.Mutex{}, - fn: dao.Archives, - data: *safety.NewVar([]models.PostArchive{}), - } searchPostIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime) @@ -92,19 +85,19 @@ func InitActionsCommonCache() { InitFeed() } -func Archives(ctx context.Context) (r []models.PostArchive) { - return archivesCaches.getArchiveCache(ctx) -} - type Arch struct { - data safety.Var[[]models.PostArchive] - mutex *sync.Mutex + data []models.PostArchive fn func(context.Context) ([]models.PostArchive, error) month time.Month } -func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { - data := a.data.Load() +var arch = safety.NewVar(Arch{ + fn: dao.Archives, +}) + +func Archives(ctx context.Context) []models.PostArchive { + a := arch.Load() + data := a.data l := len(data) m := time.Now().Month() if l > 0 && a.month != m || l < 1 { @@ -113,10 +106,9 @@ func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { logs.ErrPrintln(err, "set cache err[%s]") return nil } - a.mutex.Lock() - defer a.mutex.Unlock() a.month = m - a.data.Store(r) + a.data = r + arch.Store(a) data = r } return data diff --git a/internal/theme/wp/components/widget/search.go b/internal/theme/wp/components/widget/search.go index 91ca6c8..5f8aa2f 100644 --- a/internal/theme/wp/components/widget/search.go +++ b/internal/theme/wp/components/widget/search.go @@ -72,9 +72,10 @@ func Search(h *wp.Handle, id string) string { }) args = maps.Copy(args) s := strings.ReplaceAll(searchTemplate, "{$form}", form) - args["{$value}"] = "" + val := "" if h.Scene() == constraints.Search { - args["{$value}"] = html.SpecialChars(h.Index.Param.Search) + val = html.SpecialChars(h.Index.Param.Search) } + s = strings.ReplaceAll(s, "{$value}", val) return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args)) } diff --git a/internal/theme/wp/customlogo.go b/internal/theme/wp/customlogo.go index 1708910..573bcd9 100644 --- a/internal/theme/wp/customlogo.go +++ b/internal/theme/wp/customlogo.go @@ -6,8 +6,11 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/wpconfig" + "sync" ) +var logoLock = sync.Mutex{} + func CalCustomLogo(h *Handle) (r string) { id := uint64(h.themeMods.CustomLogo) if id < 1 { @@ -30,7 +33,9 @@ func CalCustomLogo(h *Handle) (r string) { "decoding": "async", //"loading":"lazy", } + logoLock.Lock() img := wpconfig.Thumbnail(logo.AttachmentMetadata, siz, "", "") + logoLock.Unlock() imgx["srcset"] = img.Srcset imgx["sizes"] = img.Sizes imgx["src"] = img.Path