revise reload package

This commit is contained in:
xing 2023-11-12 21:39:04 +08:00
parent 7978e9ee74
commit a5294e2e20
29 changed files with 121 additions and 105 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/cmd/route" "github.com/fthvgb1/wp-go/app/cmd/route"
"github.com/fthvgb1/wp-go/app/mail" "github.com/fthvgb1/wp-go/app/mail"
"github.com/fthvgb1/wp-go/app/pkg/cache" "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/theme"
"github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/app/wpconfig"
"github.com/fthvgb1/wp-go/cache/cachemanager" "github.com/fthvgb1/wp-go/cache/cachemanager"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
"log" "log"
"os" "os"

View File

@ -2,12 +2,12 @@ package route
import ( import (
"github.com/fthvgb1/wp-go/app/actions" "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/middleware"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/theme" "github.com/fthvgb1/wp-go/app/theme"
"github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/app/wpconfig"
"github.com/fthvgb1/wp-go/cache/reload"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof" "github.com/gin-contrib/pprof"

View File

@ -1,8 +1,8 @@
package middleware package middleware
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )

View File

@ -1,8 +1,8 @@
package middleware package middleware
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"

View File

@ -1,9 +1,9 @@
package twentyfifteen package twentyfifteen
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/cache/reload"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
) )

View File

@ -2,7 +2,6 @@ package twentyseventeen
import ( import (
"fmt" "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"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/logs" "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"
"github.com/fthvgb1/wp-go/app/theme/wp/components/widget" "github.com/fthvgb1/wp-go/app/theme/wp/components/widget"
"github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/app/wpconfig"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper" "github.com/fthvgb1/wp-go/helper"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"

View File

@ -1,8 +1,8 @@
package wp package wp
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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) { 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) { return maps.MergeBy(func(k string, v1, v2 []Components[string]) ([]Components[string], bool) {
vv := append(v1, v2...) vv := append(v1, v2...)
return vv, vv != nil 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 { for k, components := range componentss {
key := str.Join("calComponents-", h.scene, "-", k) 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) { r := slice.FilterAndMap(components, func(t Components[string]) (Components[string], bool) {
fns, ok := h.componentHook[k] fns, ok := h.componentHook[k]
if !ok { if !ok {
@ -53,7 +53,7 @@ func CalComponents(h *Handle) {
slice.Sort(r, func(i, j Components[string]) bool { slice.Sort(r, func(i, j Components[string]) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
return r return r, true
}) })
var s = make([]string, 0, len(ss)) var s = make([]string, 0, len(ss))
for _, component := range ss { for _, component := range ss {
@ -64,7 +64,9 @@ func CalComponents(h *Handle) {
if component.Fn != nil { if component.Fn != nil {
v := "" v := ""
if component.Cached { 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 { } else {
v = component.Fn(h) v = component.Fn(h)
} }

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
constraints2 "github.com/fthvgb1/wp-go/app/pkg/constraints" constraints2 "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "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/pkg/models"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/theme/wp/components/widget" "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/maps"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
str "github.com/fthvgb1/wp-go/helper/strings" 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 var attrs []string
class := maps.GetAnyAnyValWithDefaults(attr, "", "className") class := maps.GetAnyAnyValWithDefaults(attr, "", "className")
classes := strings.Split(class, " ") 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(`style="%s;"`, strings.Join(styles, ";")))
} }
attrs = append(attrs, fmt.Sprintf(`class="%s"`, strings.Join(classes, " "))) 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) { func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string, error) {
counter := number.Counters[int]() counter := number.Counters[int]()
var err error 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 var con any
err = json.Unmarshal([]byte(blockParser.Attrs), &con) err = json.Unmarshal([]byte(blockParser.Attrs), &con)
if err != nil { if err != nil {
logs.Error(err, "解析category attr错误", blockParser.Attrs) logs.Error(err, "解析category attr错误", blockParser.Attrs)
return nil return nil, false
} }
var conf map[any]any var conf map[any]any
switch con.(type) { 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") classes = append(classes, "wp-block-categories-list")
conf["className"] = strings.Join(classes, " ") conf["className"] = strings.Join(classes, " ")
} }
return conf return conf, true
}) }, 5)
if err != nil { if err != nil {
return nil, err return nil, err
@ -127,9 +127,9 @@ func Category(h *wp.Handle, id string, blockParser ParserBlock) (func() string,
if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") { if maps.GetAnyAnyValWithDefaults(conf, false, "showOnlyTopLevel") {
h.C.Set("showOnlyTopLevel", true) 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{}) args := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
return maps.FilterZeroMerge(categoryDefaultArgs(), args) return maps.FilterZeroMerge(categoryDefaultArgs(), args), true
}) })
return func() string { return func() string {

View File

@ -2,11 +2,11 @@ package widget
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/theme/wp" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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 { func Archive(h *wp.Handle, id string) string {
conf := configs(archivesConfig, "widget_archives", int64(2)) 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() archiveArgs := archiveArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs()) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, CommonArgs())
args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs) args := wp.GetComponentsArgs(h, widgets.Archive, archiveArgs)
@ -53,7 +53,7 @@ func Archive(h *wp.Handle, id string) string {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string)) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"].(string))
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
return args return args, true
}) })
s := archiveTemplate s := archiveTemplate

