mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 10:34:15 +08:00
更新底层架构
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"networkDev/database"
|
||||
"networkDev/models"
|
||||
"NetworkAuth/database"
|
||||
"NetworkAuth/models"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
@@ -53,6 +53,10 @@ func (s *SettingsService) loadAllSettings() {
|
||||
logrus.WithError(err).Error("获取数据库连接失败")
|
||||
return
|
||||
}
|
||||
// 如果数据库未初始化,直接返回,保持缓存为空
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var settings []models.Settings
|
||||
if err := db.Find(&settings).Error; err != nil {
|
||||
@@ -67,7 +71,24 @@ func (s *SettingsService) loadAllSettings() {
|
||||
s.cache[setting.Name] = setting.Value
|
||||
}
|
||||
|
||||
logrus.WithField("count", len(settings)).Info("设置缓存加载完成")
|
||||
logrus.WithField("count", len(settings)).Debug("设置缓存加载完成")
|
||||
}
|
||||
|
||||
// Set 设置值(用于测试或运行时更新)
|
||||
func (s *SettingsService) Set(name, value string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.cache[name] = value
|
||||
}
|
||||
|
||||
// GetSettingRealtime 实时获取设置值(优先使用Redis缓存,自动回源数据库)
|
||||
// 相比 GetString 的内存缓存,此方法能感知其他实例或直接数据库的变更(在Redis TTL过期后)
|
||||
func (s *SettingsService) GetSettingRealtime(name string) (*models.Settings, error) {
|
||||
db, err := database.GetDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FindSettingByName(name, db)
|
||||
}
|
||||
|
||||
// GetString 获取字符串类型的设置值
|
||||
@@ -118,3 +139,32 @@ func (s *SettingsService) GetSessionTimeout() int {
|
||||
func (s *SettingsService) IsMaintenanceMode() bool {
|
||||
return s.GetBool("maintenance_mode", false)
|
||||
}
|
||||
|
||||
// GetJWTSecret 获取JWT密钥
|
||||
func (s *SettingsService) GetJWTSecret() string {
|
||||
return s.GetString("jwt_secret", "")
|
||||
}
|
||||
|
||||
// GetEncryptionKey 获取加密密钥
|
||||
func (s *SettingsService) GetEncryptionKey() string {
|
||||
return s.GetString("encryption_key", "")
|
||||
}
|
||||
|
||||
// GetJWTRefresh 获取JWT刷新时间(小时)
|
||||
func (s *SettingsService) GetJWTRefresh() int {
|
||||
return s.GetInt("jwt_refresh", 6)
|
||||
}
|
||||
|
||||
// GetJWTExpire 获取JWT有效期(小时)
|
||||
func (s *SettingsService) GetJWTExpire() int {
|
||||
return s.GetInt("jwt_expire", 24)
|
||||
}
|
||||
|
||||
// GetCookieConfig 获取Cookie配置
|
||||
func (s *SettingsService) GetCookieConfig() (secure bool, sameSite string, domain string, maxAge int) {
|
||||
secure = s.GetBool("cookie_secure", true)
|
||||
sameSite = s.GetString("cookie_same_site", "Lax")
|
||||
domain = s.GetString("cookie_domain", "")
|
||||
maxAge = s.GetInt("cookie_max_age", 86400)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user