-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
WIP: Optimize for big repository on repo home page to make it load less than 1s and /main less than 8s #6045
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63693b2
added two last commit cache implementations to speed up repo page loa…
lunny ec9c119
fixed lints
lunny 63779e7
fix spells
lunny 1a4d086
add adaptor to old cache configuration and add redis cache for last c…
lunny 1d47c1d
add lstree cache
lunny 3c25048
improve ls tree cache
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package lastcommit | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"code.gitea.io/git" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
var ( | ||
_ git.LastCommitCache = &BoltDBCache{} | ||
) | ||
|
||
// BoltDBCache implements git.LastCommitCache interface to save the last commits on boltdb | ||
type BoltDBCache struct { | ||
cacheDir string | ||
bucket []byte | ||
db *bolt.DB | ||
} | ||
|
||
// NewBoltDBCache creates a boltdb cache | ||
func NewBoltDBCache(cacheDir string) (*BoltDBCache, error) { | ||
db, err := bolt.Open(cacheDir, 0600, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var bucket = []byte("default") | ||
err = db.Update(func(tx *bolt.Tx) error { | ||
_, err := tx.CreateBucketIfNotExists(bucket) | ||
if err != nil { | ||
return fmt.Errorf("create bucket: %s", err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &BoltDBCache{ | ||
cacheDir: cacheDir, | ||
bucket: bucket, | ||
db: db, | ||
}, nil | ||
} | ||
|
||
// Get implements git.LastCommitCache | ||
func (c *BoltDBCache) Get(repoPath, ref, entryPath string) (*git.Commit, error) { | ||
var commit git.Commit | ||
var found bool | ||
err := c.db.View(func(tx *bolt.Tx) error { | ||
b := tx.Bucket(c.bucket) | ||
v := b.Get([]byte(getKey(repoPath, ref, entryPath))) | ||
if v == nil || len(v) <= 0 { | ||
return nil | ||
} | ||
found = true | ||
return json.Unmarshal(v, &commit) | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if found { | ||
return &commit, nil | ||
} | ||
return nil, nil | ||
} | ||
|
||
// Put implements git.LastCommitCache | ||
func (c *BoltDBCache) Put(repoPath, ref, entryPath string, commit *git.Commit) error { | ||
err := c.db.Update(func(tx *bolt.Tx) error { | ||
b := tx.Bucket(c.bucket) | ||
v, err := json.Marshal(commit) | ||
if err != nil { | ||
return err | ||
} | ||
return b.Put([]byte(getKey(repoPath, ref, entryPath)), v) | ||
}) | ||
return err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package lastcommit | ||
|
||
import ( | ||
"fmt" | ||
|
||
"code.gitea.io/git" | ||
|
||
"github.com/go-macaron/cache" | ||
) | ||
|
||
type lastCommitCache struct { | ||
mc cache.Cache | ||
timeout int64 // seconds | ||
} | ||
|
||
func getKey(repoPath, ref, entryPath string) string { | ||
return fmt.Sprintf("%s:%s:%s", repoPath, ref, entryPath) | ||
} | ||
|
||
func (l *lastCommitCache) Get(repoPath, ref, entryPath string) (*git.Commit, error) { | ||
res := l.mc.Get(getKey(repoPath, ref, entryPath)) | ||
if res == nil { | ||
return nil, nil | ||
} | ||
return res.(*git.Commit), nil | ||
} | ||
|
||
func (l *lastCommitCache) Put(repoPath, ref, entryPath string, commit *git.Commit) error { | ||
return l.mc.Put(getKey(repoPath, ref, entryPath), commit, l.timeout) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package lastcommit | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"code.gitea.io/git" | ||
"code.gitea.io/gitea/modules/cache" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
var ( | ||
// Cache defines globally last commit cache object | ||
Cache git.LastCommitCache | ||
) | ||
|
||
// NewContext init | ||
func NewContext() error { | ||
var err error | ||
switch setting.CacheService.LastCommit.Type { | ||
case "default": | ||
if cache.Cache != nil { | ||
Cache = &lastCommitCache{ | ||
mc: cache.Cache, | ||
timeout: int64(setting.CacheService.TTL / time.Second), | ||
} | ||
} else { | ||
log.Warn("Last Commit Cache Enabled but Cache Service not Configed Well") | ||
return nil | ||
} | ||
case "memory": | ||
Cache = &MemoryCache{} | ||
case "boltdb": | ||
Cache, err = NewBoltDBCache(setting.CacheService.LastCommit.ConnStr) | ||
case "redis": | ||
addrs, pass, dbIdx, err := parseConnStr(setting.CacheService.LastCommit.ConnStr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
Cache, err = NewRedisCache(addrs, pass, dbIdx) | ||
case "": | ||
return nil | ||
default: | ||
return fmt.Errorf("Unsupported last commit type: %s", setting.CacheService.LastCommit.Type) | ||
} | ||
if err == nil { | ||
log.Info("Last Commit Cache %s Enabled", setting.CacheService.LastCommit.Type) | ||
} | ||
return err | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can simplify that whole thing with something like
if len(v) == 0 {
, ifv
isnil
,len(v)
will return0
.