github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/last_commit_cache.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package git 7 8 import ( 9 "crypto/sha256" 10 "fmt" 11 12 "github.com/gitbundle/modules/log" 13 ) 14 15 // Cache represents a caching interface 16 type Cache interface { 17 // Put puts value into cache with key and expire time. 18 Put(key string, val interface{}, timeout int64) error 19 // Get gets cached value by given key. 20 Get(key string) interface{} 21 } 22 23 func (c *LastCommitCache) getCacheKey(repoPath, ref, entryPath string) string { 24 hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, ref, entryPath))) 25 return fmt.Sprintf("last_commit:%x", hashBytes) 26 } 27 28 // Put put the last commit id with commit and entry path 29 func (c *LastCommitCache) Put(ref, entryPath, commitID string) error { 30 if c == nil || c.cache == nil { 31 return nil 32 } 33 log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID) 34 return c.cache.Put(c.getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl()) 35 }