View File

@ -2,12 +2,12 @@ package widget
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/theme/wp" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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 { func Category(h *wp.Handle, id string) string {
conf := configs(categoryConfig, "widget_categories", int64(2)) 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{}) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs()) args := wp.GetComponentsArgs(h, widgets.Categories, categoryArgs())
args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args) args = maps.FilterZeroMerge(categoryArgs(), CommonArgs(), commonArgs, args)
@ -58,7 +58,7 @@ func Category(h *wp.Handle, id string) string {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"]) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{title}"])
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
return args return args, true
}) })
t := categoryTemplate t := categoryTemplate

View File

@ -1,9 +1,9 @@
package widget package widget
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
str "github.com/fthvgb1/wp-go/helper/strings" 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 { 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...) c := wpconfig.GetPHPArrayVal[M](key, nil, a...)
return maps.FilterZeroMerge(maps.Copy(m), c) return maps.FilterZeroMerge(maps.Copy(m), c), true
}) })
} }

View File

@ -2,10 +2,10 @@ package widget
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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 { 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{}) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
metaArgs := metaArgs() metaArgs := metaArgs()
args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs) args := wp.GetComponentsArgs(h, widgets.Meta, metaArgs)
@ -46,7 +46,7 @@ func Meta(h *wp.Handle, id string) string {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{$title}"]) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, args["{$title}"])
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
return args return args, true
}) })
ss := str.NewBuilder() ss := str.NewBuilder()

View File

