2022-03-30 16:42:47 +08:00
|
|
|
<div class="header-wrapper">
|
2022-07-31 20:32:51 +08:00
|
|
|
<!-- TODO: make template org and user can share -->
|
|
|
|
{{with .ContextUser}}
|
|
|
|
<div class="ui container">
|
|
|
|
<div class="ui vertically grid head">
|
|
|
|
<div class="column">
|
|
|
|
<div class="ui header">
|
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.
2023-02-15 21:37:34 +08:00
|
|
|
{{avatar $.Context . 100}}
|
2022-07-31 20:32:51 +08:00
|
|
|
<span class="text thin grey"><a href="{{.HomeLink}}">{{.DisplayName}}</a></span>
|
|
|
|
<span class="org-visibility">
|
2023-01-31 15:05:25 +08:00
|
|
|
{{if .Visibility.IsLimited}}<div class="ui medium basic horizontal label">{{$.locale.Tr "org.settings.visibility.limited_shortname"}}</div>{{end}}
|
|
|
|
{{if .Visibility.IsPrivate}}<div class="ui medium basic horizontal label">{{$.locale.Tr "org.settings.visibility.private_shortname"}}</div>{{end}}
|
2022-07-31 20:32:51 +08:00
|
|
|
</span>
|
|
|
|
</div>
|
2022-03-30 16:42:47 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-31 20:32:51 +08:00
|
|
|
{{end}}
|
|
|
|
|
2022-03-30 16:42:47 +08:00
|
|
|
<div class="ui tabs container">
|
|
|
|
<div class="ui tabular stackable menu navbar">
|
|
|
|
<a class="item" href="{{.ContextUser.HomeLink}}">
|
2022-06-28 04:58:46 +08:00
|
|
|
{{svg "octicon-repo"}} {{.locale.Tr "user.repositories"}}
|
2022-03-30 16:42:47 +08:00
|
|
|
</a>
|
2023-01-20 19:42:33 +08:00
|
|
|
<a href="{{.ContextUser.HomeLink}}/-/projects" class="{{if .PageIsViewProjects}}active {{end}}item">
|
2023-03-07 00:32:56 +08:00
|
|
|
{{svg "octicon-project-symlink"}} {{.locale.Tr "user.projects"}}
|
2023-01-20 19:42:33 +08:00
|
|
|
</a>
|
2022-03-30 16:42:47 +08:00
|
|
|
{{if (not .UnitPackagesGlobalDisabled)}}
|
2022-10-11 07:12:03 +08:00
|
|
|
<a href="{{.ContextUser.HomeLink}}/-/packages" class="{{if .IsPackagesPage}}active {{end}}item">
|
2022-06-28 04:58:46 +08:00
|
|
|
{{svg "octicon-package"}} {{.locale.Tr "packages.title"}}
|
2022-03-30 16:42:47 +08:00
|
|
|
</a>
|
|
|
|
{{end}}
|
2022-10-11 07:12:03 +08:00
|
|
|
{{if .IsRepoIndexerEnabled}}
|
|
|
|
<a href="{{.ContextUser.HomeLink}}/-/code" class="{{if .IsCodePage}}active {{end}}item">
|
|
|
|
{{svg "octicon-code"}} {{.locale.Tr "user.code"}}
|
|
|
|
</a>
|
|
|
|
{{end}}
|
2022-07-31 20:32:51 +08:00
|
|
|
|
|
|
|
{{if .ContextUser.IsOrganization}}
|
|
|
|
{{if .IsOrganizationMember}}
|
|
|
|
<a class="item" href="{{$.OrgLink}}/members">
|
2023-02-18 21:41:31 +08:00
|
|
|
{{svg "octicon-person"}} {{$.locale.Tr "org.members"}}
|
2022-07-31 20:32:51 +08:00
|
|
|
{{if .NumMembers}}
|
|
|
|
<div class="ui primary label">{{.NumMembers}}</div>
|
|
|
|
{{end}}
|
|
|
|
</a>
|
|
|
|
<a class="item" href="{{$.OrgLink}}/teams">
|
|
|
|
{{svg "octicon-people"}} {{$.locale.Tr "org.teams"}}
|
|
|
|
{{if .NumTeams}}
|
2022-08-31 17:04:41 +08:00
|
|
|
<div class="ui primary label">{{.NumTeams}}</div>
|
2022-07-31 20:32:51 +08:00
|
|
|
{{end}}
|
|
|
|
</a>
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{if .IsOrganizationOwner}}
|
|
|
|
<div class="right menu">
|
|
|
|
<a class="item" href="{{.OrgLink}}/settings">
|
|
|
|
{{svg "octicon-tools"}} {{.locale.Tr "repo.settings"}}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
{{end}}
|
2022-10-24 20:01:05 +08:00
|
|
|
{{else}}
|
|
|
|
<a class='{{if eq .TabName "activity"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=activity">
|
|
|
|
{{svg "octicon-rss"}} {{.locale.Tr "user.activity"}}
|
|
|
|
</a>
|
|
|
|
{{if not .DisableStars}}
|
|
|
|
<a class='{{if eq .TabName "stars"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=stars">
|
|
|
|
{{svg "octicon-star"}} {{.locale.Tr "user.starred"}}
|
|
|
|
{{if .Owner.NumStars}}
|
|
|
|
<div class="ui primary label">{{.Owner.NumStars}}</div>
|
|
|
|
{{end}}
|
|
|
|
</a>
|
|
|
|
{{else}}
|
|
|
|
<a class='{{if eq .TabName "watching"}}active {{end}}item' href="{{.ContextUser.HomeLink}}?tab=watching">
|
|
|
|
{{svg "octicon-eye"}} {{.locale.Tr "user.watched"}}
|
|
|
|
</a>
|
|
|
|
{{end}}
|
2022-07-31 20:32:51 +08:00
|
|
|
{{end}}
|
|
|
|
|
2022-03-30 16:42:47 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="ui tabs divider"></div>
|
|
|
|
</div>
|