From a5294e2e20d1c77712e299b70ab0827ed36eb32e Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 12 Nov 2023 21:39:04 +0800 Subject: [PATCH] revise reload package --- app/cmd/main.go | 2 +- app/cmd/route/route.go | 2 +- app/middleware/searchlimit.go | 2 +- app/middleware/validateservername.go | 2 +- app/theme/twentyfifteen/customheader.go | 2 +- app/theme/twentyseventeen/twentyseventeen.go | 2 +- app/theme/wp/components.go | 14 ++++--- app/theme/wp/components/block/category.go | 18 ++++----- app/theme/wp/components/widget/archive.go | 6 +-- app/theme/wp/components/widget/category.go | 6 +-- app/theme/wp/components/widget/fn.go | 6 +-- app/theme/wp/components/widget/meta.go | 6 +-- .../wp/components/widget/recentcomments.go | 6 +-- app/theme/wp/components/widget/recentposts.go | 10 ++--- app/theme/wp/components/widget/search.go | 6 +-- app/theme/wp/customheader.go | 10 ++--- app/theme/wp/customlogo.go | 6 +-- app/theme/wp/index.go | 6 +-- app/theme/wp/listpostplugins.go | 2 +- app/theme/wp/pipe.go | 10 ++--- app/theme/wp/route/route.go | 32 ++++++++-------- app/theme/wp/stickyposts.go | 9 +++-- app/theme/wp/wp.go | 6 +-- app/wpconfig/thememods.go | 2 +- cache/cachemanager/manger.go | 2 +- {app/cmd => cache}/reload/reload.go | 38 +++++++++---------- {app/cmd => cache}/reload/reload_test.go | 0 helper/func.go | 10 +++++ safety/slice.go | 3 +- 29 files changed, 121 insertions(+), 105 deletions(-) rename {app/cmd => cache}/reload/reload.go (88%) rename {app/cmd => cache}/reload/reload_test.go (100%) diff --git a/app/cmd/main.go b/app/cmd/main.go index c99e884..9c6f3db 100644 --- a/app/cmd/main.go +++ b/app/cmd/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/cmd/route" "github.com/fthvgb1/wp-go/app/mail" "github.com/fthvgb1/wp-go/app/pkg/cache" @@ -15,6 +14,7 @@ import ( "github.com/fthvgb1/wp-go/app/theme" "github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/cache/cachemanager" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/model" "log" "os" diff --git a/app/cmd/route/route.go b/app/cmd/route/route.go index 4fda57e..8d0ed6a 100644 --- a/app/cmd/route/route.go +++ b/app/cmd/route/route.go @@ -2,12 +2,12 @@ package route import ( "github.com/fthvgb1/wp-go/app/actions" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/middleware" "github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/theme" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-contrib/gzip" "github.com/gin-contrib/pprof" diff --git a/app/middleware/searchlimit.go b/app/middleware/searchlimit.go index 8be4ac2..eea1f2a 100644 --- a/app/middleware/searchlimit.go +++ b/app/middleware/searchlimit.go @@ -1,8 +1,8 @@ package middleware import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/config" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/gin-gonic/gin" ) diff --git a/app/middleware/validateservername.go b/app/middleware/validateservername.go index 270fa3e..6cde608 100644 --- a/app/middleware/validateservername.go +++ b/app/middleware/validateservername.go @@ -1,8 +1,8 @@ package middleware import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/config" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/gin-gonic/gin" "net/http" diff --git a/app/theme/twentyfifteen/customheader.go b/app/theme/twentyfifteen/customheader.go index 838623c..e60bdb7 100644 --- a/app/theme/twentyfifteen/customheader.go +++ b/app/theme/twentyfifteen/customheader.go @@ -1,9 +1,9 @@ package twentyfifteen import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/reload" str "github.com/fthvgb1/wp-go/helper/strings" ) diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 244b2bd..249285b 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -2,7 +2,6 @@ package twentyseventeen import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/logs" @@ -12,6 +11,7 @@ import ( "github.com/fthvgb1/wp-go/app/theme/wp/components" "github.com/fthvgb1/wp-go/app/theme/wp/components/widget" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-gonic/gin" diff --git a/app/theme/wp/components.go b/app/theme/wp/components.go index 3cefd3f..87a21a3 100644 --- a/app/theme/wp/components.go +++ b/app/theme/wp/components.go @@ -1,8 +1,8 @@ package wp import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -27,15 +27,15 @@ func (h *Handle) HookComponents(scene string, fn func(Components[string]) (Compo } func CalComponents(h *Handle) { - componentss := reload.GetAnyValMapBy("scene-components", str.Join("allScene-", h.scene), h, func(h *Handle) map[string][]Components[string] { + componentss := reload.GetAnyValMapBy("scene-components", str.Join("allScene-", h.scene), h, func(h *Handle) (map[string][]Components[string], bool) { return maps.MergeBy(func(k string, v1, v2 []Components[string]) ([]Components[string], bool) { vv := append(v1, v2...) return vv, vv != nil - }, nil, h.components[h.scene], h.components[constraints.AllScene]) + }, nil, h.components[h.scene], h.components[constraints.AllScene]), true }) for k, components := range componentss { key := str.Join("calComponents-", h.scene, "-", k) - ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) []Components[string] { + ss := reload.GetAnyValMapBy("calComponents", key, h, func(h *Handle) ([]Components[string], bool) { r := slice.FilterAndMap(components, func(t Components[string]) (Components[string], bool) { fns, ok := h.componentHook[k] if !ok { @@ -53,7 +53,7 @@ func CalComponents(h *Handle) { slice.Sort(r, func(i, j Components[string]) bool { return i.Order > j.Order }) - return r + return r, true }) var s = make([]string, 0, len(ss)) for _, component := range ss { @@ -64,7 +64,9 @@ func CalComponents(h *Handle) { if component.Fn != nil { v := "" if component.Cached { - v = reload.GetAnyValMapBy("cacheComponents", component.Name, h, component.Fn) + v = reload.GetAnyValMapBy("cacheComponents", component.Name, h, func(a *Handle) (string, bool) { + return component.Fn(h), true + }) } else { v = component.Fn(h) } diff --git a/app/theme/wp/components/block/category.go b/app/theme/wp/components/block/category.go index 9211e9f..dc11774 100644 --- a/app/theme/wp/components/block/category.go +++ b/app/theme/wp/components/block/category.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" constraints2 "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" @@ -12,6 +11,7 @@ import ( "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp/components/widget" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/number" str "github.com/fthvgb1/wp-go/helper/strings" @@ -40,7 +40,7 @@ func categoryDefaultArgs() map[string]string { } } -func parseAttr(attr map[any]any) string { +func parseAttr(attr map[any]any) (string, bool) { var attrs []string class := maps.GetAnyAnyValWithDefaults(attr, "", "className") classes := strings.Split(class, " ") @@ -68,18 +68,18 @@ func parseAttr(attr map[any]any) string { attrs = append(attrs, fmt.Sprintf(`style="%s;"`, strings.Join(styles, ";"))) } attrs = append(attrs, fmt.Sprintf(`class="%s"`, strings.Join(classes, " "))) - return strings.Join(attrs, " ") + return strings.Join(attrs, " "), true } func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, error) { counter := number.Counters[int]() var err error - conf := reload.GetAnyValBys("block-category-conf", h, func(h *wp.Handle) map[any]any { + conf := reload.GetAnyValBy("block-category-conf", h, func(h *wp.Handle) (map[any]any, bool) { var con any err = json.Unmarshal([]byte(blockParser.Attrs), &con) if err != nil { logs.Error(err, "解析category attr错误", blockParser.Attrs) - return nil + return nil, false } var conf map[any]any switch con.(type) { @@ -111,8 +111,8 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, classes = append(classes, "wp-block-categories-list") conf["className"] = strings.Join(classes, " ") } - return conf - }) + return conf, true + }, 5) if err != nil { return nil, err @@ -127,9 +127,9 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") { h.C.Set("showOnlyTopLevel", true) } - args := reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("block-category-args", h, func(h *wp.Handle) (map[string]string, bool) { args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) - return maps.FilterZeroMerge(categoryDefaultArgs(), args) + return maps.FilterZeroMerge(categoryDefaultArgs(), args), true }) return func() string { diff --git a/app/theme/wp/components/widget/archive.go b/app/theme/wp/components/widget/archive.go index 08b03b6..d46a571 100644 --- a/app/theme/wp/components/widget/archive.go +++ b/app/theme/wp/components/widget/archive.go @@ -2,11 +2,11 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -42,7 +42,7 @@ var archivesConfig = map[any]any{ func Archive(h *wp.Handle, id string) string { conf := configs(archivesConfig, "widget_archives", int64(2)) - args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-archive-args", h, func(h *wp.Handle) (map[string]string, bool) { archiveArgs := archiveArgs() commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs()) args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs) @@ -53,7 +53,7 @@ func Archive(h *wp.Handle, id string) string { args["{$nav}"] = fmt.Sprintf(`" } - return args + return args, true }) s := archiveTemplate diff --git a/app/theme/wp/components/widget/category.go b/app/theme/wp/components/widget/category.go index 86318d9..1e8a24e 100644 --- a/app/theme/wp/components/widget/category.go +++ b/app/theme/wp/components/widget/category.go @@ -2,12 +2,12 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -48,7 +48,7 @@ func categoryArgs() map[string]string { func Category(h *wp.Handle, id string) string { conf := configs(categoryConfig, "widget_categories", int64(2)) - args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-category-args", h, func(h *wp.Handle) (map[string]string, bool) { commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs()) args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args) @@ -58,7 +58,7 @@ func Category(h *wp.Handle, id string) string { args["{$nav}"] = fmt.Sprintf(`" } - return args + return args, true }) t := categoryTemplate diff --git a/app/theme/wp/components/widget/fn.go b/app/theme/wp/components/widget/fn.go index 6781c8c..e74e147 100644 --- a/app/theme/wp/components/widget/fn.go +++ b/app/theme/wp/components/widget/fn.go @@ -1,9 +1,9 @@ package widget import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" ) @@ -15,8 +15,8 @@ func Fn(id string, fn func(*wp.Handle, string) string) func(h *wp.Handle) string } func configs[M ~map[K]V, K comparable, V any](m M, key string, a ...any) M { - return reload.GetAnyValBys(str.Join("widget-config-", key), key, func(_ string) M { + return reload.GetAnyValBys(str.Join("widget-config-", key), key, func(_ string) (M, bool) { c := wpconfig.GetPHPArrayVal[M](key, nil, a...) - return maps.FilterZeroMerge(maps.Copy(m), c) + return maps.FilterZeroMerge(maps.Copy(m), c), true }) } diff --git a/app/theme/wp/components/widget/meta.go b/app/theme/wp/components/widget/meta.go index d37110d..db5937a 100644 --- a/app/theme/wp/components/widget/meta.go +++ b/app/theme/wp/components/widget/meta.go @@ -2,10 +2,10 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -29,7 +29,7 @@ func metaArgs() map[string]string { } func Meta(h *wp.Handle, id string) string { - args := reload.GetAnyValBys("widget-meta-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-meta-args", h, func(h *wp.Handle) (map[string]string, bool) { commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) metaArgs := metaArgs() args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs) @@ -46,7 +46,7 @@ func Meta(h *wp.Handle, id string) string { args["{$nav}"] = fmt.Sprintf(`" } - return args + return args, true }) ss := str.NewBuilder() diff --git a/app/theme/wp/components/widget/recentcomments.go b/app/theme/wp/components/widget/recentcomments.go index 61a68e7..6ff4175 100644 --- a/app/theme/wp/components/widget/recentcomments.go +++ b/app/theme/wp/components/widget/recentcomments.go @@ -2,11 +2,11 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -42,7 +42,7 @@ var recentCommentsTemplate = `{$before_widget} func RecentComments(h *wp.Handle, id string) string { conf := configs(recentCommentConf, "widget_recent-comments", int64(2)) - args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-recent-comment-args", h, func(h *wp.Handle) (map[string]string, bool) { commentsArgs := recentCommentsArgs() commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.RecentComments, commentsArgs) @@ -53,7 +53,7 @@ func RecentComments(h *wp.Handle, id string) string { args["{$nav}"] = fmt.Sprintf(`" } - return args + return args, true }) comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string { diff --git a/app/theme/wp/components/widget/recentposts.go b/app/theme/wp/components/widget/recentposts.go index d348d8b..f897cce 100644 --- a/app/theme/wp/components/widget/recentposts.go +++ b/app/theme/wp/components/widget/recentposts.go @@ -2,13 +2,13 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -44,14 +44,14 @@ func recentConf() map[any]any { } func RecentPosts(h *wp.Handle, id string) string { - conf := reload.GetAnyValBys("widget-recent-posts-conf", h, func(h *wp.Handle) map[any]any { + conf := reload.GetAnyValBys("widget-recent-posts-conf", h, func(h *wp.Handle) (map[any]any, bool) { recent := recentConf() conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2)) conf = maps.FilterZeroMerge(recent, conf) - return conf + return conf, true }) - args := reload.GetAnyValBys("widget-recent-posts-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-recent-posts-args", h, func(h *wp.Handle) (map[string]string, bool) { recent := recentPostsArgs() commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent) @@ -62,7 +62,7 @@ func RecentPosts(h *wp.Handle, id string) string { args["{$nav}"] = fmt.Sprintf(`" } - return args + return args, true }) currentPostId := uint64(0) diff --git a/app/theme/wp/components/widget/search.go b/app/theme/wp/components/widget/search.go index 4b2ff9e..1a35bc6 100644 --- a/app/theme/wp/components/widget/search.go +++ b/app/theme/wp/components/widget/search.go @@ -2,11 +2,11 @@ package widget import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/html" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" @@ -48,7 +48,7 @@ func searchArgs() map[string]string { var form = html5SearchForm func Search(h *wp.Handle, id string) string { - args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) map[string]string { + args := reload.GetAnyValBys("widget-search-args", h, func(h *wp.Handle) (map[string]string, bool) { search := searchArgs() commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) args := wp.GetComponentsArgs(h, widgets.Search, search) @@ -69,7 +69,7 @@ func Search(h *wp.Handle, id string) string { form = xmlSearchForm } - return args + return args, true }) s := strings.ReplaceAll(searchTemplate, "{$form}", form) val := "" diff --git a/app/theme/wp/customheader.go b/app/theme/wp/customheader.go index 5b7fbd5..93c1312 100644 --- a/app/theme/wp/customheader.go +++ b/app/theme/wp/customheader.go @@ -3,11 +3,11 @@ package wp import ( "encoding/json" "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/model" @@ -20,14 +20,14 @@ func (h *Handle) DisplayHeaderText() bool { func (h *Handle) GetCustomHeaderImg() (r models.PostThumbnail, isRand bool) { var err error - img := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail { + img := reload.GetAnyValBy("headerImages", h.theme, func(theme string) ([]models.PostThumbnail, bool) { hs, er := h.GetHeaderImages(h.theme) if er != nil { err = er - return nil + return nil, false } - return hs - }) + return hs, true + }, 5) if err != nil { logs.Error(err, "获取页眉背景图失败") return diff --git a/app/theme/wp/customlogo.go b/app/theme/wp/customlogo.go index 881816d..ede13bd 100644 --- a/app/theme/wp/customlogo.go +++ b/app/theme/wp/customlogo.go @@ -2,9 +2,9 @@ package wp import ( "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" ) @@ -44,8 +44,8 @@ func CalCustomLogo(h *Handle) (r string) { func customLogo(h *Handle) func() string { return func() string { - return reload.GetAnyValBys("customLogo", h, func(h *Handle) string { - return CalCustomLogo(h) + return reload.GetAnyValBys("customLogo", h, func(h *Handle) (string, bool) { + return CalCustomLogo(h), true }) } } diff --git a/app/theme/wp/index.go b/app/theme/wp/index.go index c081527..9e98c30 100644 --- a/app/theme/wp/index.go +++ b/app/theme/wp/index.go @@ -4,11 +4,11 @@ import ( "database/sql" "errors" "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/plugins" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/model" @@ -141,8 +141,8 @@ func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) { func (i *IndexHandle) ExecPostsPlugin() { fn := i.postsPlugin if fn == nil { - fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) PostsPlugin { - return UsePostsPlugins() + fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) (PostsPlugin, bool) { + return UsePostsPlugins(), true }) } for j := range i.Posts { diff --git a/app/theme/wp/listpostplugins.go b/app/theme/wp/listpostplugins.go index 3b58a86..5656c30 100644 --- a/app/theme/wp/listpostplugins.go +++ b/app/theme/wp/listpostplugins.go @@ -1,11 +1,11 @@ package wp import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/plugins" "github.com/fthvgb1/wp-go/app/plugins/wpposts" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" ) diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index b0fd493..c8df26e 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -1,8 +1,8 @@ package wp import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/constraints" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" @@ -68,7 +68,7 @@ func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) { func BuildPipe(pipeScene string, keyFn func(*Handle, string) string, fn func(*Handle, map[string][]HandleCall, string) []HandleCall) func(HandleFn[*Handle], *Handle) { return func(next HandleFn[*Handle], h *Handle) { key := keyFn(h, pipeScene) - handlers := reload.GetAnyValMapBy("pipeHandlers", key, h, func(h *Handle) []HandleCall { + handlers := reload.GetAnyValMapBy("pipeHandlers", key, h, func(h *Handle) ([]HandleCall, bool) { conf := h.handleHook[pipeScene] calls := fn(h, h.handlers[pipeScene], key) calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { @@ -84,7 +84,7 @@ func BuildPipe(pipeScene string, keyFn func(*Handle, string) string, fn func(*Ha slice.Sort(calls, func(i, j HandleCall) bool { return i.Order > j.Order }) - return calls + return calls, true }) for _, handler := range handlers { handler.Fn(h) @@ -107,7 +107,7 @@ func Run(h *Handle, conf func(*Handle)) { if !helper.GetContextVal(h.C, "inited", false) { InitHandle(conf, h) } - reload.GetAnyValBys(str.Join("pipeInit-", h.scene), h, func(h *Handle) func(*Handle) { + reload.GetAnyValBys(str.Join("pipeInit-", h.scene), h, func(h *Handle) (func(*Handle), bool) { p := GetFn[Pipe]("pipe", constraints.AllScene) p = append(p, GetFn[Pipe]("pipe", h.scene)...) pipes := slice.FilterAndMap(p, func(pipe Pipe) (Pipe, bool) { @@ -128,7 +128,7 @@ func Run(h *Handle, conf func(*Handle)) { arr := slice.Map(pipes, func(t Pipe) HandlePipeFn[*Handle] { return t.Fn }) - return HandlePipe(NothingToDo, arr...) + return HandlePipe(NothingToDo, arr...), true })(h) } diff --git a/app/theme/wp/route/route.go b/app/theme/wp/route/route.go index 25a0f0e..5d7ed61 100644 --- a/app/theme/wp/route/route.go +++ b/app/theme/wp/route/route.go @@ -1,8 +1,8 @@ package route import ( - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/theme/wp" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/safety" "regexp" @@ -74,7 +74,7 @@ func ResolveRoute(h *wp.Handle) { requestURI := h.C.Request.RequestURI rs, rrs := reload.GetAnyValBys("route", struct{}{}, - func(_ struct{}) func() (map[string]Route, map[string]*regexp.Regexp) { + func(_ struct{}) (func() (map[string]Route, map[string]*regexp.Regexp), bool) { m := map[string]Route{} rrs := map[string]*regexp.Regexp{} routes.Range(func(key string, value Route) bool { @@ -82,19 +82,21 @@ func ResolveRoute(h *wp.Handle) { if len(routeHook) > 0 { for _, fn := range routeHook { v, ok := fn(value) - if ok { - m[v.Path] = v - if v.Type == "reg" { - if v.Path != key { - vvv, err := regexp.Compile(v.Path) - if err != nil { - panic(err) - } - vv = vvv - } - rrs[v.Path] = vv - } + if !ok { + continue } + m[v.Path] = v + if v.Type != "reg" { + continue + } + if v.Path != key { + vvv, err := regexp.Compile(v.Path) + if err != nil { + panic(err) + } + vv = vvv + } + rrs[v.Path] = vv } } else { m[key] = value @@ -105,7 +107,7 @@ func ResolveRoute(h *wp.Handle) { }) return func() (map[string]Route, map[string]*regexp.Regexp) { return m, rrs - } + }, true })() v, ok := rs[requestURI] if ok && slice.IsContained(v.Method, h.C.Request.Method) { diff --git a/app/theme/wp/stickyposts.go b/app/theme/wp/stickyposts.go index a828438..7f130b4 100644 --- a/app/theme/wp/stickyposts.go +++ b/app/theme/wp/stickyposts.go @@ -3,18 +3,18 @@ package wp import ( "fmt" "github.com/elliotchance/phpserialize" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/slice" str "github.com/fthvgb1/wp-go/helper/strings" ) func (h *Handle) StickPosts() []models.Posts { - return reload.GetAnyValBys("stickPostsSlice", h, func(h *Handle) (r []models.Posts) { + return reload.GetAnyValBys("stickPostsSlice", h, func(h *Handle) (r []models.Posts, ok bool) { v := wpconfig.GetOption("sticky_posts") if v == "" { return @@ -30,15 +30,16 @@ func (h *Handle) StickPosts() []models.Posts { post.IsSticky = true return post, err == nil }) + ok = true return }) } func (h *Handle) StickMapPosts() map[uint64]models.Posts { - return reload.GetAnyValBys("stickPostsMap", h, func(h *Handle) map[uint64]models.Posts { + return reload.GetAnyValBys("stickPostsMap", h, func(h *Handle) (map[uint64]models.Posts, bool) { return slice.SimpleToMap(h.StickPosts(), func(v models.Posts) uint64 { return v.Id - }) + }), true }) } diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index 9c823fd..63c71b8 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -2,12 +2,12 @@ package wp import ( "errors" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/plugins/wphandle/apply" "github.com/fthvgb1/wp-go/app/wpconfig" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/gin-contrib/sessions" @@ -100,7 +100,7 @@ type HandleCall struct { func InitHandle(fn func(*Handle), h *Handle) { var inited = false - hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) Handle { + hh := reload.GetAnyValBys("themeArgAndConfig", h, func(h *Handle) (Handle, bool) { h.components = make(map[string]map[string][]Components[string]) h.componentsArgs = make(map[string]any) h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string) @@ -117,7 +117,7 @@ func InitHandle(fn func(*Handle), h *Handle) { } h.C.Set("inited", true) inited = true - return *h + return *h, true }) h.ginH = maps.Copy(hh.ginH) h.ginH["calPostClass"] = postClass(h) diff --git a/app/wpconfig/thememods.go b/app/wpconfig/thememods.go index 90c0db8..2c073b7 100644 --- a/app/wpconfig/thememods.go +++ b/app/wpconfig/thememods.go @@ -3,10 +3,10 @@ package wpconfig import ( "embed" "fmt" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/app/phphelper" "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/models" + "github.com/fthvgb1/wp-go/cache/reload" "github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/safety" "path/filepath" diff --git a/cache/cachemanager/manger.go b/cache/cachemanager/manger.go index 3096d03..9d8c04f 100644 --- a/cache/cachemanager/manger.go +++ b/cache/cachemanager/manger.go @@ -3,8 +3,8 @@ package cachemanager import ( "context" "errors" - "github.com/fthvgb1/wp-go/app/cmd/reload" "github.com/fthvgb1/wp-go/cache" + "github.com/fthvgb1/wp-go/cache/reload" str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/safety" "time" diff --git a/app/cmd/reload/reload.go b/cache/reload/reload.go similarity index 88% rename from app/cmd/reload/reload.go rename to cache/reload/reload.go index e71bd48..3f99242 100644 --- a/app/cmd/reload/reload.go +++ b/cache/reload/reload.go @@ -1,6 +1,7 @@ package reload import ( + "github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/safety" @@ -63,14 +64,13 @@ func GetAnyMapFnBys[K comparable, V, A any](namespace string, fn func(A) V) func return v } m.mutex.Lock() + defer m.mutex.Unlock() v, ok = m.val.Load(key) if ok { - m.mutex.Unlock() return v } v = fn(a) m.val.Store(key, v) - m.mutex.Unlock() return v } } @@ -82,6 +82,7 @@ func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyM m = vv.(*safetyMap[K, V, A]) } else { safetyMapLock.Lock() + defer safetyMapLock.Unlock() vv, ok = safetyMaps.Load(namespace) if ok { m = vv.(*safetyMap[K, V, A]) @@ -102,26 +103,26 @@ func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyM }, ord, namespace) safetyMaps.Store(namespace, m) } - safetyMapLock.Unlock() } return m } -func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V, args ...any) V { +func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) (V, bool), args ...any) V { m := safetyMapFn[K, V, A](namespace, args...) v, ok := m.val.Load(key) if ok { return v } m.mutex.Lock() + defer m.mutex.Unlock() v, ok = m.val.Load(key) if ok { - m.mutex.Unlock() return v } - v = fn(a) - m.val.Store(key, v) - m.mutex.Unlock() + v, ok = fn(a) + if ok { + m.val.Store(key, v) + } return v } @@ -132,6 +133,7 @@ func anyVal[T, A any](namespace string, counter bool, args ...any) *safetyVar[T, vv = vvv.(*safetyVar[T, A]) } else { safetyMapLock.Lock() + defer safetyMapLock.Unlock() vvv, ok = safetyMaps.Load(namespace) if ok { vv = vvv.(*safetyVar[T, A]) @@ -147,12 +149,11 @@ func anyVal[T, A any](namespace string, counter bool, args ...any) *safetyVar[T, }, ord, namespace) safetyMaps.Store(namespace, vv) } - safetyMapLock.Unlock() } return vv } -func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, bool), args ...any) T { +func GetAnyValBy[T, A any](namespace string, a A, fn func(A) (T, bool), args ...any) T { var vv = anyVal[T, A](namespace, true, args...) var ok bool v := vv.Val.Load() @@ -160,37 +161,38 @@ func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, b return v.v } vv.mutex.Lock() + defer vv.mutex.Unlock() v = vv.Val.Load() if v.ok { - vv.mutex.Unlock() return v.v } v.v, ok = fn(a) + if v.counter == nil { + v.counter = number.Counters[int]() + } times := v.counter() + tryTimes := helper.ParseArgs(1, args...) if ok || times >= tryTimes { v.ok = true vv.Val.Store(v) } - vv.mutex.Unlock() return v.v } -func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T, args ...any) T { +func GetAnyValBys[T, A any](namespace string, a A, fn func(A) (T, bool), args ...any) T { var vv = anyVal[T, A](namespace, false, args...) v := vv.Val.Load() if v.ok { return v.v } vv.mutex.Lock() + defer vv.mutex.Unlock() v = vv.Val.Load() if v.ok { - vv.mutex.Unlock() return v.v } - v.v = fn(a) - v.ok = true + v.v, v.ok = fn(a) vv.Val.Store(v) - vv.mutex.Unlock() return v.v } @@ -253,7 +255,6 @@ func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] { func Push(fn func(), a ...any) { ord, name := parseArgs(a...) calls.Append(queue{fn, ord, name}) - //calls = append(calls, queue{fn, ord, name}) if name != "" { callsM.Store(name, fn) } @@ -261,7 +262,6 @@ func Push(fn func(), a ...any) { func Reload() { anyMap.Flush() - //safetyMaps.Flush() callsM.Flush() flushMapFn.Flush() callll := calls.Load() diff --git a/app/cmd/reload/reload_test.go b/cache/reload/reload_test.go similarity index 100% rename from app/cmd/reload/reload_test.go rename to cache/reload/reload_test.go diff --git a/helper/func.go b/helper/func.go index 2533388..ca6c73c 100644 --- a/helper/func.go +++ b/helper/func.go @@ -134,3 +134,13 @@ func GetAnyVal[T any](v any, defaults T) T { } return vv } + +func ParseArgs[T any](defaults T, a ...any) T { + for _, aa := range a { + v, ok := aa.(T) + if ok { + return v + } + } + return defaults +} diff --git a/safety/slice.go b/safety/slice.go index 98ac89b..6be8ee0 100644 --- a/safety/slice.go +++ b/safety/slice.go @@ -16,7 +16,8 @@ func NewSlice[T any](a []T) *Slice[T] { func (r *Slice[T]) Append(t ...T) { r.mu.Lock() + defer r.mu.Unlock() ts := append(r.Load(), t...) r.Store(ts) - r.mu.Unlock() + }