code.gitea.io/gitea@v1.21.7/services/repository/cache.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repository
     5  
     6  import (
     7  	"context"
     8  
     9  	repo_model "code.gitea.io/gitea/models/repo"
    10  	"code.gitea.io/gitea/modules/cache"
    11  	"code.gitea.io/gitea/modules/git"
    12  	"code.gitea.io/gitea/modules/setting"
    13  )
    14  
    15  // CacheRef cachhe last commit information of the branch or the tag
    16  func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error {
    17  	if !setting.CacheService.LastCommit.Enabled {
    18  		return nil
    19  	}
    20  
    21  	commit, err := gitRepo.GetCommit(fullRefName.String())
    22  	if err != nil {
    23  		return err
    24  	}
    25  
    26  	if gitRepo.LastCommitCache == nil {
    27  		commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(fullRefName.ShortName(), true), commit.CommitsCount)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache())
    32  	}
    33  
    34  	return commit.CacheCommit(ctx)
    35  }