sync: cache image URLs for main homepage performance

This commit is contained in:
mappu 2022-12-31 14:28:12 +13:00
parent 5430f503e0
commit 92853a495c
3 changed files with 22 additions and 8 deletions

View File

@ -68,7 +68,6 @@ func (ac *APIClient) PopulateCommitInfo(ctx context.Context, rr *Repo) error {
func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error {
t, err := ac.topicsForRepo(ctx, rr.Name)
if err != nil {
return err
}
@ -77,6 +76,16 @@ func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error {
return nil
}
func (ac *APIClient) PopulateImages(ctx context.Context, rr *Repo) error {
img, err := ac.ImageFilesForRepo(ctx, rr.Name)
if err != nil {
return err
}
rr.Images = img
return nil
}
func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
err := ac.apiSem.Acquire(ctx, 1)
if err != nil {
@ -248,7 +257,7 @@ func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([]
return ret, nil
}
// imageFilesForRepo finds documentation images for the repository.
// ImageFilesForRepo finds documentation images for the repository.
// It searches the top-level directory and the dist/ and doc/ subdirectories.
func (ac *APIClient) ImageFilesForRepo(ctx context.Context, repo string) ([]ReaddirEntry, error) {

View File

@ -13,8 +13,10 @@ type Repo struct {
// NewestCommit is populated via PopulateCommitInfo().
NewestCommit time.Time
// Topics is populated via topicsForRepo().
// Topics is populated via PopulateTopics().
Topics []string
// Images are populated via PopulateImages().
Images []ReaddirEntry
}
type ContentsResponse struct {

View File

@ -1,19 +1,22 @@
package main
import (
"fmt"
"net/http"
)
func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string) {
ctx := r.Context()
images, err := this.gitea.ImageFilesForRepo(ctx, repoName)
if err != nil {
this.internalError(w, r, fmt.Errorf("listing images: %w", err))
this.reposMut.RLock()
defer this.reposMut.RUnlock()
ridx, ok := this.reposAlphabeticalOrder[repoName]
if !ok {
w.Header().Set(`Location`, `/static/no_image.png`)
w.WriteHeader(301)
return
}
images := this.reposCache[ridx].Images
if len(images) == 0 {
w.Header().Set(`Location`, `/static/no_image.png`)
w.WriteHeader(301)