From c77f120ffab696387579893f12de6f3081526fed Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Mon, 30 Oct 2023 14:47:59 +0100 Subject: dashboard: create a generic cache helper function This will simplify caching of other dashboard output pieces. --- dashboard/app/cache.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'dashboard/app/cache.go') diff --git a/dashboard/app/cache.go b/dashboard/app/cache.go index f52cf5d11..0f78e2b05 100644 --- a/dashboard/app/cache.go +++ b/dashboard/app/cache.go @@ -208,29 +208,36 @@ func minuteCacheNsUpdate(c context.Context, ns string) error { } func CachedManagerList(c context.Context, ns string) ([]string, error) { - key := fmt.Sprintf("%s-managers-list", ns) + return cachedObjectList(c, + fmt.Sprintf("%s-managers-list", ns), + func(c context.Context) ([]string, error) { + return managerList(c, ns) + }, + ) +} +func cachedObjectList[T any](c context.Context, key string, load func(context.Context) ([]T, error)) ([]T, error) { // Check if the object is in cache. - list := []string{} - _, err := memcache.Gob.Get(c, key, &list) + var obj []T + _, err := memcache.Gob.Get(c, key, &obj) if err == nil { - return list, nil + return obj, nil } else if err != memcache.ErrCacheMiss { return nil, err } - // Update cache. - list, err = managerList(c, ns) + // Load the object. + obj, err = load(c) if err != nil { return nil, err } item := &memcache.Item{ Key: key, - Object: list, + Object: obj, Expiration: time.Minute, } if err := memcache.Gob.Set(c, item); err != nil { return nil, err } - return list, nil + return obj, nil } -- cgit mrf-deployment