From 72e794c46fcfe059006cd6efdf2f6f315a71ff56 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 25 Oct 2023 15:32:15 +0200 Subject: dashboard: cache per-ns manager lists The query may take up to 100ms in some cases, while the result changes on quite rare occasions. Let's use a cached version of the data when rendering UI pages. We don't need extra tests because it's already excercised in existing tests that trigger web endpoints. --- dashboard/app/api.go | 1 + dashboard/app/cache.go | 28 ++++++++++++++++++++++++++++ dashboard/app/main.go | 8 ++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/dashboard/app/api.go b/dashboard/app/api.go index a51a2328e..2dce92d55 100644 --- a/dashboard/app/api.go +++ b/dashboard/app/api.go @@ -642,6 +642,7 @@ func bugNeedsCommitUpdate(c context.Context, bug *Bug, manager string, fixCommit return true } +// Note: if you do not need the latest data, prefer CachedManagersList(). func managerList(c context.Context, ns string) ([]string, error) { var builds []*Build _, err := db.NewQuery("Build"). diff --git a/dashboard/app/cache.go b/dashboard/app/cache.go index 4e77d55cf..f52cf5d11 100644 --- a/dashboard/app/cache.go +++ b/dashboard/app/cache.go @@ -206,3 +206,31 @@ func minuteCacheNsUpdate(c context.Context, ns string) error { } return nil } + +func CachedManagerList(c context.Context, ns string) ([]string, error) { + key := fmt.Sprintf("%s-managers-list", ns) + + // Check if the object is in cache. + list := []string{} + _, err := memcache.Gob.Get(c, key, &list) + if err == nil { + return list, nil + } else if err != memcache.ErrCacheMiss { + return nil, err + } + + // Update cache. + list, err = managerList(c, ns) + if err != nil { + return nil, err + } + item := &memcache.Item{ + Key: key, + Object: list, + Expiration: time.Minute, + } + if err := memcache.Gob.Set(c, item); err != nil { + return nil, err + } + return list, nil +} diff --git a/dashboard/app/main.go b/dashboard/app/main.go index 18e43a897..4b525b694 100644 --- a/dashboard/app/main.go +++ b/dashboard/app/main.go @@ -958,7 +958,7 @@ func handleBug(c context.Context, w http.ResponseWriter, r *http.Request) error if err != nil { return err } - managers, err := managerList(c, bug.Namespace) + managers, err := CachedManagerList(c, bug.Namespace) if err != nil { return err } @@ -1517,7 +1517,7 @@ func fetchNamespaceBugs(c context.Context, accessLevel AccessLevel, ns string, if err != nil { return nil, err } - managers, err := managerList(c, ns) + managers, err := CachedManagerList(c, ns) if err != nil { return nil, err } @@ -1652,7 +1652,7 @@ func fetchTerminalBugs(c context.Context, accessLevel AccessLevel, if err != nil { return nil, nil, err } - managers, err := managerList(c, ns) + managers, err := CachedManagerList(c, ns) if err != nil { return nil, nil, err } @@ -1744,7 +1744,7 @@ func loadSimilarBugsUI(c context.Context, r *http.Request, bug *Bug, state *Repo continue } if managers[similar.Namespace] == nil { - mgrs, err := managerList(c, similar.Namespace) + mgrs, err := CachedManagerList(c, similar.Namespace) if err != nil { return nil, err } -- cgit mrf-deployment