修复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 {
if len(text) < 1 {
return ""
}
flags := EntQuotes
if len(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/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

View File

@ -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))
}

View File

@ -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