mirror of
https://github.com/go-gitea/gitea
synced 2025-07-09 20:17:21 +00:00
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:
8
modules/cache/context.go
vendored
8
modules/cache/context.go
vendored
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user