5
5
package cache
6
6
7
7
import (
8
+ "crypto/sha256"
8
9
"fmt"
9
10
10
11
"code.gitea.io/gitea/modules/git"
@@ -34,9 +35,14 @@ func NewLastCommitCache(repoPath string, gitRepo *git.Repository, ttl int64) *La
34
35
}
35
36
}
36
37
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
+
37
43
// Get get the last commit information by commit id and entry path
38
44
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 ))
40
46
if vs , ok := v .(string ); ok {
41
47
log .Trace ("LastCommitCache hit level 1: [%s:%s:%s]" , ref , entryPath , vs )
42
48
if commit , ok := c .commitCache [vs ]; ok {
@@ -60,5 +66,5 @@ func (c LastCommitCache) Get(ref, entryPath string) (*object.Commit, error) {
60
66
// Put put the last commit id with commit and entry path
61
67
func (c LastCommitCache ) Put (ref , entryPath , commitID string ) error {
62
68
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 )
64
70
}
0 commit comments