feat: 去除设置层的缓存

This commit is contained in:
耗子
2026-07-25 00:05:59 +08:00
parent 634e361e86
commit aca88ffeea
3 changed files with 8 additions and 41 deletions
-1
View File
@@ -34,7 +34,6 @@ require (
github.com/libdns/porkbun v1.1.0
github.com/libdns/tencentcloud v1.4.3
github.com/libdns/westcn v1.0.2
github.com/libtnb/cache v1.3.0
github.com/libtnb/chix/v2 v2.1.1
github.com/libtnb/cron v0.5.2
github.com/libtnb/gormstore v1.3.0
-2
View File
@@ -260,8 +260,6 @@ github.com/libdns/westcn v1.0.2 h1:PA2M3tME5/0T3klPMzSHvGk1EWnGNNkJiKxaJjFalnM=
github.com/libdns/westcn v1.0.2/go.mod h1:iKpk8jjOU+793Yp8nHoihPTCR6N1KWtm4/r8BB9mVnk=
github.com/libtnb/acmez/v3 v3.0.0-20260406165834-a264acd02292 h1:FF1ZlwFFjpwT6CtBodr/NV4OMJPMC7plTmfzqhXrlSk=
github.com/libtnb/acmez/v3 v3.0.0-20260406165834-a264acd02292/go.mod h1:5nTPosTGosLxF3+LU4ygbgMRFDhbAVpqMI4+a4aHLBY=
github.com/libtnb/cache v1.3.0 h1:hrsbC79gdByKeiiH119WUypXnhWg6U+LiP7WCOisgjU=
github.com/libtnb/cache v1.3.0/go.mod h1:kArfr9kzJdtryO5FzLVirY5bxFKXf4rok32MK5/F1Ts=
github.com/libtnb/chix/v2 v2.1.1 h1:QQwHXUyPaUUp/KOwzXf2NB0IRKb9v2pqIdOIItzBPiE=
github.com/libtnb/chix/v2 v2.1.1/go.mod h1:OEY9v0976U/IJzP6n8VdhgXH17VL2muo3EQWsIUyxm8=
github.com/libtnb/cron v0.5.2 h1:Yy9a/S82bAS4KzE1SSMjNLtigPkejgMPUp9BHGeb1pw=
+8 -38
View File
@@ -3,11 +3,8 @@ package data
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"time"
"github.com/libtnb/cache"
"github.com/samber/do/v2"
"github.com/spf13/cast"
"gorm.io/gorm"
@@ -20,19 +17,15 @@ import (
"github.com/acepanel/panel/v3/pkg/io"
)
const settingCacheTTL = 5 * time.Minute
type settingRepo struct {
db *gorm.DB
conf *config.Config
cache cache.Cache
db *gorm.DB
conf *config.Config
}
func NewSettingRepo(i do.Injector) (biz.SettingRepo, error) {
return &settingRepo{
db: do.MustInvoke[*gorm.DB](i),
conf: do.MustInvoke[*config.Config](i),
cache: cache.NewCache(),
db: do.MustInvoke[*gorm.DB](i),
conf: do.MustInvoke[*config.Config](i),
}, nil
}
@@ -101,15 +94,10 @@ func (r *settingRepo) GetSlice(key biz.SettingKey, defaultValue ...[]string) ([]
}
func (r *settingRepo) Set(key biz.SettingKey, value string) error {
if err := r.db.Clauses(clause.OnConflict{
return r.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "key"}},
DoUpdates: clause.AssignmentColumns([]string{"value"}),
}).Create(&biz.Setting{Key: key, Value: value}).Error; err != nil {
return err
}
r.cache.Forget(r.cacheKey(key))
return nil
}).Create(&biz.Setting{Key: key, Value: value}).Error
}
func (r *settingRepo) SetSlice(key biz.SettingKey, value []string) error {
@@ -126,13 +114,7 @@ func (r *settingRepo) SetSlice(key biz.SettingKey, value []string) error {
}
func (r *settingRepo) Delete(key biz.SettingKey) error {
setting := new(biz.Setting)
if err := r.db.Where("key = ?", key).Delete(setting).Error; err != nil {
return err
}
r.cache.Forget(r.cacheKey(key))
return nil
return r.db.Where("key = ?", key).Delete(new(biz.Setting)).Error
}
func (r *settingRepo) GetPanel() (*request.SettingPanel, error) {
@@ -236,25 +218,13 @@ func (r *settingRepo) GetPanel() (*request.SettingPanel, error) {
}, nil
}
// cacheKey 生成设置项的缓存 key
func (r *settingRepo) cacheKey(key biz.SettingKey) string {
return fmt.Sprintf("setting:%s", key)
}
// getRaw 从缓存或数据库获取设置项的原始字符串值
// getRaw 从数据库获取设置项的原始字符串值
func (r *settingRepo) getRaw(key biz.SettingKey) (string, error) {
cacheKey := r.cacheKey(key)
if r.cache.Has(cacheKey) {
return r.cache.GetString(cacheKey), nil
}
setting := new(biz.Setting)
if err := r.db.Where("key = ?", key).First(setting).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
return "", err
}
}
_ = r.cache.Put(cacheKey, setting.Value, settingCacheTTL)
return setting.Value, nil
}