reload optimize add flush single element
This commit is contained in:
parent
b9c2527f4c
commit
5273f4ecf9
|
@ -77,7 +77,7 @@ func PostComment(c *gin.Context) {
|
||||||
}
|
}
|
||||||
req.Host = home.Host
|
req.Host = home.Host
|
||||||
res, err := cli.Do(req)
|
res, err := cli.Do(req)
|
||||||
if err != nil && err != http.ErrUseLastResponse {
|
if err != nil && !errors.Is(err, http.ErrUseLastResponse) {
|
||||||
logs.Error(err, "请求评论接口错误")
|
logs.Error(err, "请求评论接口错误")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,11 @@ import (
|
||||||
type queue struct {
|
type queue struct {
|
||||||
fn func()
|
fn func()
|
||||||
order float64
|
order float64
|
||||||
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
var calls []queue
|
var calls []queue
|
||||||
|
var callsM = safety.NewMap[string, func()]()
|
||||||
|
|
||||||
var anyMap = safety.NewMap[string, any]()
|
var anyMap = safety.NewMap[string, any]()
|
||||||
|
|
||||||
|
@ -33,6 +35,24 @@ 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{}
|
||||||
|
|
||||||
|
var flushMapFn = safety.NewMap[string, func(any)]()
|
||||||
|
|
||||||
|
func FlushMapVal(namespace string, key any) {
|
||||||
|
fn, ok := flushMapFn.Load(namespace)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fn(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FlushAnyVal(namespace string) {
|
||||||
|
fn, ok := callsM.Load(namespace)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
|
||||||
func GetAnyMapFnBys[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 {
|
||||||
|
@ -53,7 +73,7 @@ func GetAnyMapFnBys[K comparable, V, A any](namespace string, fn func(A) V) func
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func safetyMapFn[K comparable, V, A any](namespace string, order ...float64) *safetyMap[K, V, A] {
|
func safetyMapFn[K comparable, V, A any](namespace string, args ...any) *safetyMap[K, V, A] {
|
||||||
vv, ok := safetyMaps.Load(namespace)
|
vv, ok := safetyMaps.Load(namespace)
|
||||||
var m *safetyMap[K, V, A]
|
var m *safetyMap[K, V, A]
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -65,9 +85,17 @@ func safetyMapFn[K comparable, V, A any](namespace string, order ...float64) *sa
|
||||||
m = vv.(*safetyMap[K, V, A])
|
m = vv.(*safetyMap[K, V, A])
|
||||||
} else {
|
} else {
|
||||||
m = &safetyMap[K, V, A]{safety.NewMap[K, V](), sync.Mutex{}}
|
m = &safetyMap[K, V, A]{safety.NewMap[K, V](), sync.Mutex{}}
|
||||||
|
ord, _ := parseArgs(args...)
|
||||||
|
flushMapFn.Store(namespace, func(a any) {
|
||||||
|
k, ok := a.(K)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m.val.Delete(k)
|
||||||
|
})
|
||||||
Push(func() {
|
Push(func() {
|
||||||
m.val.Flush()
|
m.val.Flush()
|
||||||
}, order...)
|
}, ord, namespace)
|
||||||
safetyMaps.Store(namespace, m)
|
safetyMaps.Store(namespace, m)
|
||||||
}
|
}
|
||||||
safetyMapLock.Unlock()
|
safetyMapLock.Unlock()
|
||||||
|
@ -75,8 +103,8 @@ func safetyMapFn[K comparable, V, A any](namespace string, order ...float64) *sa
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V, order ...float64) V {
|
func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn func(A) V, args ...any) V {
|
||||||
m := safetyMapFn[K, V, A](namespace, order...)
|
m := safetyMapFn[K, V, A](namespace, args...)
|
||||||
v, ok := m.val.Load(key)
|
v, ok := m.val.Load(key)
|
||||||
if ok {
|
if ok {
|
||||||
return v
|
return v
|
||||||
|
@ -93,7 +121,7 @@ func GetAnyValMapBy[K comparable, V, A any](namespace string, key K, a A, fn fun
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func anyVal[T, A any](namespace string, counter bool, order ...float64) *safetyVar[T, A] {
|
func anyVal[T, A any](namespace string, counter bool, args ...any) *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 {
|
||||||
|
@ -109,9 +137,10 @@ func anyVal[T, A any](namespace string, counter bool, order ...float64) *safetyV
|
||||||
v.counter = number.Counters[int]()
|
v.counter = number.Counters[int]()
|
||||||
}
|
}
|
||||||
vv = &safetyVar[T, A]{safety.NewVar(v), sync.Mutex{}}
|
vv = &safetyVar[T, A]{safety.NewVar(v), sync.Mutex{}}
|
||||||
|
ord, _ := parseArgs(args...)
|
||||||
Push(func() {
|
Push(func() {
|
||||||
vv.Val.Flush()
|
vv.Val.Flush()
|
||||||
}, getOrder(order...))
|
}, ord, namespace)
|
||||||
safetyMaps.Store(namespace, vv)
|
safetyMaps.Store(namespace, vv)
|
||||||
}
|
}
|
||||||
safetyMapLock.Unlock()
|
safetyMapLock.Unlock()
|
||||||
|
@ -119,8 +148,8 @@ func anyVal[T, A any](namespace string, counter bool, order ...float64) *safetyV
|
||||||
return vv
|
return vv
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, bool), order ...float64) T {
|
func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, bool), args ...any) T {
|
||||||
var vv = anyVal[T, A](namespace, true, order...)
|
var vv = anyVal[T, A](namespace, true, args...)
|
||||||
var ok bool
|
var ok bool
|
||||||
v := vv.Val.Load()
|
v := vv.Val.Load()
|
||||||
if v.ok {
|
if v.ok {
|
||||||
|
@ -142,8 +171,8 @@ func GetAnyValBy[T, A any](namespace string, tryTimes int, a A, fn func(A) (T, b
|
||||||
return v.v
|
return v.v
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T, order ...float64) T {
|
func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T, args ...any) T {
|
||||||
var vv = anyVal[T, A](namespace, false, order...)
|
var vv = anyVal[T, A](namespace, false, args...)
|
||||||
v := vv.Val.Load()
|
v := vv.Val.Load()
|
||||||
if v.ok {
|
if v.ok {
|
||||||
return v.v
|
return v.v
|
||||||
|
@ -161,61 +190,68 @@ func GetAnyValBys[T, A any](namespace string, a A, fn func(A) T, order ...float6
|
||||||
return v.v
|
return v.v
|
||||||
}
|
}
|
||||||
|
|
||||||
func Vars[T any](defaults T, order ...float64) *safety.Var[T] {
|
func Vars[T any](defaults T, args ...any) *safety.Var[T] {
|
||||||
ss := safety.NewVar(defaults)
|
ss := safety.NewVar(defaults)
|
||||||
ord := getOrder(order...)
|
ord, name := parseArgs(args...)
|
||||||
calls = append(calls, queue{func() {
|
Push(func() {
|
||||||
ss.Store(defaults)
|
ss.Store(defaults)
|
||||||
}, ord})
|
}, ord, name)
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOrder(order ...float64) float64 {
|
func parseArgs(a ...any) (ord float64, name string) {
|
||||||
var ord float64
|
if len(a) > 0 {
|
||||||
if len(order) > 0 {
|
for _, arg := range a {
|
||||||
ord = order[0]
|
v, ok := arg.(float64)
|
||||||
|
if ok {
|
||||||
|
ord = v
|
||||||
}
|
}
|
||||||
return ord
|
vv, ok := arg.(string)
|
||||||
|
if ok {
|
||||||
|
name = vv
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ord, name
|
||||||
}
|
}
|
||||||
func VarsBy[T any](fn func() T, order ...float64) *safety.Var[T] {
|
func VarsBy[T any](fn func() T, args ...any) *safety.Var[T] {
|
||||||
ss := safety.NewVar(fn())
|
ss := safety.NewVar(fn())
|
||||||
ord := getOrder(order...)
|
ord, name := parseArgs(args...)
|
||||||
calls = append(calls, queue{
|
Push(func() {
|
||||||
func() {
|
|
||||||
ss.Store(fn())
|
ss.Store(fn())
|
||||||
}, ord,
|
}, ord, name)
|
||||||
})
|
|
||||||
return ss
|
return ss
|
||||||
}
|
}
|
||||||
func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), order ...float64) *safety.Map[K, T] {
|
func MapBy[K comparable, T any](fn func(*safety.Map[K, T]), args ...any) *safety.Map[K, T] {
|
||||||
m := safety.NewMap[K, T]()
|
m := safety.NewMap[K, T]()
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
fn(m)
|
fn(m)
|
||||||
}
|
}
|
||||||
ord := getOrder(order...)
|
ord, name := parseArgs(args...)
|
||||||
calls = append(calls, queue{
|
Push(func() {
|
||||||
func() {
|
|
||||||
m.Flush()
|
m.Flush()
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
fn(m)
|
fn(m)
|
||||||
}
|
}
|
||||||
}, ord,
|
}, ord, name)
|
||||||
})
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func SafeMap[K comparable, T any](order ...float64) *safety.Map[K, T] {
|
func SafeMap[K comparable, T any](args ...any) *safety.Map[K, T] {
|
||||||
m := safety.NewMap[K, T]()
|
m := safety.NewMap[K, T]()
|
||||||
ord := getOrder(order...)
|
ord, name := parseArgs(args...)
|
||||||
calls = append(calls, queue{func() {
|
Push(func() {
|
||||||
m.Flush()
|
m.Flush()
|
||||||
}, ord})
|
}, ord, name)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func Push(fn func(), order ...float64) {
|
func Push(fn func(), a ...any) {
|
||||||
ord := getOrder(order...)
|
ord, name := parseArgs(a...)
|
||||||
calls = append(calls, queue{fn, ord})
|
calls = append(calls, queue{fn, ord, name})
|
||||||
|
if name != "" {
|
||||||
|
callsM.Store(name, fn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Reload() {
|
func Reload() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user