Cache GPG keys, emails and users when list commits (#34086)

When list commits, some of the commits authors are the same at many
situations. But current logic will always fetch the same GPG keys from
database. This PR will cache the GPG keys, emails and users for the
context so that reducing the database queries.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Lunny Xiao
2025-04-09 09:34:38 -07:00
repo.diff.committed_by GitHub
repo.diff.parent f8edc29f5d
repo.diff.commit 4a5af4edca
repo.diff.stats_desc%!(EXTRA int=10, int=65, int=48)

8
modules/cache/context.go repo.diff.vendored
repo.diff.view_file

@@ -166,15 +166,15 @@ func RemoveContextData(ctx context.Context, tp, key any) {
}
// GetWithContextCache returns the cache value of the given key in the given context.
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error) {
v := GetContextData(ctx, cacheGroupKey, cacheTargetID)
func GetWithContextCache[T, K any](ctx context.Context, groupKey string, targetKey K, f func(context.Context, K) (T, error)) (T, error) {
v := GetContextData(ctx, groupKey, targetKey)
if vv, ok := v.(T); ok {
return vv, nil
}
t, err := f()
t, err := f(ctx, targetKey)
if err != nil {
return t, err
}
SetContextData(ctx, cacheGroupKey, cacheTargetID, t)
SetContextData(ctx, groupKey, targetKey, t)
return t, nil
}

3
modules/cache/context_test.go repo.diff.vendored
repo.diff.view_file

@@ -4,6 +4,7 @@
package cache
import (
"context"
"testing"
"time"
@@ -30,7 +31,7 @@ func TestWithCacheContext(t *testing.T) {
v = GetContextData(ctx, field, "my_config1")
assert.Nil(t, v)
vInt, err := GetWithContextCache(ctx, field, "my_config1", func() (int, error) {
vInt, err := GetWithContextCache(ctx, field, "my_config1", func(context.Context, string) (int, error) {
return 1, nil
})
assert.NoError(t, err)