55package cache
66
77import (
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
3844func (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
6167func (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