diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-04-13 16:38:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-13 08:38:44 +0000 |
commit | c248f010ad08a7017ba1d418e9b6a5b72aff0c88 (patch) | |
tree | 4e479229bc1248e123f2ed9296edf80fe96cdd65 /services | |
parent | 8fd8978b4934865c2b041216e84e923ad574a4c7 (diff) | |
download | gitea-c248f010ad08a7017ba1d418e9b6a5b72aff0c88.tar.gz gitea-c248f010ad08a7017ba1d418e9b6a5b72aff0c88.zip |
Refactor cache and disable go-chi cache (#30417)
use built-in cache package to wrap external go-chi cache package
Diffstat (limited to 'services')
-rw-r--r-- | services/context/api.go | 8 | ||||
-rw-r--r-- | services/context/captcha.go | 2 | ||||
-rw-r--r-- | services/context/context.go | 7 | ||||
-rw-r--r-- | services/repository/branch.go | 11 | ||||
-rw-r--r-- | services/repository/commitstatus/commitstatus.go | 2 | ||||
-rw-r--r-- | services/repository/contributors_graph.go | 32 | ||||
-rw-r--r-- | services/repository/contributors_graph_test.go | 20 |
7 files changed, 34 insertions, 48 deletions
diff --git a/services/context/api.go b/services/context/api.go index b18a206b5e..c684add297 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -13,7 +13,7 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - mc "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" @@ -21,15 +21,13 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" web_types "code.gitea.io/gitea/modules/web/types" - - "gitea.com/go-chi/cache" ) // APIContext is a specific context for API service type APIContext struct { *Base - Cache cache.Cache + Cache cache.StringCache Doer *user_model.User // current signed-in user IsSigned bool @@ -217,7 +215,7 @@ func APIContexter() func(http.Handler) http.Handler { base, baseCleanUp := NewBaseContext(w, req) ctx := &APIContext{ Base: base, - Cache: mc.GetCache(), + Cache: cache.GetCache(), Repo: &Repository{PullRequest: &PullRequest{}}, Org: &APIOrganization{}, } diff --git a/services/context/captcha.go b/services/context/captcha.go index fa8d779f56..41afe0e7d2 100644 --- a/services/context/captcha.go +++ b/services/context/captcha.go @@ -30,7 +30,7 @@ func GetImageCaptcha() *captcha.Captcha { cpt = captcha.NewCaptcha(captcha.Options{ SubURL: setting.AppSubURL, }) - cpt.Store = cache.GetCache() + cpt.Store = cache.GetCache().ChiCache() }) return cpt } diff --git a/services/context/context.go b/services/context/context.go index 4b318f7e33..7ab48afb73 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -17,7 +17,7 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" - mc "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/setting" @@ -27,7 +27,6 @@ import ( "code.gitea.io/gitea/modules/web/middleware" web_types "code.gitea.io/gitea/modules/web/types" - "gitea.com/go-chi/cache" "gitea.com/go-chi/session" ) @@ -46,7 +45,7 @@ type Context struct { Render Render PageData map[string]any // data used by JavaScript modules in one page, it's `window.config.pageData` - Cache cache.Cache + Cache cache.StringCache Csrf CSRFProtector Flash *middleware.Flash Session session.Store @@ -111,7 +110,7 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context { Render: render, Session: session, - Cache: mc.GetCache(), + Cache: cache.GetCache(), Link: setting.AppSubURL + strings.TrimSuffix(base.Req.URL.EscapedPath(), "/"), Repo: &Repository{PullRequest: &PullRequest{}}, Org: &Organization{}, diff --git a/services/repository/branch.go b/services/repository/branch.go index 229ac54f30..d74e5819a1 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -26,6 +26,7 @@ import ( "code.gitea.io/gitea/modules/queue" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" notify_service "code.gitea.io/gitea/services/notify" files_service "code.gitea.io/gitea/services/repository/files" @@ -119,17 +120,15 @@ func getDivergenceCacheKey(repoID int64, branchName string) string { // getDivergenceFromCache gets the divergence from cache func getDivergenceFromCache(repoID int64, branchName string) (*git.DivergeObject, bool) { - data := cache.GetCache().Get(getDivergenceCacheKey(repoID, branchName)) + data, ok := cache.GetCache().Get(getDivergenceCacheKey(repoID, branchName)) res := git.DivergeObject{ Ahead: -1, Behind: -1, } - s, ok := data.([]byte) - if !ok || len(s) == 0 { + if !ok || data == "" { return &res, false } - - if err := json.Unmarshal(s, &res); err != nil { + if err := json.Unmarshal(util.UnsafeStringToBytes(data), &res); err != nil { log.Error("json.UnMarshal failed: %v", err) return &res, false } @@ -141,7 +140,7 @@ func putDivergenceFromCache(repoID int64, branchName string, divergence *git.Div if err != nil { return err } - return cache.GetCache().Put(getDivergenceCacheKey(repoID, branchName), bs, 30*24*60*60) + return cache.GetCache().Put(getDivergenceCacheKey(repoID, branchName), util.UnsafeBytesToString(bs), 30*24*60*60) } func DelDivergenceFromCache(repoID int64, branchName string) error { diff --git a/services/repository/commitstatus/commitstatus.go b/services/repository/commitstatus/commitstatus.go index 7c1c6c2609..8a62a603d4 100644 --- a/services/repository/commitstatus/commitstatus.go +++ b/services/repository/commitstatus/commitstatus.go @@ -34,7 +34,7 @@ type commitStatusCacheValue struct { func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheValue { c := cache.GetCache() - statusStr, ok := c.Get(getCacheKey(repoID, branchName)).(string) + statusStr, ok := c.Get(getCacheKey(repoID, branchName)) if ok && statusStr != "" { var cv commitStatusCacheValue err := json.Unmarshal([]byte(statusStr), &cv) diff --git a/services/repository/contributors_graph.go b/services/repository/contributors_graph.go index 7c9f535ae0..b0d6de99ca 100644 --- a/services/repository/contributors_graph.go +++ b/services/repository/contributors_graph.go @@ -17,13 +17,12 @@ import ( "code.gitea.io/gitea/models/avatars" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" - - "gitea.com/go-chi/cache" ) const ( @@ -79,13 +78,13 @@ func findLastSundayBeforeDate(dateStr string) (string, error) { } // GetContributorStats returns contributors stats for git commits for given revision or default branch -func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*ContributorData, error) { +func GetContributorStats(ctx context.Context, cache cache.StringCache, repo *repo_model.Repository, revision string) (map[string]*ContributorData, error) { // as GetContributorStats is resource intensive we cache the result cacheKey := fmt.Sprintf(contributorStatsCacheKey, repo.FullName(), revision) if !cache.IsExist(cacheKey) { genReady := make(chan struct{}) - // dont start multible async generations + // dont start multiple async generations _, run := generateLock.Load(cacheKey) if run { return nil, ErrAwaitGeneration @@ -104,15 +103,11 @@ func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_mode } } // TODO: renew timeout of cache cache.UpdateTimeout(cacheKey, contributorStatsCacheTimeout) - - switch v := cache.Get(cacheKey).(type) { - case error: - return nil, v - case map[string]*ContributorData: - return v, nil - default: - return nil, fmt.Errorf("unexpected type in cache detected") + var res map[string]*ContributorData + if _, cacheErr := cache.GetJSON(cacheKey, &res); cacheErr != nil { + return nil, fmt.Errorf("cached error: %w", cacheErr.ToError()) } + return res, nil } // getExtendedCommitStats return the list of *ExtendedCommitStats for the given revision @@ -205,13 +200,12 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int return extendedCommitStats, nil } -func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey string, repo *repo_model.Repository, revision string) { +func generateContributorStats(genDone chan struct{}, cache cache.StringCache, cacheKey string, repo *repo_model.Repository, revision string) { ctx := graceful.GetManager().HammerContext() gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { - err := fmt.Errorf("OpenRepository: %w", err) - _ = cache.Put(cacheKey, err, contributorStatsCacheTimeout) + _ = cache.PutJSON(cacheKey, fmt.Errorf("OpenRepository: %w", err), contributorStatsCacheTimeout) return } defer closer.Close() @@ -221,13 +215,11 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey } extendedCommitStats, err := getExtendedCommitStats(gitRepo, revision) if err != nil { - err := fmt.Errorf("ExtendedCommitStats: %w", err) - _ = cache.Put(cacheKey, err, contributorStatsCacheTimeout) + _ = cache.PutJSON(cacheKey, fmt.Errorf("ExtendedCommitStats: %w", err), contributorStatsCacheTimeout) return } if len(extendedCommitStats) == 0 { - err := fmt.Errorf("no commit stats returned for revision '%s'", revision) - _ = cache.Put(cacheKey, err, contributorStatsCacheTimeout) + _ = cache.PutJSON(cacheKey, fmt.Errorf("no commit stats returned for revision '%s'", revision), contributorStatsCacheTimeout) return } @@ -309,7 +301,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey total.TotalCommits++ } - _ = cache.Put(cacheKey, contributorsCommitStats, contributorStatsCacheTimeout) + _ = cache.PutJSON(cacheKey, contributorsCommitStats, contributorStatsCacheTimeout) generateLock.Delete(cacheKey) if genDone != nil { genDone <- struct{}{} diff --git a/services/repository/contributors_graph_test.go b/services/repository/contributors_graph_test.go index 3801a5eee4..f22c115276 100644 --- a/services/repository/contributors_graph_test.go +++ b/services/repository/contributors_graph_test.go @@ -10,9 +10,9 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/setting" - "gitea.com/go-chi/cache" "github.com/stretchr/testify/assert" ) @@ -20,20 +20,18 @@ func TestRepository_ContributorsGraph(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) assert.NoError(t, repo.LoadOwner(db.DefaultContext)) - mockCache, err := cache.NewCacher(cache.Options{ - Adapter: "memory", - Interval: 24 * 60, - }) + mockCache, err := cache.NewStringCache(setting.Cache{}) assert.NoError(t, err) generateContributorStats(nil, mockCache, "key", repo, "404ref") - err, isErr := mockCache.Get("key").(error) - assert.True(t, isErr) - assert.ErrorAs(t, err, &git.ErrNotExist{}) + var data map[string]*ContributorData + _, getErr := mockCache.GetJSON("key", &data) + assert.NotNil(t, getErr) + assert.ErrorContains(t, getErr.ToError(), "object does not exist") generateContributorStats(nil, mockCache, "key2", repo, "master") - data, isData := mockCache.Get("key2").(map[string]*ContributorData) - assert.True(t, isData) + exist, _ := mockCache.GetJSON("key2", &data) + assert.True(t, exist) var keys []string for k := range data { keys = append(keys, k) |