diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-10-17 07:29:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 07:29:26 +0800 |
commit | f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad (patch) | |
tree | 93abb2f354576e50c87d70b0b4bb46369fb3a1f1 /modules/system | |
parent | 5d3dbffa150d832d2f9aedd9f90ca91178a95f9c (diff) | |
download | gitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.tar.gz gitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.zip |
Add system setting table with cache and also add cache supports for user setting (#18058)
Diffstat (limited to 'modules/system')
-rw-r--r-- | modules/system/appstate.go | 25 | ||||
-rw-r--r-- | modules/system/appstate_test.go | 67 | ||||
-rw-r--r-- | modules/system/db.go | 36 | ||||
-rw-r--r-- | modules/system/item_runtime.go | 15 | ||||
-rw-r--r-- | modules/system/setting.go | 46 | ||||
-rw-r--r-- | modules/system/user_setting.go | 34 |
6 files changed, 223 insertions, 0 deletions
diff --git a/modules/system/appstate.go b/modules/system/appstate.go new file mode 100644 index 0000000000..deee8cd029 --- /dev/null +++ b/modules/system/appstate.go @@ -0,0 +1,25 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +// StateStore is the interface to get/set app state items +type StateStore interface { + Get(item StateItem) error + Set(item StateItem) error +} + +// StateItem provides the name for a state item. the name will be used to generate filenames, etc +type StateItem interface { + Name() string +} + +// AppState contains the state items for the app +var AppState StateStore + +// Init initialize AppState interface +func Init() error { + AppState = &DBStore{} + return nil +} diff --git a/modules/system/appstate_test.go b/modules/system/appstate_test.go new file mode 100644 index 0000000000..fb0c2aaf9f --- /dev/null +++ b/modules/system/appstate_test.go @@ -0,0 +1,67 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", ".."), + FixtureFiles: []string{""}, // load nothing + }) +} + +type testItem1 struct { + Val1 string + Val2 int +} + +func (*testItem1) Name() string { + return "test-item1" +} + +type testItem2 struct { + K string +} + +func (*testItem2) Name() string { + return "test-item2" +} + +func TestAppStateDB(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + as := &DBStore{} + + item1 := new(testItem1) + assert.NoError(t, as.Get(item1)) + assert.Equal(t, "", item1.Val1) + assert.EqualValues(t, 0, item1.Val2) + + item1 = new(testItem1) + item1.Val1 = "a" + item1.Val2 = 2 + assert.NoError(t, as.Set(item1)) + + item2 := new(testItem2) + item2.K = "V" + assert.NoError(t, as.Set(item2)) + + item1 = new(testItem1) + assert.NoError(t, as.Get(item1)) + assert.Equal(t, "a", item1.Val1) + assert.EqualValues(t, 2, item1.Val2) + + item2 = new(testItem2) + assert.NoError(t, as.Get(item2)) + assert.Equal(t, "V", item2.K) +} diff --git a/modules/system/db.go b/modules/system/db.go new file mode 100644 index 0000000000..b1c283c488 --- /dev/null +++ b/modules/system/db.go @@ -0,0 +1,36 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +import ( + "code.gitea.io/gitea/models/system" + "code.gitea.io/gitea/modules/json" + + "github.com/yuin/goldmark/util" +) + +// DBStore can be used to store app state items in local filesystem +type DBStore struct{} + +// Get reads the state item +func (f *DBStore) Get(item StateItem) error { + content, err := system.GetAppStateContent(item.Name()) + if err != nil { + return err + } + if content == "" { + return nil + } + return json.Unmarshal(util.StringToReadOnlyBytes(content), item) +} + +// Set saves the state item +func (f *DBStore) Set(item StateItem) error { + b, err := json.Marshal(item) + if err != nil { + return err + } + return system.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b)) +} diff --git a/modules/system/item_runtime.go b/modules/system/item_runtime.go new file mode 100644 index 0000000000..ef758a5675 --- /dev/null +++ b/modules/system/item_runtime.go @@ -0,0 +1,15 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +// RuntimeState contains app state for runtime, and we can save remote version for update checker here in future +type RuntimeState struct { + LastAppPath string `json:"last_app_path"` +} + +// Name returns the item name +func (a RuntimeState) Name() string { + return "runtime-state" +} diff --git a/modules/system/setting.go b/modules/system/setting.go new file mode 100644 index 0000000000..aebf24a501 --- /dev/null +++ b/modules/system/setting.go @@ -0,0 +1,46 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +import ( + "strconv" + + "code.gitea.io/gitea/models/system" + "code.gitea.io/gitea/modules/cache" +) + +func genKey(key string) string { + return "system.setting." + key +} + +// GetSetting returns the setting value via the key +func GetSetting(key string) (string, error) { + return cache.GetString(genKey(key), func() (string, error) { + res, err := system.GetSetting(key) + if err != nil { + return "", err + } + return res.SettingValue, nil + }) +} + +// GetSettingBool return bool value of setting, +// none existing keys and errors are ignored and result in false +func GetSettingBool(key string) bool { + s, _ := GetSetting(key) + b, _ := strconv.ParseBool(s) + return b +} + +// SetSetting sets the setting value +func SetSetting(key, value string, version int) error { + cache.Remove(genKey(key)) + + return system.SetSetting(&system.Setting{ + SettingKey: key, + SettingValue: value, + Version: version, + }) +} diff --git a/modules/system/user_setting.go b/modules/system/user_setting.go new file mode 100644 index 0000000000..eaf146c08d --- /dev/null +++ b/modules/system/user_setting.go @@ -0,0 +1,34 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package system + +import ( + "fmt" + + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/cache" +) + +func genUserKey(userID int64, key string) string { + return fmt.Sprintf("user_%d.setting.%s", userID, key) +} + +// GetUserSetting returns the user setting value via the key +func GetUserSetting(userID int64, key string) (string, error) { + return cache.GetString(genUserKey(userID, key), func() (string, error) { + res, err := user.GetSetting(userID, key) + if err != nil { + return "", err + } + return res.SettingValue, nil + }) +} + +// SetUserSetting sets the user setting value +func SetUserSetting(userID int64, key, value string) error { + cache.Remove(genUserKey(userID, key)) + + return user.SetUserSetting(userID, key, value) +} |