diff options
author | zeripath <art27@cantab.net> | 2020-12-17 14:00:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 22:00:47 +0800 |
commit | 511f6138d4b5b7a464a8fa3d7f8fc52bec3789a4 (patch) | |
tree | 126d29951a505dfe499357131b31b0bde57a7896 /modules/cache | |
parent | 0851a895819e0a5a1a79dcbd596d4c93d4d47a76 (diff) | |
download | gitea-511f6138d4b5b7a464a8fa3d7f8fc52bec3789a4.tar.gz gitea-511f6138d4b5b7a464a8fa3d7f8fc52bec3789a4.zip |
Use native git variants by default with go-git variants as build tag (#13673)
* Move last commit cache back into modules/git
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Remove go-git from the interface for last commit cache
Signed-off-by: Andrew Thornton <art27@cantab.net>
* move cacheref to last_commit_cache
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Remove go-git from routers/private/hook
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Move FindLFSFiles to pipeline
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Make no-go-git variants
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Submodule RefID
Signed-off-by: Andrew Thornton <art27@cantab.net>
* fix issue with GetCommitsInfo
Signed-off-by: Andrew Thornton <art27@cantab.net>
* fix GetLastCommitForPaths
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Improve efficiency
Signed-off-by: Andrew Thornton <art27@cantab.net>
* More efficiency
Signed-off-by: Andrew Thornton <art27@cantab.net>
* even faster
Signed-off-by: Andrew Thornton <art27@cantab.net>
* Reduce duplication
* As per @lunny
Signed-off-by: Andrew Thornton <art27@cantab.net>
* attempt to fix drone
Signed-off-by: Andrew Thornton <art27@cantab.net>
* fix test-tags
Signed-off-by: Andrew Thornton <art27@cantab.net>
* default to use no-go-git variants and add gogit build tag
Signed-off-by: Andrew Thornton <art27@cantab.net>
* placate lint
Signed-off-by: Andrew Thornton <art27@cantab.net>
* as per @6543
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules/cache')
-rw-r--r-- | modules/cache/cache.go | 23 | ||||
-rw-r--r-- | modules/cache/last_commit.go | 70 |
2 files changed, 23 insertions, 70 deletions
diff --git a/modules/cache/cache.go b/modules/cache/cache.go index 60865d8335..42227f9289 100644 --- a/modules/cache/cache.go +++ b/modules/cache/cache.go @@ -27,6 +27,24 @@ func newCache(cacheConfig setting.Cache) (mc.Cache, error) { }) } +// Cache is the interface that operates the cache data. +type Cache interface { + // Put puts value into cache with key and expire time. + Put(key string, val interface{}, timeout int64) error + // Get gets cached value by given key. + Get(key string) interface{} + // Delete deletes cached value by given key. + Delete(key string) error + // Incr increases cached int-type value by given key as a counter. + Incr(key string) error + // Decr decreases cached int-type value by given key as a counter. + Decr(key string) error + // IsExist returns true if cached value exists. + IsExist(key string) bool + // Flush deletes all cached data. + Flush() error +} + // NewContext start cache service func NewContext() error { var err error @@ -40,6 +58,11 @@ func NewContext() error { return err } +// GetCache returns the currently configured cache +func GetCache() Cache { + return conn +} + // GetString returns the key value from cache with callback when no key exists in cache func GetString(key string, getFunc func() (string, error)) (string, error) { if conn == nil || setting.CacheService.TTL == 0 { diff --git a/modules/cache/last_commit.go b/modules/cache/last_commit.go deleted file mode 100644 index 660a9250d6..0000000000 --- a/modules/cache/last_commit.go +++ /dev/null @@ -1,70 +0,0 @@ -// 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 ( - "crypto/sha256" - "fmt" - - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" - - mc "gitea.com/macaron/cache" - "github.com/go-git/go-git/v5/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, - } -} - -func (c LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string { - hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, ref, entryPath))) - return fmt.Sprintf("last_commit:%x", hashBytes) -} - -// 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(c.getCacheKey(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(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl) -} |