optimize code and fix bug
This commit is contained in:
parent
802c7c7dcc
commit
f49ad731e3
|
@ -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)
|
||||
|
|
|
@ -19,7 +19,7 @@ func ValidateServerNames() func(ctx *gin.Context) {
|
|||
}
|
||||
}
|
||||
return m
|
||||
})
|
||||
}, "site-names")
|
||||
|
||||
return func(c *gin.Context) {
|
||||
m := sites.Load()
|
||||
|
|
6
app/pkg/cache/cache.go
vendored
6
app/pkg/cache/cache.go
vendored
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -7,7 +7,12 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
var comFn = template.FuncMap{
|
||||
func postsFn(fn func(models.Posts) string, a models.Posts) string {
|
||||
return fn(a)
|
||||
}
|
||||
|
||||
func FuncMap() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"unescaped": func(s string) any {
|
||||
return template.HTML(s)
|
||||
},
|
||||
|
@ -29,18 +34,4 @@ var comFn = template.FuncMap{
|
|||
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")
|
||||
}
|
||||
comFn[fnName] = fn
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}()
|
||||
|
|
39
cache/reload/reload.go
vendored
39
cache/reload/reload.go
vendored
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user