@ -2,11 +2,11 @@ package widget
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/theme/wp" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
@ -42,7 +42,7 @@ var recentCommentsTemplate = `{$before_widget}
func RecentComments(h *wp.Handle, id string) string { func RecentComments(h *wp.Handle, id string) string {
conf := configs(recentCommentConf, "widget_recent-comments", int64(2)) 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() commentsArgs := recentCommentsArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, widgets.RecentComments, commentsArgs) args := wp.GetComponentsArgs(h, widgets.RecentComments, commentsArgs)
@ -53,7 +53,7 @@ func RecentComments(h *wp.Handle, id string) string {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"]) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
return args return args, true
}) })
comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string { comments := slice.Map(cache.RecentComments(h.C, int(conf["number"].(int64))), func(t models.Comments) string {

View File

@ -2,13 +2,13 @@ package widget
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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 { 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() recent := recentConf()
conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2)) conf := wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recent, int64(2))
conf = maps.FilterZeroMerge(recent, conf) 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() recent := recentPostsArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent) args := wp.GetComponentsArgs(h, widgets.RecentPosts, recent)
@ -62,7 +62,7 @@ func RecentPosts(h *wp.Handle, id string) string {
args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"]) args["{$nav}"] = fmt.Sprintf(`<nav aria-label="%s">`, conf["title"])
args["{$navCloser}"] = "</nav>" args["{$navCloser}"] = "</nav>"
} }
return args return args, true
}) })
currentPostId := uint64(0) currentPostId := uint64(0)

View File

@ -2,11 +2,11 @@ package widget
import ( import (
"fmt" "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"
"github.com/fthvgb1/wp-go/app/pkg/constraints/widgets" "github.com/fthvgb1/wp-go/app/pkg/constraints/widgets"
"github.com/fthvgb1/wp-go/app/theme/wp" "github.com/fthvgb1/wp-go/app/theme/wp"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/html"
"github.com/fthvgb1/wp-go/helper/maps" "github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
@ -48,7 +48,7 @@ func searchArgs() map[string]string {
var form = html5SearchForm var form = html5SearchForm
func Search(h *wp.Handle, id string) string { 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() search := searchArgs()
commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{}) commonArgs := wp.GetComponentsArgs(h, widgets.Widget, map[string]string{})
args := wp.GetComponentsArgs(h, widgets.Search, search) args := wp.GetComponentsArgs(h, widgets.Search, search)
@ -69,7 +69,7 @@ func Search(h *wp.Handle, id string) string {
form = xmlSearchForm form = xmlSearchForm
} }
return args return args, true
}) })
s := strings.ReplaceAll(searchTemplate, "{$form}", form) s := strings.ReplaceAll(searchTemplate, "{$form}", form)
val := "" val := ""

View File

@ -3,11 +3,11 @@ package wp
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig" "github.com/fthvgb1/wp-go/app/wpconfig"
"github.com/fthvgb1/wp-go/cache/reload"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
@ -20,14 +20,14 @@ func (h *Handle) DisplayHeaderText() bool {
func (h *Handle) GetCustomHeaderImg() (r models.PostThumbnail, isRand bool) { func (h *Handle) GetCustomHeaderImg() (r models.PostThumbnail, isRand bool) {
var err error 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) hs, er := h.GetHeaderImages(h.theme)
if er != nil { if er != nil {
err = er err = er
return nil return nil, false
} }
return hs return hs, true
}) }, 5)
if err != nil { if err != nil {
logs.Error(err, "获取页眉背景图失败") logs.Error(err, "获取页眉背景图失败")
return return

View File

@ -2,9 +2,9 @@ package wp
import ( import (
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
) )
@ -44,8 +44,8 @@ func CalCustomLogo(h *Handle) (r string) {
func customLogo(h *Handle) func() string { func customLogo(h *Handle) func() string {
return func() string { return func() string {
return reload.GetAnyValBys("customLogo", h, func(h *Handle) string { return reload.GetAnyValBys("customLogo", h, func(h *Handle) (string, bool) {
return CalCustomLogo(h) return CalCustomLogo(h), true
}) })
} }
} }

View File

@ -4,11 +4,11 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/cache" "github.com/fthvgb1/wp-go/app/pkg/cache"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins" "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/number"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/model" "github.com/fthvgb1/wp-go/model"
@ -141,8 +141,8 @@ func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
func (i *IndexHandle) ExecPostsPlugin() { func (i *IndexHandle) ExecPostsPlugin() {
fn := i.postsPlugin fn := i.postsPlugin
if fn == nil { if fn == nil {
fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) PostsPlugin { fn = reload.GetAnyValBys("postPlugins", i, func(a *IndexHandle) (PostsPlugin, bool) {
return UsePostsPlugins() return UsePostsPlugins(), true
}) })
} }
for j := range i.Posts { for j := range i.Posts {

View File

@ -1,11 +1,11 @@
package wp package wp
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/plugins" "github.com/fthvgb1/wp-go/app/plugins"
"github.com/fthvgb1/wp-go/app/plugins/wpposts" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
) )

View File

@ -1,8 +1,8 @@
package wp package wp
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "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"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" 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) { 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) { return func(next HandleFn[*Handle], h *Handle) {
key := keyFn(h, pipeScene) 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] conf := h.handleHook[pipeScene]
calls := fn(h, h.handlers[pipeScene], key) calls := fn(h, h.handlers[pipeScene], key)
calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { 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 { slice.Sort(calls, func(i, j HandleCall) bool {
return i.Order > j.Order return i.Order > j.Order
}) })
return calls return calls, true
}) })
for _, handler := range handlers { for _, handler := range handlers {
handler.Fn(h) handler.Fn(h)
@ -107,7 +107,7 @@ func Run(h *Handle, conf func(*Handle)) {
if !helper.GetContextVal(h.C, "inited", false) { if !helper.GetContextVal(h.C, "inited", false) {
InitHandle(conf, h) 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 := GetFn[Pipe]("pipe", constraints.AllScene)
p = append(p, GetFn[Pipe]("pipe", h.scene)...) p = append(p, GetFn[Pipe]("pipe", h.scene)...)
pipes := slice.FilterAndMap(p, func(pipe Pipe) (Pipe, bool) { 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] { arr := slice.Map(pipes, func(t Pipe) HandlePipeFn[*Handle] {
return t.Fn return t.Fn
}) })
return HandlePipe(NothingToDo, arr...) return HandlePipe(NothingToDo, arr...), true
})(h) })(h)
} }

View File

@ -1,8 +1,8 @@
package route package route
import ( import (
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/theme/wp" "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/helper/slice"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"regexp" "regexp"
@ -74,7 +74,7 @@ func ResolveRoute(h *wp.Handle) {
requestURI := h.C.Request.RequestURI requestURI := h.C.Request.RequestURI
rs, rrs := reload.GetAnyValBys("route", rs, rrs := reload.GetAnyValBys("route",
struct{}{}, 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{} m := map[string]Route{}
rrs := map[string]*regexp.Regexp{} rrs := map[string]*regexp.Regexp{}
routes.Range(func(key string, value Route) bool { routes.Range(func(key string, value Route) bool {
@ -82,9 +82,13 @@ func ResolveRoute(h *wp.Handle) {
if len(routeHook) > 0 { if len(routeHook) > 0 {
for _, fn := range routeHook { for _, fn := range routeHook {
v, ok := fn(value) v, ok := fn(value)
if ok { if !ok {
continue
}
m[v.Path] = v m[v.Path] = v
if v.Type == "reg" { if v.Type != "reg" {
continue
}
if v.Path != key { if v.Path != key {
vvv, err := regexp.Compile(v.Path) vvv, err := regexp.Compile(v.Path)
if err != nil { if err != nil {
@ -94,8 +98,6 @@ func ResolveRoute(h *wp.Handle) {
} }
rrs[v.Path] = vv rrs[v.Path] = vv
} }
}
}
} else { } else {
m[key] = value m[key] = value
rrs[key] = vv rrs[key] = vv
@ -105,7 +107,7 @@ func ResolveRoute(h *wp.Handle) {
}) })
return func() (map[string]Route, map[string]*regexp.Regexp) { return func() (map[string]Route, map[string]*regexp.Regexp) {
return m, rrs return m, rrs
} }, true
})() })()
v, ok := rs[requestURI] v, ok := rs[requestURI]
if ok && slice.IsContained(v.Method, h.C.Request.Method) { if ok && slice.IsContained(v.Method, h.C.Request.Method) {

View File

@ -3,18 +3,18 @@ package wp
import ( import (
"fmt" "fmt"
"github.com/elliotchance/phpserialize" "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/cache"
"github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/app/pkg/models"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
) )
func (h *Handle) StickPosts() []models.Posts { 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") v := wpconfig.GetOption("sticky_posts")
if v == "" { if v == "" {
return return
@ -30,15 +30,16 @@ func (h *Handle) StickPosts() []models.Posts {
post.IsSticky = true post.IsSticky = true
return post, err == nil return post, err == nil
}) })
ok = true
return return
}) })
} }
func (h *Handle) StickMapPosts() map[uint64]models.Posts { 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 slice.SimpleToMap(h.StickPosts(), func(v models.Posts) uint64 {
return v.Id return v.Id
}) }), true
}) })
} }

View File

@ -2,12 +2,12 @@ package wp
import ( import (
"errors" "errors"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/pkg/config" "github.com/fthvgb1/wp-go/app/pkg/config"
"github.com/fthvgb1/wp-go/app/pkg/constraints" "github.com/fthvgb1/wp-go/app/pkg/constraints"
"github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/plugins/wphandle/apply" "github.com/fthvgb1/wp-go/app/plugins/wphandle/apply"
"github.com/fthvgb1/wp-go/app/wpconfig" "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/maps"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions"
@ -100,7 +100,7 @@ type HandleCall struct {
func InitHandle(fn func(*Handle), h *Handle) { func InitHandle(fn func(*Handle), h *Handle) {
var inited = false 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.components = make(map[string]map[string][]Components[string])
h.componentsArgs = make(map[string]any) h.componentsArgs = make(map[string]any)
h.componentFilterFn = make(map[string][]func(*Handle, string, ...any) string) 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) h.C.Set("inited", true)
inited = true inited = true
return *h return *h, true
}) })
h.ginH = maps.Copy(hh.ginH) h.ginH = maps.Copy(hh.ginH)
h.ginH["calPostClass"] = postClass(h) h.ginH["calPostClass"] = postClass(h)

View File

@ -3,10 +3,10 @@ package wpconfig
import ( import (
"embed" "embed"
"fmt" "fmt"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/app/phphelper" "github.com/fthvgb1/wp-go/app/phphelper"
"github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/logs"
"github.com/fthvgb1/wp-go/app/pkg/models" "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/helper/maps"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"path/filepath" "path/filepath"

View File

@ -3,8 +3,8 @@ package cachemanager
import ( import (
"context" "context"
"errors" "errors"
"github.com/fthvgb1/wp-go/app/cmd/reload"
"github.com/fthvgb1/wp-go/cache" "github.com/fthvgb1/wp-go/cache"
"github.com/fthvgb1/wp-go/cache/reload"
str "github.com/fthvgb1/wp-go/helper/strings" str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"time" "time"

View File

@ -1,6 +1,7 @@
package reload package reload
import ( import (
"github.com/fthvgb1/wp-go/helper"
"github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice" "github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/safety" "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 return v
} }
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock()
v, ok = m.val.Load(key) v, ok = m.val.Load(key)
if ok { if ok {
m.mutex.Unlock()
return v return v
} }
v = fn(a) v = fn(a)
m.val.Store(key, v) m.val.Store(key, v)
m.mutex.Unlock()
return v return v
} }
} }
@ -82,6 +82,7 @@ func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyM
m = vv.(*safetyMap[K, V, A]) m = vv.(*safetyMap[K, V, A])
} else { } else {
safetyMapLock.Lock() safetyMapLock.Lock()
defer safetyMapLock.Unlock()
vv, ok = safetyMaps.Load(namespace) vv, ok = safetyMaps.Load(namespace)
if ok { if ok {
m = vv.(*safetyMap[K, V, A]) m = vv.(*safetyMap[K, V, A])
@ -102,26 +103,26 @@ func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyM
}, ord, namespace) }, ord, namespace)
safetyMaps.Store(namespace, m) safetyMaps.Store(namespace, m)
} }
safetyMapLock.Unlock()
} }
return m 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...) m := safetyMapFn[K, V, A](namespace, args...)
v, ok := m.val.Load(key) v, ok := m.val.Load(key)
if ok { if ok {
return v return v
} }
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock()
v, ok = m.val.Load(key) v, ok = m.val.Load(key)
if ok { if ok {
m.mutex.Unlock()
return v return v
} }
v = fn(a) v, ok = fn(a)
if ok {
m.val.Store(key, v) m.val.Store(key, v)
m.mutex.Unlock() }
return 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]) vv = vvv.(*safetyVar[T, A])
} else { } else {
safetyMapLock.Lock() safetyMapLock.Lock()
defer safetyMapLock.Unlock()
vvv, ok = safetyMaps.Load(namespace) vvv, ok = safetyMaps.Load(namespace)
if ok { if ok {
vv = vvv.(*safetyVar[T, A]) vv = vvv.(*safetyVar[T, A])
@ -147,12 +149,11 @@ func anyVal[T, A any](namespace string, counter bool, args ...any) *safetyVar[T,
}, ord, namespace) }, ord, namespace)
safetyMaps.Store(namespace, vv) safetyMaps.Store(namespace, vv)
} }
safetyMapLock.Unlock()
} }
return vv 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 vv = anyVal[T, A](namespace, true, args...)
var ok bool var ok bool
v := vv.Val.Load() 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 return v.v
} }
vv.mutex.Lock() vv.mutex.Lock()
defer vv.mutex.Unlock()
v = vv.Val.Load() v = vv.Val.Load()
if v.ok { if v.ok {
vv.mutex.Unlock()
return v.v return v.v
} }
v.v, ok = fn(a) v.v, ok = fn(a)
if v.counter == nil {
v.counter = number.Counters[int]()
}
times := v.counter() times := v.counter()
tryTimes := helper.ParseArgs(1, args...)
if ok || times >= tryTimes { if ok || times >= tryTimes {
v.ok = true v.ok = true
vv.Val.Store(v) vv.Val.Store(v)
} }
vv.mutex.Unlock()
return v.v 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...) var vv = anyVal[T, A](namespace, false, args...)
v := vv.Val.Load() v := vv.Val.Load()
if v.ok { if v.ok {
return v.v return v.v
} }
vv.mutex.Lock() vv.mutex.Lock()
defer vv.mutex.Unlock()
v = vv.Val.Load() v = vv.Val.Load()
if v.ok { if v.ok {
vv.mutex.Unlock()
return v.v return v.v
} }
v.v = fn(a) v.v, v.ok = fn(a)
v.ok = true
vv.Val.Store(v) vv.Val.Store(v)
vv.mutex.Unlock()
return v.v 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) { func Push(fn func(), a ...any) {
ord, name := parseArgs(a...) ord, name := parseArgs(a...)
calls.Append(queue{fn, ord, name}) calls.Append(queue{fn, ord, name})
//calls = append(calls, queue{fn, ord, name})
if name != "" { if name != "" {
callsM.Store(name, fn) callsM.Store(name, fn)
} }
@ -261,7 +262,6 @@ func Push(fn func(), a ...any) {
func Reload() { func Reload() {
anyMap.Flush() anyMap.Flush()
//safetyMaps.Flush()
callsM.Flush() callsM.Flush()
flushMapFn.Flush() flushMapFn.Flush()
callll := calls.Load() callll := calls.Load()

View File

@ -134,3 +134,13 @@ func GetAnyVal[T any](v any, defaults T) T {
} }
return vv 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
}

View File

@ -16,7 +16,8 @@ func NewSlice[T any](a []T) *Slice[T] {
func (r *Slice[T]) Append(t ...T) { func (r *Slice[T]) Append(t ...T) {
r.mu.Lock() r.mu.Lock()
defer r.mu.Unlock()
ts := append(r.Load(), t...) ts := append(r.Load(), t...)
r.Store(ts) r.Store(ts)
r.mu.Unlock()
} }