feat: add site config cached
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package settings_service
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SiteConfigCache 站点配置缓存
|
||||
type SiteConfigCache struct {
|
||||
data map[string]string
|
||||
lastUpdate time.Time
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
var siteConfigCache = &SiteConfigCache{
|
||||
data: make(map[string]string),
|
||||
}
|
||||
|
||||
// GetSiteConfigCache 获取站点配置缓存
|
||||
func GetSiteConfigCache() map[string]string {
|
||||
siteConfigCache.mutex.RLock()
|
||||
defer siteConfigCache.mutex.RUnlock()
|
||||
|
||||
// 返回缓存的副本,避免外部修改
|
||||
result := make(map[string]string)
|
||||
for k, v := range siteConfigCache.data {
|
||||
result[k] = v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// SetSiteConfigCache 设置站点配置缓存
|
||||
func SetSiteConfigCache(data map[string]string) {
|
||||
siteConfigCache.mutex.Lock()
|
||||
defer siteConfigCache.mutex.Unlock()
|
||||
|
||||
// 清空现有缓存
|
||||
siteConfigCache.data = make(map[string]string)
|
||||
|
||||
// 设置新数据
|
||||
for k, v := range data {
|
||||
siteConfigCache.data[k] = v
|
||||
}
|
||||
|
||||
// 更新缓存时间
|
||||
siteConfigCache.lastUpdate = time.Now()
|
||||
}
|
||||
|
||||
// ClearSiteConfigCache 清除站点配置缓存
|
||||
func ClearSiteConfigCache() {
|
||||
siteConfigCache.mutex.Lock()
|
||||
defer siteConfigCache.mutex.Unlock()
|
||||
|
||||
siteConfigCache.data = make(map[string]string)
|
||||
siteConfigCache.lastUpdate = time.Time{}
|
||||
}
|
||||
|
||||
// IsSiteConfigCacheValid 检查缓存是否有效(可选:可以设置缓存过期时间)
|
||||
func IsSiteConfigCacheValid() bool {
|
||||
siteConfigCache.mutex.RLock()
|
||||
defer siteConfigCache.mutex.RUnlock()
|
||||
|
||||
// 如果缓存为空,认为无效
|
||||
if len(siteConfigCache.data) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 可以在这里添加缓存过期逻辑,比如缓存5分钟后过期
|
||||
// if time.Since(siteConfigCache.lastUpdate) > 5*time.Minute {
|
||||
// return false
|
||||
// }
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -31,6 +31,13 @@ func (us *UserSettings) EditUserPasswd() error {
|
||||
|
||||
// GetUserSetting 获取用户设置
|
||||
func (us *UserSettings) GetUserSetting(section string) (map[string]string, error) {
|
||||
// 如果是site_config,优先从缓存获取
|
||||
if section == constant.SiteSettingSectionName {
|
||||
if IsSiteConfigCacheValid() {
|
||||
return GetSiteConfigCache(), nil
|
||||
}
|
||||
}
|
||||
|
||||
result := make(map[string]string)
|
||||
settings, err := models.GetSettingBySection(section)
|
||||
if err != nil {
|
||||
@@ -39,6 +46,12 @@ func (us *UserSettings) GetUserSetting(section string) (map[string]string, error
|
||||
for _, setting := range settings {
|
||||
result[setting.Key] = setting.Value
|
||||
}
|
||||
|
||||
// 如果是site_config,更新缓存
|
||||
if section == constant.SiteSettingSectionName {
|
||||
SetSiteConfigCache(result)
|
||||
}
|
||||
|
||||
// 版本信息单独获取
|
||||
if section == constant.AboutSectionName {
|
||||
result = constant.LatestVersion
|
||||
@@ -64,6 +77,10 @@ func (us *UserSettings) EditSettings(section string, key string, value string, c
|
||||
Key: key,
|
||||
Value: value,
|
||||
})
|
||||
// 如果是site_config,清除缓存
|
||||
if section == constant.SiteSettingSectionName {
|
||||
ClearSiteConfigCache()
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
if value == "" {
|
||||
@@ -79,6 +96,10 @@ func (us *UserSettings) EditSettings(section string, key string, value string, c
|
||||
cronService := cron_service.CronService{}
|
||||
cronService.UpdateLogsCronRun(value)
|
||||
}
|
||||
// 如果是site_config,清除缓存
|
||||
if section == constant.SiteSettingSectionName {
|
||||
ClearSiteConfigCache()
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ export default {
|
||||
|
||||
<!-- 站点图标 -->
|
||||
<div class="space-y-2">
|
||||
<label class="text-sm font-medium text-gray-700">站点图标</label>
|
||||
<label class="text-sm font-medium text-gray-700">站点图标(只支持svg文本)</label>
|
||||
<Input v-model="state.logo" placeholder="请输入自定义的网站logo(svg文本)" />
|
||||
<!-- SVG预览 -->
|
||||
<div v-if="state.logo" class="mt-2 p-3 border border-gray-200 rounded-md bg-gray-50">
|
||||
|
||||
Reference in New Issue
Block a user