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 { func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error {
t, err := ac.topicsForRepo(ctx, rr.Name) t, err := ac.topicsForRepo(ctx, rr.Name)
if err != nil { if err != nil {
return err return err
} }
@ -77,6 +76,16 @@ func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error {
return nil 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 { func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
err := ac.apiSem.Acquire(ctx, 1) err := ac.apiSem.Acquire(ctx, 1)
if err != nil { if err != nil {
@ -248,7 +257,7 @@ func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([]
return ret, nil 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. // It searches the top-level directory and the dist/ and doc/ subdirectories.
func (ac *APIClient) ImageFilesForRepo(ctx context.Context, repo string) ([]ReaddirEntry, error) { 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 is populated via PopulateCommitInfo().
NewestCommit time.Time NewestCommit time.Time
// Topics is populated via topicsForRepo(). // Topics is populated via PopulateTopics().
Topics []string Topics []string
// Images are populated via PopulateImages().
Images []ReaddirEntry
} }
type ContentsResponse struct { type ContentsResponse struct {

View File

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