2023-02-17 15:36:54 +00:00
|
|
|
package cachemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-11-28 14:46:22 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper"
|
2024-04-17 17:39:36 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice/mockmap"
|
2023-10-26 13:38:31 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-12-26 15:09:30 +00:00
|
|
|
"runtime"
|
2024-04-15 07:25:19 +00:00
|
|
|
"sync"
|
2023-02-17 15:36:54 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
var mutex sync.Mutex
|
2023-10-26 13:38:31 +00:00
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
type Fn func(context.Context)
|
2023-02-17 15:36:54 +00:00
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
type clearExpired interface {
|
2023-02-17 15:36:54 +00:00
|
|
|
ClearExpired(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
var clears = safety.NewVar(mockmap.Map[string, Fn]{})
|
2023-02-17 15:36:54 +00:00
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
var flushes = safety.NewVar(mockmap.Map[string, Fn]{})
|
2023-02-17 15:36:54 +00:00
|
|
|
|
|
|
|
func Flush() {
|
2023-12-08 13:33:09 +00:00
|
|
|
ctx := context.WithValue(context.Background(), "execFlushBy", "mangerFlushFn")
|
2024-04-15 07:25:19 +00:00
|
|
|
for _, f := range flushes.Load() {
|
2024-04-17 17:39:36 +00:00
|
|
|
f.Value(ctx)
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
func Flushes(ctx context.Context, names ...string) {
|
|
|
|
execute(ctx, flushes, names...)
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
func execute(ctx context.Context, q *safety.Var[mockmap.Map[string, Fn]], names ...string) {
|
2024-04-15 07:25:19 +00:00
|
|
|
queues := q.Load()
|
|
|
|
for _, name := range names {
|
|
|
|
queue := queues.Get(name)
|
2024-04-17 17:39:36 +00:00
|
|
|
if queue.Value != nil {
|
|
|
|
queue.Value(ctx)
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 13:52:15 +00:00
|
|
|
func parseArgs(args ...any) (string, func() time.Duration) {
|
2023-10-26 13:38:31 +00:00
|
|
|
var name string
|
2023-10-30 13:52:15 +00:00
|
|
|
var fn func() time.Duration
|
2023-10-26 13:38:31 +00:00
|
|
|
for _, arg := range args {
|
|
|
|
v, ok := arg.(string)
|
|
|
|
if ok {
|
|
|
|
name = v
|
2023-10-30 13:52:15 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
vv, ok := arg.(func() time.Duration)
|
|
|
|
if ok {
|
|
|
|
fn = vv
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
2023-10-30 13:52:15 +00:00
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
2023-10-30 13:52:15 +00:00
|
|
|
return name, fn
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 15:09:30 +00:00
|
|
|
func buildLockFn[K comparable](args ...any) cache.LockFn[K] {
|
|
|
|
lockFn := helper.ParseArgs(cache.LockFn[K](nil), args...)
|
|
|
|
name := helper.ParseArgs("", args...)
|
|
|
|
num := helper.ParseArgs(runtime.NumCPU(), args...)
|
|
|
|
loFn := func() int {
|
|
|
|
return num
|
|
|
|
}
|
|
|
|
loFn = helper.ParseArgs(loFn, args...)
|
|
|
|
if name != "" {
|
2024-01-18 15:29:50 +00:00
|
|
|
loFn = reload.BuildFnVal(str.Join("cachesLocksNum-", name), num, loFn)
|
2023-12-26 15:09:30 +00:00
|
|
|
}
|
|
|
|
if lockFn == nil {
|
|
|
|
looo := helper.ParseArgs(cache.Lockss[K](nil), args...)
|
|
|
|
if looo != nil {
|
|
|
|
lockFn = looo.GetLock
|
|
|
|
loo, ok := any(looo).(cache.LocksNum)
|
|
|
|
if ok && loo != nil {
|
|
|
|
loo.SetLockNum(num)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lo := cache.NewLocks[K](loFn)
|
|
|
|
lockFn = lo.GetLock
|
2024-04-17 17:39:36 +00:00
|
|
|
PushOrSetFlush(mockmap.Item[string, Fn]{
|
|
|
|
Name: name,
|
|
|
|
Value: lo.Flush,
|
2024-04-15 07:25:19 +00:00
|
|
|
})
|
2023-12-26 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return lockFn
|
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
|
|
|
|
func SetExpireTime(c cache.SetTime, name string, expireTime time.Duration, expireTimeFn func() time.Duration) {
|
|
|
|
if name == "" {
|
|
|
|
return
|
|
|
|
}
|
2024-01-18 15:29:50 +00:00
|
|
|
fn := reload.BuildFnVal(str.Join("cacheManger-", name, "-expiredTime"), expireTime, expireTimeFn)
|
2023-11-28 14:46:22 +00:00
|
|
|
c.SetExpiredTime(fn)
|
2023-10-30 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 14:30:55 +00:00
|
|
|
func ChangeExpireTime(t time.Duration, coverConf bool, name ...string) {
|
2023-10-30 13:52:15 +00:00
|
|
|
for _, s := range name {
|
2024-01-18 15:29:50 +00:00
|
|
|
reload.SetFnVal(s, t, coverConf)
|
2023-10-30 13:52:15 +00:00
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
2024-04-17 17:39:36 +00:00
|
|
|
func pushOrSet(q *safety.Var[mockmap.Map[string, Fn]], queues ...mockmap.Item[string, Fn]) {
|
2024-04-15 07:25:19 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
qu := q.Load()
|
|
|
|
for _, queue := range queues {
|
|
|
|
v := qu.Get(queue.Name)
|
2024-04-17 17:39:36 +00:00
|
|
|
if v.Value != nil {
|
|
|
|
qu.Set(queue.Name, queue.Value)
|
2024-04-15 07:25:19 +00:00
|
|
|
} else {
|
|
|
|
qu = append(qu, queue)
|
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
2024-04-15 07:25:19 +00:00
|
|
|
q.Store(qu)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
// PushOrSetFlush will execute flush func when call Flush or Flushes
|
2024-04-17 17:39:36 +00:00
|
|
|
func PushOrSetFlush(queues ...mockmap.Item[string, Fn]) {
|
2024-04-15 07:25:19 +00:00
|
|
|
pushOrSet(flushes, queues...)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
// PushOrSetClearExpired will execute clearExpired func when call ClearExpired or ClearExpireds
|
2024-04-17 17:39:36 +00:00
|
|
|
func PushOrSetClearExpired(queues ...mockmap.Item[string, Fn]) {
|
2024-04-15 07:25:19 +00:00
|
|
|
pushOrSet(clears, queues...)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 17:39:36 +00:00
|
|
|
func del(q *safety.Var[mockmap.Map[string, Fn]], names ...string) {
|
2024-04-15 07:25:19 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
queues := q.Load()
|
|
|
|
for _, name := range names {
|
|
|
|
queues.Del(name)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
2024-04-15 07:25:19 +00:00
|
|
|
q.Store(queues)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
2024-04-15 07:25:19 +00:00
|
|
|
func DelFlush(names ...string) {
|
|
|
|
del(flushes, names...)
|
2023-11-02 14:40:13 +00:00
|
|
|
}
|
2023-12-20 14:30:55 +00:00
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
func DelClearExpired(names ...string) {
|
|
|
|
del(clears, names...)
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
func ClearExpireds(ctx context.Context, names ...string) {
|
|
|
|
execute(ctx, clears, names...)
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 07:25:19 +00:00
|
|
|
func ClearExpired() {
|
|
|
|
ctx := context.WithValue(context.Background(), "execClearExpired", "mangerClearExpiredFn")
|
|
|
|
for _, queue := range clears.Load() {
|
2024-04-17 17:39:36 +00:00
|
|
|
queue.Value(ctx)
|
2023-12-20 14:30:55 +00:00
|
|
|
}
|
|
|
|
}
|