From 928a60887810865dfefb446f167cdccc334125e6 Mon Sep 17 00:00:00 2001 From: xing Date: Tue, 7 Nov 2023 15:19:44 +0800 Subject: [PATCH] change package name --- {app/cmd => cache}/cachemanager/manger.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) rename {app/cmd => cache}/cachemanager/manger.go (89%) diff --git a/app/cmd/cachemanager/manger.go b/cache/cachemanager/manger.go similarity index 89% rename from app/cmd/cachemanager/manger.go rename to cache/cachemanager/manger.go index 6dd9be8..3096d03 100644 --- a/app/cmd/cachemanager/manger.go +++ b/cache/cachemanager/manger.go @@ -15,6 +15,7 @@ var ctx = context.Background() 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)]() +var getBatchToMapFn = safety.NewMap[string, func(context.Context, any, time.Duration, ...any) (any, error)]() var anyFlush = safety.NewMap[string, func()]() var getVar = safety.NewMap[string, func(context.Context, time.Duration, ...any) (any, error)]() @@ -94,6 +95,13 @@ func pushFlushMap[K comparable, V any](m *cache.MapCache[K, V], args ...any) { } return m.GetCacheBatch(ct, kk, t, a...) }) + 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...) + }) FlushPush() } @@ -125,6 +133,20 @@ func GetMultiple[T, K any](name string, ct context.Context, key []K, timeout tim r = vv.([]T) return } +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 +} func parseArgs(args ...any) (string, func() time.Duration) { var name string