You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

last_commit_cache.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "crypto/sha256"
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // Cache represents a caching interface
  12. type Cache interface {
  13. // Put puts value into cache with key and expire time.
  14. Put(key string, val interface{}, timeout int64) error
  15. // Get gets cached value by given key.
  16. Get(key string) interface{}
  17. }
  18. func getCacheKey(repoPath, commitID, entryPath string) string {
  19. hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", repoPath, commitID, entryPath)))
  20. return fmt.Sprintf("last_commit:%x", hashBytes)
  21. }
  22. // LastCommitCache represents a cache to store last commit
  23. type LastCommitCache struct {
  24. repoPath string
  25. ttl func() int64
  26. repo *Repository
  27. commitCache map[string]*Commit
  28. cache Cache
  29. }
  30. // NewLastCommitCache creates a new last commit cache for repo
  31. func NewLastCommitCache(count int64, repoPath string, gitRepo *Repository, cache Cache) *LastCommitCache {
  32. if cache == nil {
  33. return nil
  34. }
  35. if !setting.CacheService.LastCommit.Enabled || count < setting.CacheService.LastCommit.CommitsCount {
  36. return nil
  37. }
  38. return &LastCommitCache{
  39. repoPath: repoPath,
  40. repo: gitRepo,
  41. ttl: setting.LastCommitCacheTTLSeconds,
  42. cache: cache,
  43. }
  44. }
  45. // Put put the last commit id with commit and entry path
  46. func (c *LastCommitCache) Put(ref, entryPath, commitID string) error {
  47. if c == nil || c.cache == nil {
  48. return nil
  49. }
  50. log.Debug("LastCommitCache save: [%s:%s:%s]", ref, entryPath, commitID)
  51. return c.cache.Put(getCacheKey(c.repoPath, ref, entryPath), commitID, c.ttl())
  52. }
  53. // Get gets the last commit information by commit id and entry path
  54. func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
  55. if c == nil || c.cache == nil {
  56. return nil, nil
  57. }
  58. commitID, ok := c.cache.Get(getCacheKey(c.repoPath, ref, entryPath)).(string)
  59. if !ok || commitID == "" {
  60. return nil, nil
  61. }
  62. log.Debug("LastCommitCache hit level 1: [%s:%s:%s]", ref, entryPath, commitID)
  63. if c.commitCache != nil {
  64. if commit, ok := c.commitCache[commitID]; ok {
  65. log.Debug("LastCommitCache hit level 2: [%s:%s:%s]", ref, entryPath, commitID)
  66. return commit, nil
  67. }
  68. }
  69. commit, err := c.repo.GetCommit(commitID)
  70. if err != nil {
  71. return nil, err
  72. }
  73. if c.commitCache == nil {
  74. c.commitCache = make(map[string]*Commit)
  75. }
  76. c.commitCache[commitID] = commit
  77. return commit, nil
  78. }
  79. // GetCommitByPath gets the last commit for the entry in the provided commit
  80. func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, error) {
  81. sha1, err := NewIDFromString(commitID)
  82. if err != nil {
  83. return nil, err
  84. }
  85. lastCommit, err := c.Get(sha1.String(), entryPath)
  86. if err != nil || lastCommit != nil {
  87. return lastCommit, err
  88. }
  89. lastCommit, err = c.repo.getCommitByPathWithID(sha1, entryPath)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if err := c.Put(commitID, entryPath, lastCommit.ID.String()); err != nil {
  94. log.Error("Unable to cache %s as the last commit for %q in %s %s. Error %v", lastCommit.ID.String(), entryPath, commitID, c.repoPath, err)
  95. }
  96. return lastCommit, nil
  97. }