Skip to content

Commit

Permalink
perf: Speed ​​of database initialization (#7694)
Browse files Browse the repository at this point in the history
* perf: 优化非sqlite3数据库时初始化慢的问题

* refactor
  • Loading branch information
j2rong4cn authored Dec 25, 2024
1 parent b8bd14f commit db99224
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
48 changes: 29 additions & 19 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package data
import (
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/offline_download/tool"
"github.com/alist-org/alist/v3/internal/op"
Expand All @@ -21,44 +22,53 @@ func initSettings() {
if err != nil {
utils.Log.Fatalf("failed get settings: %+v", err)
}
for i := range settings {
if !isActive(settings[i].Key) && settings[i].Flag != model.DEPRECATED {
settings[i].Flag = model.DEPRECATED
err = op.SaveSettingItem(&settings[i])
settingMap := map[string]*model.SettingItem{}
for _, v := range settings {
if !isActive(v.Key) && v.Flag != model.DEPRECATED {
v.Flag = model.DEPRECATED
err = op.SaveSettingItem(&v)
if err != nil {
utils.Log.Fatalf("failed save setting: %+v", err)
}
}
settingMap[v.Key] = &v
}

// create or save setting
save := false
for i := range initialSettingItems {
item := &initialSettingItems[i]
item.Index = uint(i)
if item.PreDefault == "" {
item.PreDefault = item.Value
}
// err
stored, err := op.GetSettingItemByKey(item.Key)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
utils.Log.Fatalf("failed get setting: %+v", err)
continue
stored, ok := settingMap[item.Key]
if !ok {
stored, err = op.GetSettingItemByKey(item.Key)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
utils.Log.Fatalf("failed get setting: %+v", err)
continue
}
}
// save
if stored != nil && item.Key != conf.VERSION && stored.Value != item.PreDefault {
item.Value = stored.Value
}
_, err = op.HandleSettingItemHook(item)
if err != nil {
utils.Log.Errorf("failed to execute hook on %s: %+v", item.Key, err)
continue
}
// save
if stored == nil || *item != *stored {
err = op.SaveSettingItem(item)
if err != nil {
utils.Log.Fatalf("failed save setting: %+v", err)
}
save = true
}
}
if save {
err = db.SaveSettingItems(initialSettingItems)
if err != nil {
utils.Log.Fatalf("failed save setting: %+v", err)
} else {
// Not save so needs to execute hook
_, err = op.HandleSettingItemHook(item)
if err != nil {
utils.Log.Errorf("failed to execute hook on %s: %+v", item.Key, err)
}
op.SettingCacheUpdate()
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions internal/bootstrap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ func InitDB() {
}
case "mysql":
{
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode)
if database.DSN != "" {
dsn = database.DSN
dsn := database.DSN
if dsn == "" {
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode)
}
dB, err = gorm.Open(mysql.Open(dsn), gormConfig)
}
case "postgres":
{
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
database.Host, database.User, database.Password, database.Name, database.Port, database.SSLMode)
if database.DSN != "" {
dsn = database.DSN
dsn := database.DSN
if dsn == "" {
dsn = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
database.Host, database.User, database.Password, database.Name, database.Port, database.SSLMode)
}
dB, err = gorm.Open(postgres.Open(dsn), gormConfig)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/op/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var settingGroupCacheF = func(key string, item []model.SettingItem) {
settingGroupCache.Set(key, item, cache.WithEx[[]model.SettingItem](time.Hour))
}

func settingCacheUpdate() {
func SettingCacheUpdate() {
settingCache.Clear()
settingGroupCache.Clear()
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func SaveSettingItems(items []model.SettingItem) error {
}
}
if len(errs) < len(items)-len(noHookItems)+1 {
settingCacheUpdate()
SettingCacheUpdate()
}
return utils.MergeErrors(errs...)
}
Expand All @@ -181,7 +181,7 @@ func SaveSettingItem(item *model.SettingItem) (err error) {
if err = db.SaveSettingItem(item); err != nil {
return err
}
settingCacheUpdate()
SettingCacheUpdate()
return nil
}

Expand All @@ -193,6 +193,6 @@ func DeleteSettingItemByKey(key string) error {
if !old.IsDeprecated() {
return errors.Errorf("setting [%s] is not deprecated", key)
}
settingCacheUpdate()
SettingCacheUpdate()
return db.DeleteSettingItemByKey(key)
}
8 changes: 4 additions & 4 deletions server/common/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
func GetApiUrl(r *http.Request) string {
api := conf.Conf.SiteURL
if strings.HasPrefix(api, "http") {
return api
return strings.TrimSuffix(api, "/")
}
if r != nil {
protocol := "http"
if r.TLS != nil || r.Header.Get("X-Forwarded-Proto") == "https" {
protocol = "https"
}
host := r.Host
if r.Header.Get("X-Forwarded-Host") != "" {
host = r.Header.Get("X-Forwarded-Host")
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
api = fmt.Sprintf("%s://%s", protocol, stdpath.Join(host, api))
}
Expand Down

0 comments on commit db99224

Please sign in to comment.