2022-10-16 19:29:26 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-16 19:29:26 -04:00
|
|
|
|
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-05 06:37:59 -04:00
|
|
|
"math"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2022-10-16 19:29:26 -04:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2023-10-05 06:37:59 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting/config"
|
2022-10-16 19:29:26 -04:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Setting struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
2023-10-05 06:37:59 -04:00
|
|
|
SettingKey string `xorm:"varchar(255) unique"` // key should be lowercase
|
2022-10-16 19:29:26 -04:00
|
|
|
SettingValue string `xorm:"text"`
|
2023-10-05 06:37:59 -04:00
|
|
|
Version int `xorm:"version"`
|
2022-10-16 19:29:26 -04:00
|
|
|
Created timeutil.TimeStamp `xorm:"created"`
|
|
|
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName sets the table name for the settings struct
|
|
|
|
func (s *Setting) TableName() string {
|
|
|
|
return "system_setting"
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Setting))
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
const keyRevision = "revision"
|
2022-11-10 01:43:53 -05:00
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
func GetRevision(ctx context.Context) int {
|
|
|
|
revision := &Setting{SettingKey: keyRevision}
|
|
|
|
if has, err := db.GetByBean(ctx, revision); err != nil {
|
|
|
|
return 0
|
|
|
|
} else if !has {
|
|
|
|
err = db.Insert(ctx, &Setting{SettingKey: keyRevision, Version: 1})
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
} else if revision.Version <= 0 || revision.Version >= math.MaxInt-1 {
|
|
|
|
_, err = db.Exec(ctx, "UPDATE system_setting SET version=1 WHERE setting_key=?", keyRevision)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2023-02-24 05:23:13 -05:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
return revision.Version
|
2023-02-24 05:23:13 -05:00
|
|
|
}
|
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
func GetAllSettings(ctx context.Context) (revision int, res map[string]string, err error) {
|
|
|
|
_ = GetRevision(ctx) // prepare the "revision" key ahead
|
|
|
|
var settings []*Setting
|
2023-02-24 05:23:13 -05:00
|
|
|
if err := db.GetEngine(ctx).
|
2022-10-16 19:29:26 -04:00
|
|
|
Find(&settings); err != nil {
|
2023-10-05 06:37:59 -04:00
|
|
|
return 0, nil, err
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
res = make(map[string]string)
|
2022-10-16 19:29:26 -04:00
|
|
|
for _, s := range settings {
|
2023-10-05 06:37:59 -04:00
|
|
|
if s.SettingKey == keyRevision {
|
|
|
|
revision = s.Version
|
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 08:37:34 -05:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
res[s.SettingKey] = s.SettingValue
|
2023-01-08 08:22:41 -05:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
return revision, res, nil
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
func SetSettings(ctx context.Context, settings map[string]string) error {
|
|
|
|
_ = GetRevision(ctx) // prepare the "revision" key ahead
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
2022-10-16 19:29:26 -04:00
|
|
|
e := db.GetEngine(ctx)
|
2023-10-05 06:37:59 -04:00
|
|
|
_, err := db.Exec(ctx, "UPDATE system_setting SET version=version+1 WHERE setting_key=?", keyRevision)
|
2022-10-16 19:29:26 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
for k, v := range settings {
|
|
|
|
res, err := e.Exec("UPDATE system_setting SET setting_value=? WHERE setting_key=?", v, k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, _ := res.RowsAffected()
|
|
|
|
if rows == 0 { // if no existing row, insert a new row
|
|
|
|
if _, err = e.Insert(&Setting{SettingKey: k, SettingValue: v}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
return nil
|
2022-10-16 19:29:26 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
type dbConfigCachedGetter struct {
|
|
|
|
mu sync.RWMutex
|
2022-10-16 19:29:26 -04:00
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
cacheTime time.Time
|
|
|
|
revision int
|
|
|
|
settings map[string]string
|
|
|
|
}
|
2022-10-16 19:29:26 -04:00
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
var _ config.DynKeyGetter = (*dbConfigCachedGetter)(nil)
|
2022-10-16 19:29:26 -04:00
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
func (d *dbConfigCachedGetter) GetValue(ctx context.Context, key string) (v string, has bool) {
|
|
|
|
d.mu.RLock()
|
|
|
|
defer d.mu.RUnlock()
|
|
|
|
v, has = d.settings[key]
|
|
|
|
return v, has
|
|
|
|
}
|
2023-09-11 06:14:01 -04:00
|
|
|
|
2023-10-05 06:37:59 -04:00
|
|
|
func (d *dbConfigCachedGetter) GetRevision(ctx context.Context) int {
|
|
|
|
d.mu.RLock()
|
|
|
|
defer d.mu.RUnlock()
|
|
|
|
if time.Since(d.cacheTime) < time.Second {
|
|
|
|
return d.revision
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
if GetRevision(ctx) != d.revision {
|
|
|
|
d.mu.RUnlock()
|
|
|
|
d.mu.Lock()
|
|
|
|
rev, set, err := GetAllSettings(ctx)
|
2022-10-16 19:29:26 -04:00
|
|
|
if err != nil {
|
2023-10-05 06:37:59 -04:00
|
|
|
log.Error("Unable to get all settings: %v", err)
|
2022-10-16 19:29:26 -04:00
|
|
|
} else {
|
2023-10-05 06:37:59 -04:00
|
|
|
d.cacheTime = time.Now()
|
|
|
|
d.revision = rev
|
|
|
|
d.settings = set
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
d.mu.Unlock()
|
|
|
|
d.mu.RLock()
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|
2023-10-05 06:37:59 -04:00
|
|
|
return d.revision
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dbConfigCachedGetter) InvalidateCache() {
|
|
|
|
d.mu.Lock()
|
|
|
|
d.cacheTime = time.Time{}
|
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatabaseDynKeyGetter() config.DynKeyGetter {
|
|
|
|
return &dbConfigCachedGetter{}
|
2022-10-16 19:29:26 -04:00
|
|
|
}
|