Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commit count caching #2774

Merged
merged 5 commits into from
Oct 26, 2017
Merged

Conversation

lafriks
Copy link
Member

@lafriks lafriks commented Oct 23, 2017

Especially on large repositories like linux kernel getting commit count from git takes more or less 7 seconds, so let's cache it as commit count is needed on every repository page load.

For linux kernel repository (more then 700K commits) it brings down repository page load time from 11.5 seconds to 3.3 seconds on my development machine.

[Macaron] 2017-10-24 16:50:25: Started GET /user1/linux for [::1]
[Macaron] 2017-10-24 16:50:36: Completed /user1/linux 200 OK in 11.4833512s
[Macaron] 2017-10-24 16:50:56: Started GET /user1/linux for [::1]
[Macaron] 2017-10-24 16:50:59: Completed /user1/linux 200 OK in 3.2968169s

@lafriks lafriks added the type/enhancement An improvement of existing functionality label Oct 23, 2017
@lafriks lafriks added this to the 1.3.0 milestone Oct 23, 2017
@codecov-io
Copy link

codecov-io commented Oct 23, 2017

Codecov Report

Merging #2774 into master will decrease coverage by 0.02%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2774      +/-   ##
==========================================
- Coverage   27.23%   27.21%   -0.03%     
==========================================
  Files          88       88              
  Lines       17333    17348      +15     
==========================================
  Hits         4721     4721              
- Misses      11927    11942      +15     
  Partials      685      685
Impacted Files Coverage Δ
models/update.go 10.75% <0%> (-0.43%) ⬇️
models/repo.go 13.06% <0%> (-0.07%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ab580c...fcc0bef. Read the comment docs.

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 23, 2017
Copy link
Member

@ethantkoenig ethantkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor things

models/update.go Outdated
if err != nil {
return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
}
}
} else if !isDelRef {
// If is branch reference

// Clear cache for branch commit counr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: counr

Copy link
Member

@ethantkoenig ethantkoenig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(forgot to include a comment)

// GetCommitsCount returns cached commit count for current view
func (r *Repository) GetCommitsCount() (int64, error) {
if r.IsViewBranch {
return cache.GetInt64(fmt.Sprintf("branch-commits-count-%d-%s", r.Repository.ID, r.BranchName), func() (int64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add helper functions for these cache keys instead of having "magic" strings? e.g

func BranchCommitsCountKey(repoID int64, branchName string) string { ... }

With helper functions, we don't have to worry about mistyped strings, and it makes the code cleaner IMO

// GetCommitsCount returns cached commit count for current view
func (r *Repository) GetCommitsCount() (int64, error) {
if r.IsViewBranch {
return cache.GetInt64(fmt.Sprintf("branch-commits-count-%d-%s", r.Repository.ID, r.BranchName), func() (int64, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it feels like this should be in the model instead. Because now we end up with some places having it cached, and other places direct access 😕

Copy link
Member Author

@lafriks lafriks Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bkcsoft
Theoretically I can move it to model and make it less context dependant as branch and tag names can not be the with same name anyway

EDIT: I can't really move it to model as it is context dependent

@lafriks
Copy link
Member Author

lafriks commented Oct 24, 2017

@ethantkoenig fixed

models/repo.go Outdated
@@ -258,6 +258,11 @@ func (repo *Repository) APIFormat(mode AccessMode) *api.Repository {
return repo.innerAPIFormat(mode, false)
}

// GetCommitsCountCacheKey returns cache key used for commits count caching.
func (repo *Repository) GetCommitsCountCacheKey(contextName string) string {
Copy link
Member

@ethantkoenig ethantkoenig Oct 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming scheme is ambiguous. Although branch names cannot conflict with tag names, commit names may conflict with either branch or tag names (i.e. if a branch/tag's name matches a commit's SHA).

I think we'll need multiple functions for each type (branch, tag, commit)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't display it in repo page anyway but ok I'll add it back :)

@lafriks
Copy link
Member Author

lafriks commented Oct 25, 2017

@ethantkoenig done

@ethantkoenig
Copy link
Member

LGTM

@tboerger tboerger added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 25, 2017
func newCacheService() {
CacheAdapter = Cfg.Section("cache").Key("ADAPTER").In("memory", []string{"memory", "redis", "memcache"})
switch CacheAdapter {
sec := Cfg.Section("mailer")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why mailer not cache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch, good catch 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean all the cache. If I have only serval repos on my private VPS. I maybe don't want to enable cache to spent the memory.

@lunny
Copy link
Member

lunny commented Oct 25, 2017

And the cache could be disabled?

@lafriks
Copy link
Member Author

lafriks commented Oct 25, 2017

@lunny do you mean disabling all caching or only commit count caching?

@lafriks
Copy link
Member Author

lafriks commented Oct 25, 2017

@lunny I can add option for that but does that mean also disabling go-macaron cache? I don't know if it is actually used currently

@lafriks
Copy link
Member Author

lafriks commented Oct 25, 2017

Just to note - commits are cached for 1 hour and if not accessed in this time are removed from memory. It could possibly be better to add configurable cache time for each item

@lafriks
Copy link
Member Author

lafriks commented Oct 25, 2017

@lunny fixed and added option to change caching time or disable it (setting caching time to 0)

@lunny
Copy link
Member

lunny commented Oct 26, 2017

LGTM

@tboerger tboerger removed the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Oct 26, 2017
@tboerger tboerger added the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Oct 26, 2017
@lunny lunny merged commit eca05b0 into go-gitea:master Oct 26, 2017
vdbt pushed a commit to vdbt/gitea that referenced this pull request Oct 27, 2017
* Add commit count caching

* Small refactoring

* Add different key prefix for refs and commits

* Add configuratuion option to allow to change caching time or disable it
@lunny lunny mentioned this pull request Nov 15, 2017
8 tasks
@lafriks lafriks deleted the feat/add_caching branch November 15, 2017 16:04
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants