107 lines
3.3 KiB
Go
107 lines
3.3 KiB
Go
// Package sysconfig 系统参数配置域(Issue#3 方案B):system_config KV 表统一
|
|
// 读(Get 带 TTL 缓存)/写(Set UPSERT+缓存即刻失效)/列(List)。
|
|
// 消费方:access(系统默认接入码回退,见 KeyDefaultExtCode)、uplink 拉黑词表
|
|
// (uplink.blacklist.keywords,该域自刷新不走本缓存,页面改后 60s 生效)。
|
|
package sysconfig
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/logger"
|
|
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// KeyDefaultExtCode 系统默认接入码前缀(账号 ext_code 为空时回退;Issue#3 方案B:
|
|
// 账号级优先、系统级兜底——新账号开箱即有码校验,无需逐账号配置)。
|
|
const KeyDefaultExtCode = "cmpp.default_ext_code"
|
|
|
|
const defaultTTL = 30 * time.Second
|
|
|
|
type cacheEntry struct {
|
|
value string
|
|
expireAt time.Time
|
|
}
|
|
|
|
// Service 系统配置服务。
|
|
type Service struct {
|
|
db *gorm.DB
|
|
ttl time.Duration
|
|
|
|
mu sync.RWMutex
|
|
cache map[string]cacheEntry
|
|
}
|
|
|
|
// New 构造(ttl≤0=30s,与 access 账号缓存同量级;Set 主动失效兜底即刻生效)。
|
|
func New(db *gorm.DB, ttl time.Duration) *Service {
|
|
if ttl <= 0 {
|
|
ttl = defaultTTL
|
|
}
|
|
return &Service{db: db, ttl: ttl, cache: map[string]cacheEntry{}}
|
|
}
|
|
|
|
// Get 读配置(未配置/缺行=空串;DB 错误降级空串记日志且不缓存——校验语义=不拦,
|
|
// 与账号级 ext_code 缺省放行口径一致)。
|
|
func (s *Service) Get(ctx context.Context, key string) string {
|
|
now := time.Now()
|
|
s.mu.RLock()
|
|
e, ok := s.cache[key]
|
|
s.mu.RUnlock()
|
|
if ok && now.Before(e.expireAt) {
|
|
return e.value
|
|
}
|
|
var row entity.SystemConfig
|
|
err := s.db.WithContext(ctx).Where("cfg_key = ?", key).First(&row).Error
|
|
value := ""
|
|
if err == nil {
|
|
value = row.CfgValue
|
|
} else if err != gorm.ErrRecordNotFound {
|
|
logger.L().Warn("sysconfig_get_failed", zap.String("key", key), zap.Error(err))
|
|
return ""
|
|
}
|
|
s.mu.Lock()
|
|
s.cache[key] = cacheEntry{value: value, expireAt: now.Add(s.ttl)}
|
|
s.mu.Unlock()
|
|
return value
|
|
}
|
|
|
|
// List 全量(cfg_key 升序;管理页数据源)。
|
|
func (s *Service) List(ctx context.Context) ([]entity.SystemConfig, error) {
|
|
var rows []entity.SystemConfig
|
|
err := s.db.WithContext(ctx).Order("cfg_key").Find(&rows).Error
|
|
return rows, err
|
|
}
|
|
|
|
// Set UPSERT(uk cfg_key 冲突→更新值/备注/操作人/时间)并即刻失效该键缓存
|
|
// (改完即生效——区别于账号 ext_code 的 30s accCache 窗口,管理页操作即时可验)。
|
|
func (s *Service) Set(ctx context.Context, key, value, remark, updatedBy string) (*entity.SystemConfig, error) {
|
|
key = strings.TrimSpace(key)
|
|
if key == "" || len(key) > 64 {
|
|
return nil, errors.InvalidParam.WithMsg("cfg_key 须 1~64 字符")
|
|
}
|
|
if len(value) > 1024 {
|
|
return nil, errors.InvalidParam.WithMsg("cfg_value 超 1024 字符")
|
|
}
|
|
if len(remark) > 255 {
|
|
return nil, errors.InvalidParam.WithMsg("remark 超 255 字符")
|
|
}
|
|
row := &entity.SystemConfig{CfgKey: key, CfgValue: value, Remark: remark, UpdatedBy: updatedBy}
|
|
if err := s.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "cfg_key"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"cfg_value", "remark", "updated_by", "updated_at"}),
|
|
}).Create(row).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
s.mu.Lock()
|
|
delete(s.cache, key)
|
|
s.mu.Unlock()
|
|
return row, nil
|
|
}
|