Skip to content

Commit 3daedb3

Browse files
authored
Use hash of repo path, ref and entrypath as cache key (#12151) (#12161)
1 parent 2bf9872 commit 3daedb3

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

modules/cache/last_commit.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package cache
66

77
import (
8+
"crypto/sha256"
89
"fmt"
910

1011
"code.gitea.io/gitea/modules/git"
@@ -34,9 +35,14 @@ func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *La
3435
}
3536
}
3637

38+
func (c LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string {
39+
hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, ref, entryPath)))
40+
return fmt.Sprintf("last_commit:%x", hashBytes)
41+
}
42+
3743
// Get get the last commit information by commit id and entry path
3844
func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
39-
v := c.Cache.Get(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath))
45+
v := c.Cache.Get(c.getCacheKey(c.repoPath, ref, entryPath))
4046
if vs, ok := v.(string); ok {
4147
log.Trace("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, vs)
4248
if commit, ok := c.commitCache[vs]; ok {
@@ -60,5 +66,5 @@ func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
6066
// Put put the last commit id with commit and entry path
6167
func (c LastCommitCache) Put(ref, entryPath, commitID string) error {
6268
log.Trace("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
63-
return c.Cache.Put(fmt.Sprintf("last_commit:%s:%s:%s", c.repoPath, ref, entryPath), commitID, c.ttl)
69+
return c.Cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl)
6470
}

0 commit comments

Comments
 (0)