loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Store JSON in contributors commit cache' (#3159) from gusted/forgejo-cache-json into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3159
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: Gergely Nagy <algernon@noreply.codeberg.org>

+22 -4
+14 -3
services/repository/contributors_graph.go
··· 20 20 "code.gitea.io/gitea/modules/git" 21 21 "code.gitea.io/gitea/modules/gitrepo" 22 22 "code.gitea.io/gitea/modules/graceful" 23 + "code.gitea.io/gitea/modules/json" 23 24 "code.gitea.io/gitea/modules/log" 24 25 api "code.gitea.io/gitea/modules/structs" 25 26 ··· 108 109 switch v := cache.Get(cacheKey).(type) { 109 110 case error: 110 111 return nil, v 111 - case map[string]*ContributorData: 112 - return v, nil 112 + case string: 113 + var cachedStats map[string]*ContributorData 114 + return cachedStats, json.Unmarshal([]byte(v), &cachedStats) 113 115 default: 114 116 return nil, fmt.Errorf("unexpected type in cache detected") 115 117 } ··· 309 311 total.TotalCommits++ 310 312 } 311 313 312 - _ = cache.Put(cacheKey, contributorsCommitStats, contributorStatsCacheTimeout) 314 + data, err := json.Marshal(contributorsCommitStats) 315 + if err != nil { 316 + err := fmt.Errorf("couldn't marshal the data: %w", err) 317 + _ = cache.Put(cacheKey, err, contributorStatsCacheTimeout) 318 + return 319 + } 320 + 321 + // Store the data as an string, to make it uniform what data type is returned 322 + // from caches. 323 + _ = cache.Put(cacheKey, string(data), contributorStatsCacheTimeout) 313 324 generateLock.Delete(cacheKey) 314 325 if genDone != nil { 315 326 genDone <- struct{}{}
+8 -1
services/repository/contributors_graph_test.go
··· 11 11 repo_model "code.gitea.io/gitea/models/repo" 12 12 "code.gitea.io/gitea/models/unittest" 13 13 "code.gitea.io/gitea/modules/git" 14 + "code.gitea.io/gitea/modules/json" 14 15 15 16 "gitea.com/go-chi/cache" 16 17 "github.com/stretchr/testify/assert" ··· 32 33 assert.ErrorAs(t, err, &git.ErrNotExist{}) 33 34 34 35 generateContributorStats(nil, mockCache, "key2", repo, "master") 35 - data, isData := mockCache.Get("key2").(map[string]*ContributorData) 36 + dataString, isData := mockCache.Get("key2").(string) 36 37 assert.True(t, isData) 38 + // Verify that JSON is actually stored in the cache. 39 + assert.EqualValues(t, `{"ethantkoenig@gmail.com":{"name":"Ethan Koenig","login":"","avatar_link":"https://secure.gravatar.com/avatar/b42fb195faa8c61b8d88abfefe30e9e3?d=identicon","home_link":"","total_commits":1,"weeks":{"1511654400000":{"week":1511654400000,"additions":3,"deletions":0,"commits":1}}},"jimmy.praet@telenet.be":{"name":"Jimmy Praet","login":"","avatar_link":"https://secure.gravatar.com/avatar/93c49b7c89eb156971d11161c9b52795?d=identicon","home_link":"","total_commits":1,"weeks":{"1624752000000":{"week":1624752000000,"additions":2,"deletions":0,"commits":1}}},"jon@allspice.io":{"name":"Jon","login":"","avatar_link":"https://secure.gravatar.com/avatar/00388ce725e6886f3e07c3733007289b?d=identicon","home_link":"","total_commits":1,"weeks":{"1607817600000":{"week":1607817600000,"additions":10,"deletions":0,"commits":1}}},"total":{"name":"Total","login":"","avatar_link":"","home_link":"","total_commits":3,"weeks":{"1511654400000":{"week":1511654400000,"additions":3,"deletions":0,"commits":1},"1607817600000":{"week":1607817600000,"additions":10,"deletions":0,"commits":1},"1624752000000":{"week":1624752000000,"additions":2,"deletions":0,"commits":1}}}}`, dataString) 40 + 41 + var data map[string]*ContributorData 42 + assert.NoError(t, json.Unmarshal([]byte(dataString), &data)) 43 + 37 44 var keys []string 38 45 for k := range data { 39 46 keys = append(keys, k)