小优化

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() {
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"

View File

@ -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()
}

View File

@ -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)

View File

@ -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())...))

View File

@ -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)

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) {
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) {

View File

@ -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)
}