修复并发读写错误 todo customlogo还没处理完全

This commit is contained in:
xing 2023-03-30 00:46:33 +08:00
parent 96368234de
commit 522a358819
6 changed files with 28 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/fthvgb1/wp-go/internal/pkg/dao" "github.com/fthvgb1/wp-go/internal/pkg/dao"
"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"
"sync" "sync"
"time" "time"
) )
@ -47,6 +48,7 @@ func InitActionsCommonCache() {
archivesCaches = &Arch{ archivesCaches = &Arch{
mutex: &sync.Mutex{}, mutex: &sync.Mutex{},
fn: dao.Archives, 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)
@ -95,14 +97,15 @@ func Archives(ctx context.Context) (r []models.PostArchive) {
} }
type Arch struct { type Arch struct {
data []models.PostArchive data safety.Var[[]models.PostArchive]
mutex *sync.Mutex 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 { func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
l := len(a.data) data := a.data.Load()
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 {
r, err := a.fn(ctx) r, err := a.fn(ctx)
@ -113,9 +116,10 @@ func (a *Arch) getArchiveCache(ctx context.Context) []models.PostArchive {
a.mutex.Lock() a.mutex.Lock()
defer a.mutex.Unlock() defer a.mutex.Unlock()
a.month = m a.month = m
a.data = r a.data.Store(r)
data = r
} }
return a.data return data
} }
// CategoriesTags categories or tags // CategoriesTags categories or tags

View File

@ -100,6 +100,17 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string,
if maps.GetAnyAnyValWithDefaults(conf, false, "showHierarchy") { if maps.GetAnyAnyValWithDefaults(conf, false, "showHierarchy") {
conf["hierarchical"] = int64(1) conf["hierarchical"] = int64(1)
} }
class := maps.GetAnyAnyValWithDefaults(conf, "", "className")
classes := strings.Split(class, " ")
classes = append(classes, "wp-block-categories")
if conf["dropdown"].(int64) == 1 {
classes = append(classes, "wp-block-categories-dropdown")
conf["className"] = strings.Join(classes, " ")
} else {
classes = append(classes, "wp-block-categories-list")
conf["className"] = strings.Join(classes, " ")
}
return conf return conf
}) })
@ -130,16 +141,9 @@ func category(h *wp.Handle, id string, counter number.Counter[int], args map[str
var out = "" var out = ""
categories := cache.CategoriesTags(h.C, constraints2.Category) categories := cache.CategoriesTags(h.C, constraints2.Category)
class := []string{"widget", "widget_block", "widget_categories"} class := []string{"widget", "widget_block", "widget_categories"}
classx := maps.GetAnyAnyValWithDefaults(conf, "", "className")
classes := strings.Split(classx, " ")
classes = append(classes, "wp-block-categories")
if conf["dropdown"].(int64) == 1 { if conf["dropdown"].(int64) == 1 {
classes = append(classes, "wp-block-categories-dropdown")
conf["className"] = strings.Join(classes, " ")
out = dropdown(h, categories, counter(), args, conf) out = dropdown(h, categories, counter(), args, conf)
} else { } else {
classes = append(classes, "wp-block-categories-list")
conf["className"] = strings.Join(classes, " ")
out = categoryUl(h, categories, conf) out = categoryUl(h, categories, conf)
} }
before := fmt.Sprintf(args["{$before_widget}"], str.Join("block-", id), strings.Join(class, " ")) before := fmt.Sprintf(args["{$before_widget}"], str.Join("block-", id), strings.Join(class, " "))

View File

@ -57,6 +57,10 @@ func Archive(h *wp.Handle, id string) string {
args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args) args = maps.FilterZeroMerge(archiveArgs, CommonArgs(), commonArgs, args)
args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive")) args["{$before_widget}"] = fmt.Sprintf(args["{$before_widget}"], str.Join("archives-", id), str.Join("widget widget_", "archive"))
args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"]) args["{$title}"] = str.Join(args["{$before_title}"], conf["title"].(string), args["{$after_title}"])
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string))
args["{$navCloser}"] = "</nav>"
}
return args return args
}) })
@ -109,10 +113,6 @@ func archiveDropDown(h *wp.Handle, conf map[any]any, args map[string]string, arc
} }
func archiveUl(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string { func archiveUl(h *wp.Handle, conf map[any]any, args map[string]string, archives []models.PostArchive) string {
if slice.IsContained(h.CommonThemeMods().ThemeSupport.HTML5, "navigation-widgets") {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string))
args["{$navCloser}"] = "</nav>"
}
s := str.NewBuilder() s := str.NewBuilder()
s.WriteString(`<ul>`) s.WriteString(`<ul>`)
showCount := conf["count"].(int64) showCount := conf["count"].(int64)

View File

@ -70,6 +70,7 @@ func Search(h *wp.Handle, id string) string {
return args return args
}) })
args = maps.Copy(args)
s := strings.ReplaceAll(searchTemplate, "{$form}", form) s := strings.ReplaceAll(searchTemplate, "{$form}", form)
args["{$value}"] = "" args["{$value}"] = ""
if h.Scene() == constraints.Search { if h.Scene() == constraints.Search {

View File

@ -18,7 +18,7 @@ func (h *Handle) DisplayHeaderText() bool {
func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) { func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
var err error var err error
hs := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail { hss := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail {
hs, er := h.GetHeaderImages(h.theme) hs, er := h.GetHeaderImages(h.theme)
if er != nil { if er != nil {
err = er err = er
@ -30,6 +30,7 @@ func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) {
logs.ErrPrintln(err, "获取页眉背景图失败") logs.ErrPrintln(err, "获取页眉背景图失败")
return return
} }
hs := slice.Copy(hss)
if len(hs) < 1 { if len(hs) < 1 {
return return

View File

@ -73,11 +73,12 @@ type ImageData struct {
func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) { func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except ...string) (r models.PostThumbnail) {
up := strings.Split(metadata.File, "/") up := strings.Split(metadata.File, "/")
if metadata.File != "" && Type == "full" { if metadata.File != "" && Type == "full" {
mimeType := metadata.Sizes["thumbnail"].MimeType
metadata.Sizes["full"] = models.MetaDataFileSize{ metadata.Sizes["full"] = models.MetaDataFileSize{
File: filepath.Base(metadata.File), File: filepath.Base(metadata.File),
Width: metadata.Width, Width: metadata.Width,
Height: metadata.Height, Height: metadata.Height,
MimeType: metadata.Sizes["thumbnail"].MimeType, MimeType: mimeType,
FileSize: metadata.FileSize, FileSize: metadata.FileSize,
} }
} }