修复customlogo和归档的并发读写问题

This commit is contained in:
xing 2023-03-30 19:50:29 +08:00
parent 522a358819
commit e7e643f5d3
4 changed files with 21 additions and 20 deletions

View File

@ -32,6 +32,9 @@ const (
) )
func SpecialChars(text string, flag ...int) string { func SpecialChars(text string, flag ...int) string {
if len(text) < 1 {
return ""
}
flags := EntQuotes flags := EntQuotes
if len(flag) > 0 { if len(flag) > 0 {
flags = flag[0] flags = flag[0]

View File

@ -10,12 +10,10 @@ import (
"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/pkg/models"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"sync"
"time" "time"
) )
var postContextCache *cache.MapCache[uint64, dao.PostContext] var postContextCache *cache.MapCache[uint64, dao.PostContext]
var archivesCaches *Arch
var categoryAndTagsCaches *cache.MapCache[int, []models.TermsMy] var categoryAndTagsCaches *cache.MapCache[int, []models.TermsMy]
var recentPostsCaches *cache.VarCache[[]models.Posts] var recentPostsCaches *cache.VarCache[[]models.Posts]
var recentCommentsCaches *cache.VarCache[[]models.Comments] var recentCommentsCaches *cache.VarCache[[]models.Comments]
@ -45,11 +43,6 @@ var allUsernameCache *cache.VarCache[map[string]struct{}]
func InitActionsCommonCache() { func InitActionsCommonCache() {
c := config.GetConfig() 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) searchPostIdsCache = cachemanager.MapCacheBy[string](dao.SearchPostIds, c.CacheTime.SearchPostCacheTime)
@ -92,19 +85,19 @@ func InitActionsCommonCache() {
InitFeed() InitFeed()
} }
func Archives(ctx context.Context) (r []models.PostArchive) {
return archivesCaches.getArchiveCache(ctx)
}
type Arch struct { type Arch struct {
data safety.Var[[]models.PostArchive] data []models.PostArchive
mutex *sync.Mutex
fn func(context.Context) ([]models.PostArchive, error) fn func(context.Context) ([]models.PostArchive, error)
month time.Month month time.Month
} }
func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive { var arch = safety.NewVar(Arch{
data := a.data.Load() fn: dao.Archives,
})
func Archives(ctx context.Context) []models.PostArchive {
a := arch.Load()
data := a.data
l := len(data) l := len(data)
m := time.Now().Month() m := time.Now().Month()
if l > 0 && a.month != m || l < 1 { 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]") logs.ErrPrintln(err, "set cache err[%s]")
return nil return nil
} }
a.mutex.Lock()
defer a.mutex.Unlock()
a.month = m a.month = m
a.data.Store(r) a.data = r
arch.Store(a)
data = r data = r
} }
return data return data

View File

@ -72,9 +72,10 @@ func Search(h *wp.Handle, id string) string {
}) })
args = maps.Copy(args) args = maps.Copy(args)
s := strings.ReplaceAll(searchTemplate, "{$form}", form) s := strings.ReplaceAll(searchTemplate, "{$form}", form)
args["{$value}"] = "" val := ""
if h.Scene() == constraints.Search { 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)) return h.ComponentFilterFnHook(widgets.Search, str.Replace(s, args))
} }

View File

@ -6,8 +6,11 @@ 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/wpconfig" "github.com/fthvgb1/wp-go/internal/wpconfig"
"sync"
) )
var logoLock = sync.Mutex{}
func CalCustomLogo(h *Handle) (r string) { func CalCustomLogo(h *Handle) (r string) {
id := uint64(h.themeMods.CustomLogo) id := uint64(h.themeMods.CustomLogo)
if id < 1 { if id < 1 {
@ -30,7 +33,9 @@ func CalCustomLogo(h *Handle) (r string) {
"decoding": "async", "decoding": "async",
//"loading":"lazy", //"loading":"lazy",
} }
logoLock.Lock()
img := wpconfig.Thumbnail(logo.AttachmentMetadata, siz, "", "") img := wpconfig.Thumbnail(logo.AttachmentMetadata, siz, "", "")
logoLock.Unlock()
imgx["srcset"] = img.Srcset imgx["srcset"] = img.Srcset
imgx["sizes"] = img.Sizes imgx["sizes"] = img.Sizes
imgx["src"] = img.Path imgx["src"] = img.Path