diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-25 02:24:52 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-25 02:24:52 +0800 |
commit | 958d64cc922b2688c910e339748fa28a0ff540b7 (patch) | |
tree | 7e0ebe68ce9bf31b051514682e95ee88c0dbfbec /http_handle_repo_index.go | |
parent | Revert "Add prometheus" (diff) | |
download | forge-958d64cc922b2688c910e339748fa28a0ff540b7.tar.gz forge-958d64cc922b2688c910e339748fa28a0ff540b7.tar.zst forge-958d64cc922b2688c910e339748fa28a0ff540b7.zip |
Cache commit logs on the repo index page
Diffstat (limited to 'http_handle_repo_index.go')
-rw-r--r-- | http_handle_repo_index.go | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index 9285e6e..21493c0 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -4,7 +4,6 @@ package main import ( - "iter" "net/http" "strings" "time" @@ -22,14 +21,12 @@ func httpHandleRepoIndex(writer http.ResponseWriter, _ *http.Request, params map var refHash plumbing.Hash var refHashSlice []byte var err error - var logOptions git.LogOptions - var commitIter object.CommitIter - var commitIterSeq iter.Seq[*object.Commit] var commitObj *object.Commit var tree *object.Tree var notes []string var branches []string var branchesIter storer.ReferenceIter + var commits []commitDisplay repo, repoName, groupPath = params["repo"].(*git.Repository), params["repo_name"].(string), params["group_path"].([]string) @@ -52,13 +49,28 @@ func httpHandleRepoIndex(writer http.ResponseWriter, _ *http.Request, params map } params["branches"] = branches - // TODO: Cache - logOptions = git.LogOptions{From: refHash} //exhaustruct:ignore - if commitIter, err = repo.Log(&logOptions); err != nil { - goto no_ref + if value, found := indexCommitsDisplayCache.Get(refHashSlice); found { + if value != nil { + commits = value + } else { + goto readme + } + } else { + start := time.Now() + commits, err = getRecentCommitsDisplay(repo, refHash, 5) + if err != nil { + commits = nil + } + cost := time.Since(start).Nanoseconds() + indexCommitsDisplayCache.Set(refHashSlice, commits, cost) + if err != nil { + goto readme + } } - commitIterSeq, params["commits_err"] = commitIterSeqErr(commitIter) - params["commits"] = iterSeqLimit(commitIterSeq, 3) + + params["commits"] = commits + +readme: if value, found := treeReadmeCache.Get(refHashSlice); found { params["files"] = value.DisplayTree |