1
0
Fork 0
forked from forgejo/forgejo

Cache last commit to accelerate the repository directory page visit (#10069)

* Cache last commit to accelerate the repository directory page visit

* Default use default cache configuration

* add tests for last commit cache

* Simplify last commit cache

* Revert Enabled back

* Change the last commit cache default ttl to 8760h

* Fix test
This commit is contained in:
Lunny Xiao 2020-02-02 03:11:32 +08:00 committed by GitHub
parent 046bb05979
commit ce7062a422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 273 additions and 23 deletions

View file

@ -7,8 +7,10 @@ package integrations
import (
"fmt"
"net/http"
"path"
"strings"
"testing"
"time"
"code.gitea.io/gitea/modules/setting"
@ -29,12 +31,71 @@ func TestViewRepo(t *testing.T) {
session.MakeRequest(t, req, http.StatusNotFound)
}
func TestViewRepo2(t *testing.T) {
func testViewRepo(t *testing.T) {
defer prepareTestEnv(t)()
req := NewRequest(t, "GET", "/user3/repo3")
session := loginUser(t, "user2")
session.MakeRequest(t, req, http.StatusOK)
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
files := htmlDoc.doc.Find("#repo-files-table > TBODY > TR")
type file struct {
fileName string
commitID string
commitMsg string
commitTime string
}
var items []file
files.Each(func(i int, s *goquery.Selection) {
tds := s.Find("td")
var f file
tds.Each(func(i int, s *goquery.Selection) {
if i == 0 {
f.fileName = strings.TrimSpace(s.Text())
} else if i == 1 {
a := s.Find("a")
f.commitMsg = strings.TrimSpace(a.Text())
l, _ := a.Attr("href")
f.commitID = path.Base(l)
}
})
f.commitTime, _ = s.Find("span.time-since").Attr("title")
items = append(items, f)
})
assert.EqualValues(t, []file{
{
fileName: "doc",
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
commitMsg: "init project",
commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
},
{
fileName: "README.md",
commitID: "2a47ca4b614a9f5a43abbd5ad851a54a616ffee6",
commitMsg: "init project",
commitTime: time.Date(2017, time.June, 14, 13, 54, 21, 0, time.UTC).Format(time.RFC1123),
},
}, items)
}
func TestViewRepo2(t *testing.T) {
// no last commit cache
testViewRepo(t)
// enable last commit cache for all repositories
oldCommitsCount := setting.CacheService.LastCommit.CommitsCount
setting.CacheService.LastCommit.CommitsCount = 0
// first view will not hit the cache
testViewRepo(t)
// second view will hit the cache
testViewRepo(t)
setting.CacheService.LastCommit.CommitsCount = oldCommitsCount
}
func TestViewRepo3(t *testing.T) {