mirror of
https://github.com/AlistGo/alist.git
synced 2025-05-24 00:30:00 +08:00
✨ cache
This commit is contained in:
@ -1,6 +1,16 @@
|
||||
package model
|
||||
|
||||
import "github.com/Xhofe/alist/conf"
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
PUBLIC = iota
|
||||
PRIVATE
|
||||
CONST
|
||||
)
|
||||
|
||||
type SettingItem struct {
|
||||
Key string `json:"key" gorm:"primaryKey" validate:"required"`
|
||||
@ -10,11 +20,6 @@ type SettingItem struct {
|
||||
}
|
||||
|
||||
func SaveSettings(items []SettingItem) error {
|
||||
//tx := conf.DB.Begin()
|
||||
//for _,item := range items{
|
||||
// tx.Save(item)
|
||||
//}
|
||||
//tx.Commit()
|
||||
return conf.DB.Save(items).Error
|
||||
}
|
||||
|
||||
@ -25,3 +30,56 @@ func GetSettingByType(t int) (*[]SettingItem, error) {
|
||||
}
|
||||
return &items, nil
|
||||
}
|
||||
|
||||
func GetSettingByKey(key string) (*SettingItem, error) {
|
||||
var items SettingItem
|
||||
if err := conf.DB.Where("key = ?", key).First(&items).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &items, nil
|
||||
}
|
||||
|
||||
func initSettings() {
|
||||
log.Infof("init settings...")
|
||||
version, err := GetSettingByKey("version")
|
||||
if err != nil {
|
||||
log.Debugf("first run")
|
||||
version = &SettingItem{
|
||||
Key: "version",
|
||||
Value: "0.0.0",
|
||||
Description: "version",
|
||||
Type: CONST,
|
||||
}
|
||||
}
|
||||
settingsMap := map[string][]SettingItem{
|
||||
"2.0.0": {
|
||||
{
|
||||
Key: "title",
|
||||
Value: "Alist",
|
||||
Description: "title",
|
||||
Type: PUBLIC,
|
||||
},
|
||||
{
|
||||
Key: "password",
|
||||
Value: "alist",
|
||||
Description: "password",
|
||||
Type: PRIVATE,
|
||||
},
|
||||
{
|
||||
Key: "version",
|
||||
Value: "2.0.0",
|
||||
Description: "version",
|
||||
Type: CONST,
|
||||
},
|
||||
},
|
||||
}
|
||||
for k, v := range settingsMap {
|
||||
if utils.VersionCompare(k, version.Value) > 0 {
|
||||
log.Infof("writing [v%s] settings",k)
|
||||
err = SaveSettings(v)
|
||||
if err != nil {
|
||||
log.Fatalf("save settings error")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user