diff --git a/app/middleware/searchlimit.go b/app/middleware/searchlimit.go index eea1f2a..8c6257b 100644 --- a/app/middleware/searchlimit.go +++ b/app/middleware/searchlimit.go @@ -8,9 +8,9 @@ import ( func SearchLimit(num int64) func(ctx *gin.Context) { fn, reFn := IpLimit(num) - reload.Push(func() { + reload.Append(func() { reFn(config.GetConfig().SingleIpSearchNum) - }) + }, "search-ip-limit-number") return func(c *gin.Context) { if c.Query("s") != "" { fn(c) diff --git a/app/middleware/validateservername.go b/app/middleware/validateservername.go index 6cde608..d00243f 100644 --- a/app/middleware/validateservername.go +++ b/app/middleware/validateservername.go @@ -19,7 +19,7 @@ func ValidateServerNames() func(ctx *gin.Context) { } } return m - }) + }, "site-names") return func(c *gin.Context) { m := sites.Load() diff --git a/app/pkg/cache/cache.go b/app/pkg/cache/cache.go index 891ddc8..f88644f 100644 --- a/app/pkg/cache/cache.go +++ b/app/pkg/cache/cache.go @@ -7,7 +7,7 @@ import ( "github.com/fthvgb1/wp-go/app/pkg/logs" "github.com/fthvgb1/wp-go/app/pkg/models" "github.com/fthvgb1/wp-go/cache/cachemanager" - "github.com/fthvgb1/wp-go/safety" + "github.com/fthvgb1/wp-go/cache/reload" "time" ) @@ -102,9 +102,9 @@ type Arch struct { month time.Month } -var arch = safety.NewVar(Arch{ +var arch = reload.Vars(Arch{ fn: dao.Archives, -}) +}, "archives-year-month-data") func Archives(ctx context.Context) []models.PostArchive { a := arch.Load() diff --git a/app/route/route.go b/app/route/route.go index 14cdbb1..575dac8 100644 --- a/app/route/route.go +++ b/app/route/route.go @@ -91,9 +91,9 @@ func SetupRouter() *gin.Engine { fn(r) } - reload.Push(func() { + reload.Append(func() { c := config.GetConfig() siteFlow(c.MaxRequestSleepNum, c.MaxRequestNum, c.CacheTime.SleepTime) - }) + }, "site-flowLimit-config") return r } diff --git a/app/theme/templateFuncs.go b/app/theme/templateFuncs.go index e7529f2..22e7398 100644 --- a/app/theme/templateFuncs.go +++ b/app/theme/templateFuncs.go @@ -7,40 +7,31 @@ import ( "time" ) -var comFn = template.FuncMap{ - "unescaped": func(s string) any { - return template.HTML(s) - }, - "dateCh": func(t time.Time) any { - return t.Format("2006年 01月 02日") - }, - "timeFormat": func(t time.Time, format string) any { - return t.Format(format) - }, - "getOption": func(k string) string { - return wpconfig.GetOption(k) - }, - "getLang": wpconfig.GetLang, - "postsFn": postsFn, - "exec": func(fn func() string) template.HTML { - return template.HTML(fn()) - }, - "callFuncString": func(fn func(string) string, s string) template.HTML { - return template.HTML(fn(s)) - }, -} - func postsFn(fn func(models.Posts) string, a models.Posts) string { return fn(a) } func FuncMap() template.FuncMap { - return comFn -} - -func addTemplateFunc(fnName string, fn any) { - if _, ok := comFn[fnName]; ok { - panic("exists same name func") + return template.FuncMap{ + "unescaped": func(s string) any { + return template.HTML(s) + }, + "dateCh": func(t time.Time) any { + return t.Format("2006年 01月 02日") + }, + "timeFormat": func(t time.Time, format string) any { + return t.Format(format) + }, + "getOption": func(k string) string { + return wpconfig.GetOption(k) + }, + "getLang": wpconfig.GetLang, + "postsFn": postsFn, + "exec": func(fn func() string) template.HTML { + return template.HTML(fn()) + }, + "callFuncString": func(fn func(string) string, s string) template.HTML { + return template.HTML(fn(s)) + }, } - comFn[fnName] = fn } diff --git a/app/theme/twentyfifteen/customheader.go b/app/theme/twentyfifteen/customheader.go index e60bdb7..b16e2d5 100644 --- a/app/theme/twentyfifteen/customheader.go +++ b/app/theme/twentyfifteen/customheader.go @@ -81,7 +81,7 @@ var imgStyle = `.site-header { } }` -var header = reload.Vars(constraints.Defaults) +var header = reload.Vars(constraints.Defaults, "twentyfifteen-customheader") func calCustomHeaderImg(h *wp.Handle) (r string, rand bool) { img, rand := h.GetCustomHeaderImg() diff --git a/app/theme/twentyseventeen/twentyseventeen.go b/app/theme/twentyseventeen/twentyseventeen.go index 74e3785..7705cd2 100644 --- a/app/theme/twentyseventeen/twentyseventeen.go +++ b/app/theme/twentyseventeen/twentyseventeen.go @@ -162,7 +162,7 @@ func postThumbnail(h *wp.Handle, posts *models.Posts) { } } -var header = reload.Vars(models.PostThumbnail{}) +var header = reload.Vars(models.PostThumbnail{}, "twentyseventeen-headerImage") func calCustomHeader(h *wp.Handle) { h.SetData("HeaderImage", getHeaderImage(h)) diff --git a/app/theme/wp/listpostplugins.go b/app/theme/wp/listpostplugins.go index c9fa4e9..2ac7cf9 100644 --- a/app/theme/wp/listpostplugins.go +++ b/app/theme/wp/listpostplugins.go @@ -23,7 +23,7 @@ func PostsPlugins(initial PostsPlugin, calls ...func(PostsPlugin, *Handle, *mode var pluginFns = reload.Vars(map[string]func(PostsPlugin, *Handle, *models.Posts){ "passwordProject": PasswordProject, "digest": Digest, -}) +}, "list-post-plugins-fns") func (h *Handle) PushPostsPlugin(name string, fn func(PostsPlugin, *Handle, *models.Posts)) { m := pluginFns.Load() @@ -53,7 +53,7 @@ func Digest(next PostsPlugin, h *Handle, post *models.Posts) { next(h, post) } -var ordinaryPlugin = reload.Vars([]PostsPlugin{}) +var ordinaryPlugin = reload.Vars([]PostsPlugin{}, "ordinaryPlugin") func (h *Handle) PushPostPlugin(plugin ...PostsPlugin) { p := ordinaryPlugin.Load() diff --git a/app/theme/wp/middleware/middleware.go b/app/theme/wp/middleware/middleware.go index 96bb534..57d328d 100644 --- a/app/theme/wp/middleware/middleware.go +++ b/app/theme/wp/middleware/middleware.go @@ -81,7 +81,7 @@ var plainRouteParam = reload.Vars([]Plain{ return true }, }, -}) +}, "plainRouteParam") var GetUsersIds = reload.BuildValFnWithConfirm("usersIds", func(h *wp.Handle) (map[uint64]string, bool) { users, err := cache.GetAllUsername(h.C) diff --git a/app/theme/wp/pipe.go b/app/theme/wp/pipe.go index 5e269ae..0b3fca3 100644 --- a/app/theme/wp/pipe.go +++ b/app/theme/wp/pipe.go @@ -110,20 +110,17 @@ func BuildHandler(pipeScene string, keyFn func(*Handle, string) string, func HookHandles(hooks map[string][]func(HandleCall) (HandleCall, bool), handlers map[string][]HandleCall, sceneOrStats ...string) []HandleCall { + hookedHandlers := slice.FilterAndMap(sceneOrStats, func(k string) ([]HandleCall, bool) { r := handlers[k] for _, hook := range hooks[k] { - r = HookHandler(hook, r) + r = slice.FilterAndMap(r, hook) } return r, true }) return slice.Decompress(hookedHandlers) } -func HookHandler(hookFn func(HandleCall) (HandleCall, bool), handlers []HandleCall) []HandleCall { - return slice.FilterAndMap(handlers, hookFn) -} - func PipeKey(h *Handle, pipScene string) string { key := str.Join("pipekey", "-", pipScene, "-", h.scene, "-", h.Stats) return h.DoActionFilter("pipeKey", key, pipScene) diff --git a/app/theme/wp/route/route.go b/app/theme/wp/route/route.go index c8abd7f..078c2bc 100644 --- a/app/theme/wp/route/route.go +++ b/app/theme/wp/route/route.go @@ -20,10 +20,10 @@ var routeHook []func(Route) (Route, bool) var regRoutes *safety.Map[string, *regexp.Regexp] var routes = func() *safety.Map[string, Route] { r := safety.NewMap[string, Route]() - reload.Push(func() { + reload.Append(func() { r.Flush() regRoutes.Flush() - }) + }, "wp-routers") regRoutes = safety.NewMap[string, *regexp.Regexp]() return r }() diff --git a/app/theme/wp/wp.go b/app/theme/wp/wp.go index a5ca158..f4e1106 100644 --- a/app/theme/wp/wp.go +++ b/app/theme/wp/wp.go @@ -113,11 +113,6 @@ func SetConfigHandle(a ...any) Handle { var GetInitHandleFn = reload.BuildValFnWithAnyParams("themeArgAndConfig", SetConfigHandle, false) -type ConfigParm struct { - ConfigFn func(*Handle) - H *Handle -} - func InitHandle(configFn func(*Handle), h *Handle) { hh := GetInitHandleFn(configFn, h) mods, err := wpconfig.GetThemeMods(h.theme) diff --git a/app/wpconfig/thememods.go b/app/wpconfig/thememods.go index 2c073b7..4994321 100644 --- a/app/wpconfig/thememods.go +++ b/app/wpconfig/thememods.go @@ -111,10 +111,10 @@ func Thumbnail(metadata models.WpAttachmentMetadata, Type, host string, except . var themeModes = func() *safety.Map[string, ThemeMods] { m := safety.NewMap[string, ThemeMods]() themeModsRaw = safety.NewMap[string, map[string]any]() - reload.Push(func() { + reload.Append(func() { m.Flush() themeModsRaw.Flush() - }) + }, "theme-modes") return m }() diff --git a/cache/reload/reload.go b/cache/reload/reload.go index 7b1fdba..1c0a3d3 100644 --- a/cache/reload/reload.go +++ b/cache/reload/reload.go @@ -2,10 +2,11 @@ package reload import ( "github.com/fthvgb1/wp-go/helper" - "github.com/fthvgb1/wp-go/helper/number" "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" "github.com/fthvgb1/wp-go/safety" "sync" + "sync/atomic" ) type queue struct { @@ -165,7 +166,7 @@ func BuildSafetyMap[K comparable, V, A any](namespace string, args ...any) *Safe m.Val.Delete(key) } }) - Push(m.Val.Flush, args...) + Append(m.Val.Flush, args...) safetyMaps.Store(namespace, m) return m } @@ -205,7 +206,7 @@ func BuildAnyVal[T, A any](namespace string, counter bool, args ...any) *SafetyV v := Val[T]{} vv = &SafetyVar[T, A]{safety.NewVar(v), sync.Mutex{}} args = append(args, namespace) - Push(vv.Val.Flush, args...) + Append(vv.Val.Flush, args...) safetyMaps.Store(namespace, vv) return vv } @@ -233,9 +234,11 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) (T, bool), args .. func BuildValFnWithConfirm[T, A any](namespace string, fn func(A) (T, bool), args ...any) func(A) T { var vv = BuildAnyVal[T, A](namespace, false, args...) tryTimes := helper.ParseArgs(1, args...) - var counter func() int + var counter int64 if tryTimes > 1 { - counter = number.Counters[int]() + Append(func() { + atomic.StoreInt64(&counter, 0) + }, str.Join("reload-valFn-counter-", namespace)) } return func(a A) T { v := vv.Val.Load() @@ -253,11 +256,11 @@ func BuildValFnWithConfirm[T, A any](namespace string, fn func(A) (T, bool), arg vv.Val.Store(v) return v.V } - if counter == nil { + if atomic.LoadInt64(&counter) <= 1 { return v.V } - times := counter() - if times >= tryTimes { + atomic.AddInt64(&counter, 1) + if atomic.LoadInt64(&counter) >= int64(tryTimes) { v.Ok = true vv.Val.Store(v) } @@ -316,7 +319,7 @@ func BuildValFnWithAnyParams[T any](namespace string, fn func(...any) T, args .. // Vars get default value and whenever reloaded assign default value // -// args same as Push +// args same as Append // // if give a name, then can be flushed by calls Reloads // @@ -326,7 +329,7 @@ func BuildValFnWithAnyParams[T any](namespace string, fn func(...any) T, args .. func Vars[T any](defaults T, args ...any) *safety.Var[T] { ss := safety.NewVar(defaults) - Push(func() { + Append(func() { ss.Store(defaults) }, args...) return ss @@ -350,11 +353,11 @@ func parseArgs(a ...any) (ord float64, name string) { // VarsBy // -// args same as Push +// args same as Append // if give a name, then can be flushed by calls Reloads func VarsBy[T any](fn func() T, args ...any) *safety.Var[T] { ss := safety.NewVar(fn()) - Push(func() { + Append(func() { ss.Store(fn()) }, args...) return ss @@ -364,7 +367,7 @@ func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety if fn != nil { fn(m) } - Push(func() { + Append(func() { m.Flush() if fn != nil { fn(m) @@ -375,18 +378,18 @@ func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] { m := safety.NewMap[K, T]() - Push(m.Flush, args...) + Append(m.Flush, args...) return m } -// Push the func that will be called whenever Reload called +// Append the func that will be called whenever Reload called // // if give a name, then can be called by called Reloads // // if give a float then can be called early or lately when called Reload, more bigger more earlier // // if give a bool false will not flushed when called Reload, then can called GetValMap to flush manually -func Push(fn func(), a ...any) { +func Append(fn func(), a ...any) { ord, name := parseArgs(a...) auto := helper.ParseArgs(true, a...) if name != "" && !auto { @@ -434,11 +437,11 @@ func BuildFnVal[T any](name string, t T, fn func() T) func() T { v: p, isManual: safety.NewVar(false), } - Push(func() { + Append(func() { if !e.isManual.Load() { e.v.Store(fn()) } - }) + }, str.Join("fnval-", name)) setFnVal.Store(name, e) return func() T { return e.v.Load()