From 3f299e5a84936bccadaffd35434a2774e9a88574 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 25 Apr 2023 20:58:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmd/main.go | 2 +- internal/cmd/reload/reload.go | 49 +++++++++++++++---- internal/plugins/wphandle/handle.go | 13 +++-- internal/theme/twentyfifteen/twentyfifteen.go | 2 +- .../theme/twentyseventeen/twentyseventeen.go | 2 +- internal/theme/wp/pipe.go | 2 +- internal/theme/wp/wp.go | 2 +- 7 files changed, 54 insertions(+), 18 deletions(-) diff --git a/internal/cmd/main.go b/internal/cmd/main.go index a3c79f6..d69fc33 100644 --- a/internal/cmd/main.go +++ b/internal/cmd/main.go @@ -30,7 +30,7 @@ var intReg = regexp.MustCompile(`^\d`) func init() { flag.StringVar(&confPath, "c", "config.yaml", "config file") - flag.StringVar(&address, "p", "", "listen address(port)") + flag.StringVar(&address, "p", "", "listen address and port") flag.Parse() if address == "" && os.Getenv("PORT") == "" { address = "80" diff --git a/internal/cmd/reload/reload.go b/internal/cmd/reload/reload.go index 2fc2cb2..160a5b3 100644 --- a/internal/cmd/reload/reload.go +++ b/internal/cmd/reload/reload.go @@ -1,14 +1,13 @@ package reload import ( + "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/safety" "sync" ) var calls []func() -var str = safety.NewMap[string, string]() - var anyMap = safety.NewMap[string, any]() type safetyVar[T, A any] struct { @@ -16,8 +15,9 @@ type safetyVar[T, A any] struct { mutex sync.Mutex } type val[T any] struct { - v T - ok bool + v T + ok bool + counter number.Counter[int] } type safetyMap[K comparable, V, A any] struct { val *safety.Map[K, V] @@ -27,7 +27,7 @@ type safetyMap[K comparable, V, A any] struct { var safetyMaps = safety.NewMap[string, any]() var safetyMapLock = sync.Mutex{} -func SafetyMapByFn[K comparable, V, A any](namespace string, fn func(A) V) func(key K, args A) V { +func GetAnyMapFnBys[K comparable, V, A any](namespace string, fn func(A) V) func(key K, args A) V { m := safetyMapFn[K, V, A](namespace) return func(key K, a A) V { v, ok := m.val.Load(key) @@ -69,7 +69,7 @@ func safetyMapFn[K comparable, V, A any](namespace string) *safetyMap[K, V, A] { return m } -func SafetyMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V) V { +func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V) V { m := safetyMapFn[K, V, A](namespace) v, ok := m.val.Load(key) if ok { @@ -87,7 +87,7 @@ func SafetyMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A return v } -func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T) T { +func anyVal[T, A any](namespace string, counter bool) *safetyVar[T, A] { var vv *safetyVar[T, A] vvv, ok := safetyMaps.Load(namespace) if ok { @@ -98,7 +98,11 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T) T { if ok { vv = vvv.(*safetyVar[T, A]) } else { - vv = &safetyVar[T, A]{safety.NewVar(val[T]{}), sync.Mutex{}} + v := val[T]{} + if counter { + v.counter = number.Counters[int]() + } + vv = &safetyVar[T, A]{safety.NewVar(v), sync.Mutex{}} Push(func() { vv.Val.Flush() }) @@ -106,6 +110,34 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T) T { } safetyMapLock.Unlock() } + return vv +} + +func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, bool)) T { + var vv = anyVal[T, A](namespace, true) + var ok bool + v := vv.Val.Load() + if v.ok { + return v.v + } + vv.mutex.Lock() + v = vv.Val.Load() + if v.ok { + vv.mutex.Unlock() + return v.v + } + v.v, ok = fn(a) + times := v.counter() + 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) T { + var vv = anyVal[T, A](namespace, false) v := vv.Val.Load() if v.ok { return v.v @@ -147,6 +179,5 @@ func Reload() { call() } anyMap.Flush() - str.Flush() safetyMaps.Flush() } diff --git a/internal/plugins/wphandle/handle.go b/internal/plugins/wphandle/handle.go index 4ff447d..2833f07 100644 --- a/internal/plugins/wphandle/handle.go +++ b/internal/plugins/wphandle/handle.go @@ -1,7 +1,7 @@ package wphandle import ( - "github.com/fthvgb1/wp-go/helper/maps" + "github.com/fthvgb1/wp-go/internal/pkg/config" "github.com/fthvgb1/wp-go/internal/plugins/wphandle/enlightjs" "github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin" "github.com/fthvgb1/wp-go/internal/theme/wp" @@ -12,11 +12,16 @@ var plugins = wp.HandlePlugins{ "hiddenLogin": hiddenlogin.HiddenLogin, } -func Plugins() wp.HandlePlugins { - return maps.Copy(plugins) +func RegisterPlugins(m wp.HandlePlugins) { + for k, v := range m { + if _, ok := plugins[k]; !ok { + plugins[k] = v + } + } } -func RegisterPlugins(h *wp.Handle, calls ...string) { +func UsePlugins(h *wp.Handle, calls ...string) { + calls = append(calls, config.GetConfig().Plugins...) for _, call := range calls { if fn, ok := plugins[call]; ok { fn(h) diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index de5a0ae..22da18a 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -62,7 +62,7 @@ func configs(h *wp.Handle) { return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) }) h.Index.SetPageEle(plugins.TwentyFifteenPagination()) - wphandle.RegisterPlugins(h, conf.Plugins...) + wphandle.UsePlugins(h) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss) h.CommonComponents() h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...)) diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 5924878..4627982 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -53,7 +53,7 @@ func Hook(h *wp.Handle) { func configs(h *wp.Handle) { conf := config.GetConfig() - wphandle.RegisterPlugins(h, conf.Plugins...) + wphandle.UsePlugins(h) h.PushComponentFilterFn("bodyClass", calClass) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) components.WidgetArea(h) diff --git a/internal/theme/wp/pipe.go b/internal/theme/wp/pipe.go index 723eaad..c87b826 100644 --- a/internal/theme/wp/pipe.go +++ b/internal/theme/wp/pipe.go @@ -34,7 +34,7 @@ func (h *Handle) PushDataHandler(scene string, fns ...HandleCall) { func PipeHandle(pipeScene string, keyFn func(*Handle, string) string, fn func(*Handle, map[string][]HandleCall) []HandleCall) func(HandleFn[*Handle], *Handle) { return func(next HandleFn[*Handle], h *Handle) { - handlers := reload.SafetyMapBy("pipeHandlers", keyFn(h, pipeScene), h, func(h *Handle) []HandleCall { + handlers := reload.GetAnyValMapBy("pipeHandlers", keyFn(h, pipeScene), h, func(h *Handle) []HandleCall { conf := h.handleHook[pipeScene] calls := fn(h, h.handlers[pipeScene]) calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { diff --git a/internal/theme/wp/wp.go b/internal/theme/wp/wp.go index 936ed82..9e4be2d 100644 --- a/internal/theme/wp/wp.go +++ b/internal/theme/wp/wp.go @@ -341,7 +341,7 @@ func CalComponents(h *Handle) { if component.Fn != nil { v := "" if component.CacheKey != "" { - v = reload.SafetyMapBy("calComponent", component.CacheKey, h, component.Fn) + v = reload.GetAnyValMapBy("calComponent", component.CacheKey, h, component.Fn) } else { v = component.Fn(h) }