xwwww's picture
Upload 888 files
305487b verified
package performance_setting
import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/setting/config"
)
// PerformanceSetting 性能设置配置
type PerformanceSetting struct {
// DiskCacheEnabled 是否启用磁盘缓存(磁盘换内存)
DiskCacheEnabled bool `json:"disk_cache_enabled"`
// DiskCacheThresholdMB 触发磁盘缓存的请求体大小阈值(MB)
DiskCacheThresholdMB int `json:"disk_cache_threshold_mb"`
// DiskCacheMaxSizeMB 磁盘缓存最大总大小(MB)
DiskCacheMaxSizeMB int `json:"disk_cache_max_size_mb"`
// DiskCachePath 磁盘缓存目录
DiskCachePath string `json:"disk_cache_path"`
}
// 默认配置
var performanceSetting = PerformanceSetting{
DiskCacheEnabled: false,
DiskCacheThresholdMB: 10, // 超过 10MB 使用磁盘缓存
DiskCacheMaxSizeMB: 1024, // 最大 1GB 磁盘缓存
DiskCachePath: "", // 空表示使用系统临时目录
}
func init() {
// 注册到全局配置管理器
config.GlobalConfig.Register("performance_setting", &performanceSetting)
// 同步初始配置到 common 包
syncToCommon()
}
// syncToCommon 将配置同步到 common 包
func syncToCommon() {
common.SetDiskCacheConfig(common.DiskCacheConfig{
Enabled: performanceSetting.DiskCacheEnabled,
ThresholdMB: performanceSetting.DiskCacheThresholdMB,
MaxSizeMB: performanceSetting.DiskCacheMaxSizeMB,
Path: performanceSetting.DiskCachePath,
})
}
// GetPerformanceSetting 获取性能设置
func GetPerformanceSetting() *PerformanceSetting {
return &performanceSetting
}
// UpdateAndSync 更新配置并同步到 common 包
// 当配置从数据库加载后,需要调用此函数同步
func UpdateAndSync() {
syncToCommon()
}
// GetCacheStats 获取缓存统计信息(代理到 common 包)
func GetCacheStats() common.DiskCacheStats {
return common.GetDiskCacheStats()
}
// ResetStats 重置统计信息
func ResetStats() {
common.ResetDiskCacheStats()
}