diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-02-15 21:37:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-15 21:37:34 +0800 |
commit | bd820aa9c52da4568b460a0b8604287f8ed8df26 (patch) | |
tree | 15e59e1d4f705b1c5adbd418ed711dfd602a1b25 /templates/base | |
parent | 03638f9725de3cda5207384098b38462f56d442e (diff) | |
download | gitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.tar.gz gitea-bd820aa9c52da4568b460a0b8604287f8ed8df26.zip |
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
Diffstat (limited to 'templates/base')
-rw-r--r-- | templates/base/head.tmpl | 8 | ||||
-rw-r--r-- | templates/base/head_navbar.tmpl | 4 | ||||
-rw-r--r-- | templates/base/head_script.tmpl | 4 |
3 files changed, 8 insertions, 8 deletions
diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 36c4fafd2b..c552dcfd2d 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -32,7 +32,7 @@ {{if .PageIsUserProfile}} <meta property="og:title" content="{{.Owner.DisplayName}}"> <meta property="og:type" content="profile"> - <meta property="og:image" content="{{.Owner.AvatarLink}}"> + <meta property="og:image" content="{{.Owner.AvatarLink $.Context}}"> <meta property="og:url" content="{{.Owner.HTMLURL}}"> {{if .Owner.Description}} <meta property="og:description" content="{{.Owner.Description}}"> @@ -52,10 +52,10 @@ {{end}} {{end}} <meta property="og:type" content="object"> - {{if .Repository.AvatarLink}} - <meta property="og:image" content="{{.Repository.AvatarLink}}"> + {{if (.Repository.AvatarLink $.Context)}} + <meta property="og:image" content="{{.Repository.AvatarLink $.Context}}"> {{else}} - <meta property="og:image" content="{{.Repository.Owner.AvatarLink}}"> + <meta property="og:image" content="{{.Repository.Owner.AvatarLink $.Context}}"> {{end}} {{else}} <meta property="og:title" content="{{AppName}}"> diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index dafc64cad7..059363b72d 100644 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -59,7 +59,7 @@ <div class="right stackable menu"> <div class="ui dropdown jump item tooltip" tabindex="-1" data-content="{{.locale.Tr "user_profile_and_more"}}"> <span class="text"> - {{avatar .SignedUser 24 "tiny"}} + {{avatar $.Context .SignedUser 24 "tiny"}} <span class="sr-only">{{.locale.Tr "user_profile_and_more"}}</span> <span class="mobile-only">{{.SignedUser.Name}}</span> <span class="fitted not-mobile" tabindex="-1">{{svg "octicon-triangle-down"}}</span> @@ -152,7 +152,7 @@ <div class="ui dropdown jump item tooltip gt-mx-0" tabindex="-1" data-content="{{.locale.Tr "user_profile_and_more"}}"> <span class="text"> - {{avatar .SignedUser 24 "tiny"}} + {{avatar $.Context .SignedUser 24 "tiny"}} <span class="sr-only">{{.locale.Tr "user_profile_and_more"}}</span> <span class="mobile-only">{{.SignedUser.Name}}</span> <span class="fitted not-mobile" tabindex="-1">{{svg "octicon-triangle-down"}}</span> diff --git a/templates/base/head_script.tmpl b/templates/base/head_script.tmpl index c4ac18a86e..1b130d9cb4 100644 --- a/templates/base/head_script.tmpl +++ b/templates/base/head_script.tmpl @@ -22,11 +22,11 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly. tributeValues: Array.from(new Map([ {{range .Participants}} ['{{.Name}}', {key: '{{.Name}} {{.FullName}}', value: '{{.Name}}', - name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink}}'}], + name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink $.Context}}'}], {{end}} {{range .Assignees}} ['{{.Name}}', {key: '{{.Name}} {{.FullName}}', value: '{{.Name}}', - name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink}}'}], + name: '{{.Name}}', fullname: '{{.FullName}}', avatar: '{{.AvatarLink $.Context}}'}], {{end}} {{range .MentionableTeams}} ['{{$.MentionableTeamsOrg}}/{{.Name}}', {key: '{{$.MentionableTeamsOrg}}/{{.Name}}', value: '{{$.MentionableTeamsOrg}}/{{.Name}}', |