2023-02-17 15:36:54 +00:00
|
|
|
package cachemanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-26 13:38:31 +00:00
|
|
|
"errors"
|
2023-02-17 15:36:54 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-11-12 13:39:04 +00:00
|
|
|
"github.com/fthvgb1/wp-go/cache/reload"
|
2023-10-26 13:38:31 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
|
|
|
"github.com/fthvgb1/wp-go/safety"
|
2023-02-17 15:36:54 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
var mapFlush = safety.NewMap[string, func(any)]()
|
|
|
|
var getSingleFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
|
|
|
var getBatchFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
2023-11-07 07:19:44 +00:00
|
|
|
var getBatchToMapFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]()
|
2023-10-26 13:38:31 +00:00
|
|
|
var anyFlush = safety.NewMap[string, func()]()
|
|
|
|
|
2023-11-02 14:40:13 +00:00
|
|
|
var getVar = safety.NewMap[string, func(context.Context, time.Duration, ...any) (any, error)]()
|
|
|
|
|
2023-10-30 13:52:15 +00:00
|
|
|
var expiredTime = safety.NewMap[string, expire]()
|
|
|
|
|
2023-11-02 14:40:13 +00:00
|
|
|
var varCache = safety.NewMap[string, any]()
|
|
|
|
var mapCache = safety.NewMap[string, any]()
|
|
|
|
|
2023-10-30 13:52:15 +00:00
|
|
|
type expire struct {
|
|
|
|
fn func() time.Duration
|
|
|
|
p *safety.Var[time.Duration]
|
|
|
|
isUseManger *safety.Var[bool]
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:54 +00:00
|
|
|
type flush interface {
|
|
|
|
Flush(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
type clearExpired interface {
|
2023-02-17 15:36:54 +00:00
|
|
|
ClearExpired(ctx context.Context)
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:38:31 +00:00
|
|
|
var clears []clearExpired
|
2023-02-17 15:36:54 +00:00
|
|
|
|
|
|
|
var flushes []flush
|
|
|
|
|
|
|
|
func Flush() {
|
|
|
|
for _, f := range flushes {
|
|
|
|
f.Flush(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 12:51:46 +00:00
|
|
|
func FlushMapVal[T any](name string, keys ...T) {
|
2023-10-26 13:38:31 +00:00
|
|
|
v, ok := mapFlush.Load(name)
|
2023-10-27 12:51:46 +00:00
|
|
|
if !ok || len(keys) < 1 {
|
2023-10-26 13:38:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-27 12:51:46 +00:00
|
|
|
v(keys)
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FlushAnyVal(name ...string) {
|
|
|
|
for _, s := range name {
|
|
|
|
v, ok := anyFlush.Load(s)
|
|
|
|
if ok {
|
|
|
|
v()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushFlushMap[K comparable, V any](m *cache.MapCache[K, V], args ...any) {
|
2023-10-30 13:52:15 +00:00
|
|
|
name, _ := parseArgs(args...)
|
2023-11-02 14:40:13 +00:00
|
|
|
if name == "" {
|
|
|
|
return
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
mapCache.Store(name, m)
|
|
|
|
anyFlush.Store(name, func() {
|
|
|
|
m.Flush(ctx)
|
|
|
|
})
|
|
|
|
mapFlush.Store(name, func(a any) {
|
|
|
|
k, ok := a.([]K)
|
|
|
|
if ok && len(k) > 0 {
|
|
|
|
m.Del(ctx, k...)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
getSingleFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
|
|
|
kk, ok := k.(K)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
|
|
|
}
|
|
|
|
return m.GetCache(ct, kk, t, a...)
|
|
|
|
})
|
|
|
|
getBatchFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
|
|
|
kk, ok := k.([]K)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
|
|
|
}
|
|
|
|
return m.GetCacheBatch(ct, kk, t, a...)
|
|
|
|
})
|
2023-11-07 07:19:44 +00:00
|
|
|
getBatchToMapFn.Store(name, func(ct context.Context, k any, t time.Duration, a ...any) (any, error) {
|
|
|
|
kk, ok := k.([]K)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New(str.Join("cache ", name, " key type err"))
|
|
|
|
}
|
|
|
|
return m.GetBatchToMap(ct, kk, t, a...)
|
|
|
|
})
|
2023-11-02 14:40:13 +00:00
|
|
|
FlushPush()
|
2023-02-17 15:36:54 +00:00
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
|
|
|
|
func Get[T, K any](name string, ct context.Context, key K, timeout time.Duration, params ...any) (r T, err error) {
|
2023-11-02 14:40:13 +00:00
|
|
|
ct = context.WithValue(ct, "getCache", name)
|
2023-10-26 13:38:31 +00:00
|
|
|
v, ok := getSingleFn.Load(name)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, err := v(ct, key, timeout, params...)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
r = vv.(T)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func GetMultiple[T, K any](name string, ct context.Context, key []K, timeout time.Duration, params ...any) (r []T, err error) {
|
2023-11-02 14:40:13 +00:00
|
|
|
ct = context.WithValue(ct, "getCache", name)
|
2023-10-26 13:38:31 +00:00
|
|
|
v, ok := getBatchFn.Load(name)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, err := v(ct, key, timeout, params...)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
r = vv.([]T)
|
|
|
|
return
|
|
|
|
}
|
2023-11-07 07:19:44 +00:00
|
|
|
func GetMultipleToMap[T any, K comparable](name string, ct context.Context, key []K, timeout time.Duration, params ...any) (r map[K]T, err error) {
|
|
|
|
ct = context.WithValue(ct, "getCache", name)
|
|
|
|
v, ok := getBatchToMapFn.Load(name)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New(str.Join("cache ", name, " doesn't exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, err := v(ct, key, timeout, params...)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
r = vv.(map[K]T)
|
|
|
|
return
|
|
|
|
}
|
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-10-27 12:51:46 +00:00
|
|
|
func NewMapCache[K comparable, V any](data cache.Cache[K, V], batchFn cache.MapBatchFn[K, V], fn cache.MapSingleFn[K, V], args ...any) *cache.MapCache[K, V] {
|
|
|
|
m := cache.NewMapCache[K, V](data, fn, batchFn)
|
2023-10-26 13:38:31 +00:00
|
|
|
pushFlushMap(m, args...)
|
2023-02-17 15:36:54 +00:00
|
|
|
FlushPush(m)
|
|
|
|
ClearPush(m)
|
2023-11-02 14:40:13 +00:00
|
|
|
name, f := parseArgs(args...)
|
|
|
|
if f != nil && name != "" {
|
|
|
|
SetExpireTime(any(data).(cache.SetTime), name, 0, f)
|
|
|
|
}
|
2023-02-17 15:36:54 +00:00
|
|
|
return m
|
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
func NewMemoryMapCache[K comparable, V any](batchFn cache.MapBatchFn[K, V],
|
|
|
|
fn cache.MapSingleFn[K, V], expireTime time.Duration, args ...any) *cache.MapCache[K, V] {
|
2023-11-02 14:40:13 +00:00
|
|
|
|
|
|
|
c := NewMapCache[K, V](cache.NewMemoryMapCache[K, V](func() time.Duration {
|
|
|
|
return expireTime
|
|
|
|
}), batchFn, fn, args...)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetExpireTime(c cache.SetTime, name string, expireTime time.Duration, expireTimeFn func() time.Duration) {
|
|
|
|
if name == "" {
|
|
|
|
return
|
|
|
|
}
|
2023-10-30 13:52:15 +00:00
|
|
|
var t, tt func() time.Duration
|
2023-11-02 14:40:13 +00:00
|
|
|
t = expireTimeFn
|
2023-10-30 13:52:15 +00:00
|
|
|
if t == nil {
|
|
|
|
t = func() time.Duration {
|
|
|
|
return expireTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tt = t
|
2023-11-02 14:40:13 +00:00
|
|
|
expireTime = t()
|
|
|
|
p := safety.NewVar(expireTime)
|
|
|
|
e := expire{
|
|
|
|
fn: t,
|
|
|
|
p: p,
|
|
|
|
isUseManger: safety.NewVar(false),
|
|
|
|
}
|
|
|
|
expiredTime.Store(name, e)
|
|
|
|
reload.Push(func() {
|
|
|
|
if !e.isUseManger.Load() {
|
|
|
|
e.p.Store(tt())
|
2023-10-30 13:52:15 +00:00
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
}, str.Join("cacheManger-", name, "-expiredTime"))
|
|
|
|
t = func() time.Duration {
|
|
|
|
return e.p.Load()
|
2023-10-30 13:52:15 +00:00
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
c.SetExpiredTime(t)
|
2023-10-30 13:52:15 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 14:40:13 +00:00
|
|
|
func ChangeExpireTime(t time.Duration, name ...string) {
|
2023-10-30 13:52:15 +00:00
|
|
|
for _, s := range name {
|
|
|
|
v, ok := expiredTime.Load(s)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.p.Store(t)
|
|
|
|
if !v.isUseManger.Load() {
|
|
|
|
v.isUseManger.Store(true)
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
}
|
2023-02-17 15:36:54 +00:00
|
|
|
|
|
|
|
func FlushPush(f ...flush) {
|
|
|
|
flushes = append(flushes, f...)
|
|
|
|
}
|
2023-10-26 13:38:31 +00:00
|
|
|
func ClearPush(c ...clearExpired) {
|
2023-02-17 15:36:54 +00:00
|
|
|
clears = append(clears, c...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearExpired() {
|
|
|
|
for _, c := range clears {
|
|
|
|
c.ClearExpired(ctx)
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 14:40:13 +00:00
|
|
|
|
|
|
|
func NewVarCache[T any](c cache.AnyCache[T], fn func(context.Context, ...any) (T, error), a ...any) *cache.VarCache[T] {
|
|
|
|
v := cache.NewVarCache(c, fn)
|
|
|
|
FlushPush(v)
|
|
|
|
name, _ := parseArgs(a...)
|
|
|
|
if name != "" {
|
|
|
|
varCache.Store(name, v)
|
|
|
|
getVar.Store(name, func(c context.Context, duration time.Duration, a ...any) (any, error) {
|
|
|
|
return v.GetCache(c, duration, a...)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
cc, ok := any(c).(clearExpired)
|
|
|
|
if ok {
|
|
|
|
ClearPush(cc)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetVarVal[T any](name string, ctx context.Context, duration time.Duration, a ...any) (r T, err error) {
|
|
|
|
ctx = context.WithValue(ctx, "getCache", name)
|
|
|
|
fn, ok := getVar.Load(name)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New(str.Join("cache ", name, " is not exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v, err := fn(ctx, duration, a...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, ok := v.(T)
|
|
|
|
if !ok {
|
|
|
|
err = errors.New(str.Join("cache ", name, " value wanted can't match got"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = vv
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVarMemoryCache[T any](fn func(context.Context, ...any) (T, error), expired time.Duration, a ...any) *cache.VarCache[T] {
|
|
|
|
c := cache.NewVarMemoryCache[T](nil)
|
|
|
|
name, e := parseArgs(a...)
|
|
|
|
SetExpireTime(c, name, expired, e)
|
|
|
|
v := NewVarCache[T](c, fn, a...)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetVarCache[T any](name string) (*cache.VarCache[T], bool) {
|
|
|
|
v, ok := varCache.Load(name)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
vv, ok := v.(*cache.VarCache[T])
|
|
|
|
return vv, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMapCache[K comparable, V any](name string) (*cache.MapCache[K, V], bool) {
|
|
|
|
v, ok := mapCache.Load(name)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
vv, ok := v.(*cache.MapCache[K, V])
|
|
|
|
return vv, ok
|
|
|
|
}
|