fix: settings map read and write concurrently

pull/2520/head
Noah Hsu 2022-11-28 16:53:16 +08:00
parent 61a06992c3
commit 76f37373e0
4 changed files with 31 additions and 15 deletions

View File

@ -123,12 +123,12 @@ func InitialSettings() []model.SettingItem {
Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.OcrApi, Value: "https://api.nn.ci/ocr/file/json", Type: conf.TypeString, Group: model.GLOBAL}, {Key: conf.OcrApi, Value: "https://api.nn.ci/ocr/file/json", Type: conf.TypeString, Group: model.GLOBAL},
{Key: conf.FilenameCharMapping, Value: `{"/": "|"}`, Type: conf.TypeText, Group: model.GLOBAL}, {Key: conf.FilenameCharMapping, Value: `{"/": "|"}`, Type: conf.TypeText, Group: model.GLOBAL},
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.GLOBAL},
// aria2 settings // aria2 settings
{Key: conf.Aria2Uri, Value: "http://localhost:6800/jsonrpc", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE}, {Key: conf.Aria2Uri, Value: "http://localhost:6800/jsonrpc", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
{Key: conf.Aria2Secret, Value: "", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE}, {Key: conf.Aria2Secret, Value: "", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
// single settings // single settings
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE}, {Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.SINGLE},
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE}, {Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
} }
if flags.Dev { if flags.Dev {

View File

@ -4,44 +4,43 @@ import (
"fmt" "fmt"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/generic_sync"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var settingsMap map[string]string var settingsMap generic_sync.MapOf[string, string]
var publicSettingsMap map[string]string var publicSettingsMap generic_sync.MapOf[string, string]
func settingsUpdate() { func settingsUpdate() {
settingsMap = nil settingsMap.Clear()
publicSettingsMap = nil publicSettingsMap.Clear()
} }
func GetPublicSettingsMap() map[string]string { func GetPublicSettingsMap() map[string]string {
if publicSettingsMap == nil { if publicSettingsMap.Empty() {
publicSettingsMap = make(map[string]string)
settingItems, err := GetPublicSettingItems() settingItems, err := GetPublicSettingItems()
if err != nil { if err != nil {
log.Errorf("failed to get settingItems: %+v", err) log.Errorf("failed to get settingItems: %+v", err)
} }
for _, settingItem := range settingItems { for _, settingItem := range settingItems {
publicSettingsMap[settingItem.Key] = settingItem.Value publicSettingsMap.Store(settingItem.Key, settingItem.Value)
} }
} }
return publicSettingsMap return publicSettingsMap.ToMap()
} }
func GetSettingsMap() map[string]string { func GetSettingsMap() *generic_sync.MapOf[string, string] {
if settingsMap == nil { if settingsMap.Empty() {
settingsMap = make(map[string]string)
settingItems, err := GetSettingItems() settingItems, err := GetSettingItems()
if err != nil { if err != nil {
log.Errorf("failed to get settingItems: %+v", err) log.Errorf("failed to get settingItems: %+v", err)
} }
for _, settingItem := range settingItems { for _, settingItem := range settingItems {
settingsMap[settingItem.Key] = settingItem.Value settingsMap.Store(settingItem.Key, settingItem.Value)
} }
} }
return settingsMap return &settingsMap
} }
func GetSettingItems() ([]model.SettingItem, error) { func GetSettingItems() ([]model.SettingItem, error) {

View File

@ -7,7 +7,7 @@ import (
) )
func GetStr(key string, defaultValue ...string) string { func GetStr(key string, defaultValue ...string) string {
val, ok := db.GetSettingsMap()[key] val, ok := db.GetSettingsMap().Load(key)
if !ok { if !ok {
if len(defaultValue) > 0 { if len(defaultValue) > 0 {
return defaultValue[0] return defaultValue[0]

View File

@ -347,6 +347,23 @@ func (m *MapOf[K, V]) Values() []V {
return values return values
} }
func (m *MapOf[K, V]) Count() int {
return len(m.dirty)
}
func (m *MapOf[K, V]) Empty() bool {
return m.Count() == 0
}
func (m *MapOf[K, V]) ToMap() map[K]V {
ans := make(map[K]V)
m.Range(func(key K, value V) bool {
ans[key] = value
return true
})
return ans
}
func (m *MapOf[K, V]) Clear() { func (m *MapOf[K, V]) Clear() {
m.Range(func(key K, value V) bool { m.Range(func(key K, value V) bool {
m.Delete(key) m.Delete(key)