mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	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:
		
							
								
								
									
										26
									
								
								modules/cache/cache.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										26
									
								
								modules/cache/cache.go
									
									
									
									
										vendored
									
									
								
							| @@ -16,20 +16,28 @@ import ( | ||||
| 	_ "gitea.com/macaron/cache/redis" | ||||
| ) | ||||
|  | ||||
| var conn mc.Cache | ||||
| var ( | ||||
| 	conn mc.Cache | ||||
| ) | ||||
|  | ||||
| func newCache(cacheConfig setting.Cache) (mc.Cache, error) { | ||||
| 	return mc.NewCacher(cacheConfig.Adapter, mc.Options{ | ||||
| 		Adapter:       cacheConfig.Adapter, | ||||
| 		AdapterConfig: cacheConfig.Conn, | ||||
| 		Interval:      cacheConfig.Interval, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| // NewContext start cache service | ||||
| func NewContext() error { | ||||
| 	if setting.CacheService == nil || conn != nil { | ||||
| 		return nil | ||||
| 	var err error | ||||
|  | ||||
| 	if conn == nil && setting.CacheService.Enabled { | ||||
| 		if conn, err = newCache(setting.CacheService.Cache); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	var err error | ||||
| 	conn, err = mc.NewCacher(setting.CacheService.Adapter, mc.Options{ | ||||
| 		Adapter:       setting.CacheService.Adapter, | ||||
| 		AdapterConfig: setting.CacheService.Conn, | ||||
| 		Interval:      setting.CacheService.Interval, | ||||
| 	}) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										64
									
								
								modules/cache/last_commit.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								modules/cache/last_commit.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| // Copyright 2020 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 cache | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"code.gitea.io/gitea/modules/git" | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
|  | ||||
| 	mc "gitea.com/macaron/cache" | ||||
| 	"gopkg.in/src-d/go-git.v4/plumbing/object" | ||||
| ) | ||||
|  | ||||
| // LastCommitCache represents a cache to store last commit | ||||
| type LastCommitCache struct { | ||||
| 	repoPath    string | ||||
| 	ttl         int64 | ||||
| 	repo        *git.Repository | ||||
| 	commitCache map[string]*object.Commit | ||||
| 	mc.Cache | ||||
| } | ||||
|  | ||||
| // NewLastCommitCache creates a new last commit cache for repo | ||||
| func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *LastCommitCache { | ||||
| 	return &LastCommitCache{ | ||||
| 		repoPath:    repoPath, | ||||
| 		repo:        gitRepo, | ||||
| 		commitCache: make(map[string]*object.Commit), | ||||
| 		ttl:         ttl, | ||||
| 		Cache:       conn, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // Get get the last commit information by commit id and entry path | ||||
| func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) { | ||||
| 	v := c.Cache.Get(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath)) | ||||
| 	if vs, ok := v.(string); ok { | ||||
| 		log.Trace("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs) | ||||
| 		if commit, ok := c.commitCache[vs]; ok { | ||||
| 			log.Trace("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, vs) | ||||
| 			return commit, nil | ||||
| 		} | ||||
| 		id, err := c.repo.ConvertToSHA1(vs) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		commit, err := c.repo.GoGitRepo().CommitObject(id) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		c.commitCache[vs] = commit | ||||
| 		return commit, nil | ||||
| 	} | ||||
| 	return nil, nil | ||||
| } | ||||
|  | ||||
| // Put put the last commit id with commit and entry path | ||||
| func (c LastCommitCache) Put(ref, entryPath, commitID string) error { | ||||
| 	log.Trace("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID) | ||||
| 	return c.Cache.Put(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user