Modify: GetSettingByName returns string

This commit is contained in:
HFO4 2019-11-09 20:32:15 +08:00
parent f453b3e918
commit 29fe6047a1
4 changed files with 27 additions and 11 deletions

2
.gitignore vendored
View file

@ -12,7 +12,7 @@
*.out
# Development enviroment
.idea/
.idea/*
# Version control
version.lock

View file

@ -13,7 +13,7 @@ import (
// DB 数据库链接单例
var DB *gorm.DB
// Database 在中间件中初始化mysql链接
// Database 初始化 MySQL 链接
func Init() {
util.Log().Info("初始化数据库连接\n")

View file

@ -12,15 +12,24 @@ type Setting struct {
Value string `gorm:"size:65535"`
}
// settingCache 设置项缓存
var settingCache = make(map[string]string)
// GetSettingByName 用 Name 获取设置值
func GetSettingByName(name string) (Setting, error) {
func GetSettingByName(name string) (string, error) {
var setting Setting
// 优先尝试数据库中查找
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
return setting, nil
// 优先从缓存中查找
if optionValue, ok := settingCache[name]; ok {
return optionValue, nil
} else {
// 尝试数据库中查找
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
settingCache[setting.Name] = setting.Value
return setting.Value, nil
}
return "", result.Error
}
return setting, result.Error
}

View file

@ -1,6 +1,7 @@
package service
import (
"cloudreve/models"
"cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
)
@ -15,8 +16,14 @@ type UserLoginService struct {
// Login 用户登录函数
func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
return serializer.Response{
Code: 0,
Msg: "OK",
siteName, err := model.GetSettingByName("siteName")
if err == nil {
return serializer.Response{
Code: 0,
Msg: siteName,
}
} else {
return serializer.Err(5001, "无法获取参数值", err)
}
}