diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-10-30 14:47:59 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2023-10-31 10:09:36 +0000 |
| commit | c77f120ffab696387579893f12de6f3081526fed (patch) | |
| tree | f45e1f8d1e4657e7caaae01914a5bef95c4219ce /dashboard/app/cache.go | |
| parent | b5729d823e22b9aada3c12f2bbb218c278df1acf (diff) | |
dashboard: create a generic cache helper function
This will simplify caching of other dashboard output pieces.
Diffstat (limited to 'dashboard/app/cache.go')
| -rw-r--r-- | dashboard/app/cache.go | 23 |
1 files changed, 15 insertions, 8 deletions
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 } |
