小优化

This commit is contained in:
xing 2023-04-25 20:58:22 +08:00
parent 4760f65b9b
commit 3f299e5a84
7 changed files with 54 additions and 18 deletions

View File

@ -30,7 +30,7 @@ var intReg = regexp.MustCompile(`^\d`)
func init() { func init() {
flag.StringVar(&confPath, "c", "config.yaml", "config file") 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() flag.Parse()
if address == "" && os.Getenv("PORT") == "" { if address == "" && os.Getenv("PORT") == "" {
address = "80" address = "80"

View File

@ -1,14 +1,13 @@
package reload package reload
import ( import (
"github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/safety" "github.com/fthvgb1/wp-go/safety"
"sync" "sync"
) )
var calls []func() var calls []func()
var str = safety.NewMap[string, string]()
var anyMap = safety.NewMap[string, any]() var anyMap = safety.NewMap[string, any]()
type safetyVar[T, A any] struct { type safetyVar[T, A any] struct {
@ -18,6 +17,7 @@ type safetyVar[T, A any] struct {
type val[T any] struct { type val[T any] struct {
v T v T
ok bool ok bool
counter number.Counter[int]
} }
type safetyMap[K comparable, V, A any] struct { type safetyMap[K comparable, V, A any] struct {
val *safety.Map[K, V] val *safety.Map[K, V]
@ -27,7 +27,7 @@ type safetyMap[K comparable, V, A any] struct {
var safetyMaps = safety.NewMap[string, any]() var safetyMaps = safety.NewMap[string, any]()
var safetyMapLock = sync.Mutex{} 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) m := safetyMapFn[K, V, A](namespace)
return func(key K, a A) V { return func(key K, a A) V {
v, ok := m.val.Load(key) 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 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) m := safetyMapFn[K, V, A](namespace)
v, ok := m.val.Load(key) v, ok := m.val.Load(key)
if ok { if ok {
@ -87,7 +87,7 @@ func SafetyMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A
return v 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] var vv *safetyVar[T, A]
vvv, ok := safetyMaps.Load(namespace) vvv, ok := safetyMaps.Load(namespace)
if ok { if ok {
@ -98,7 +98,11 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T) T {
if ok { if ok {
vv = vvv.(*safetyVar[T, A]) vv = vvv.(*safetyVar[T, A])
} else { } 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() { Push(func() {
vv.Val.Flush() vv.Val.Flush()
}) })
@ -106,6 +110,34 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T) T {
} }
safetyMapLock.Unlock() 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() v := vv.Val.Load()
if v.ok { if v.ok {
return v.v return v.v
@ -147,6 +179,5 @@ func Reload() {
call() call()
} }
anyMap.Flush() anyMap.Flush()
str.Flush()
safetyMaps.Flush() safetyMaps.Flush()
} }

View File

@ -1,7 +1,7 @@
package wphandle package wphandle
import ( 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/enlightjs"
"github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin" "github.com/fthvgb1/wp-go/internal/plugins/wphandle/hiddenlogin"
"github.com/fthvgb1/wp-go/internal/theme/wp" "github.com/fthvgb1/wp-go/internal/theme/wp"
@ -12,11 +12,16 @@ var plugins = wp.HandlePlugins{
"hiddenLogin": hiddenlogin.HiddenLogin, "hiddenLogin": hiddenlogin.HiddenLogin,
} }
func Plugins() wp.HandlePlugins { func RegisterPlugins(m wp.HandlePlugins) {
return maps.Copy(plugins) 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 { for _, call := range calls {
if fn, ok := plugins[call]; ok { if fn, ok := plugins[call]; ok {
fn(h) fn(h)

View File

@ -62,7 +62,7 @@ func configs(h *wp.Handle) {
return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`) return strings.ReplaceAll(s, `class="search-submit"`, `class="search-submit screen-reader-text"`)
}) })
h.Index.SetPageEle(plugins.TwentyFifteenPagination()) h.Index.SetPageEle(plugins.TwentyFifteenPagination())
wphandle.RegisterPlugins(h, conf.Plugins...) wphandle.UsePlugins(h)
h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss) h.PushCacheGroupHeadScript("CalCustomBackGround", 10, CalCustomBackGround, colorSchemeCss)
h.CommonComponents() h.CommonComponents()
h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...)) h.Index.SetListPlugin(wp.PostsPlugins(wp.PostPlugin(), wp.GetListPostPlugins(conf.ListPagePlugins, wp.ListPostPlugins())...))

View File

@ -53,7 +53,7 @@ func Hook(h *wp.Handle) {
func configs(h *wp.Handle) { func configs(h *wp.Handle) {
conf := config.GetConfig() conf := config.GetConfig()
wphandle.RegisterPlugins(h, conf.Plugins...) wphandle.UsePlugins(h)
h.PushComponentFilterFn("bodyClass", calClass) h.PushComponentFilterFn("bodyClass", calClass)
h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader) h.PushCacheGroupHeadScript("colorScheme-customHeader", 10, colorScheme, customHeader)
components.WidgetArea(h) components.WidgetArea(h)

View File

@ -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) { 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) { 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] conf := h.handleHook[pipeScene]
calls := fn(h, h.handlers[pipeScene]) calls := fn(h, h.handlers[pipeScene])
calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) { calls = slice.FilterAndMap(calls, func(call HandleCall) (HandleCall, bool) {

View File

@ -341,7 +341,7 @@ func CalComponents(h *Handle) {
if component.Fn != nil { if component.Fn != nil {
v := "" v := ""
if component.CacheKey != "" { if component.CacheKey != "" {
v = reload.SafetyMapBy("calComponent", component.CacheKey, h, component.Fn) v = reload.GetAnyValMapBy("calComponent", component.CacheKey, h, component.Fn)
} else { } else {
v = component.Fn(h) v = component.Fn(h)
} }