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 *.out
# Development enviroment # Development enviroment
.idea/ .idea/*
# Version control # Version control
version.lock version.lock

View file

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

View file

@ -12,15 +12,24 @@ type Setting struct {
Value string `gorm:"size:65535"` Value string `gorm:"size:65535"`
} }
// settingCache 设置项缓存
var settingCache = make(map[string]string)
// GetSettingByName 用 Name 获取设置值 // GetSettingByName 用 Name 获取设置值
func GetSettingByName(name string) (Setting, error) { func GetSettingByName(name string) (string, error) {
var setting Setting var setting Setting
// 优先尝试数据库中查找 // 优先从缓存中查找
result := DB.Where("name = ?", name).First(&setting) if optionValue, ok := settingCache[name]; ok {
if result.Error == nil { return optionValue, nil
return setting, 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 package service
import ( import (
"cloudreve/models"
"cloudreve/pkg/serializer" "cloudreve/pkg/serializer"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -15,8 +16,14 @@ type UserLoginService struct {
// Login 用户登录函数 // Login 用户登录函数
func (service *UserLoginService) Login(c *gin.Context) serializer.Response { func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
return serializer.Response{ siteName, err := model.GetSettingByName("siteName")
Code: 0, if err == nil {
Msg: "OK", return serializer.Response{
Code: 0,
Msg: siteName,
}
} else {
return serializer.Err(5001, "无法获取参数值", err)
} }
} }