feat: make 5m cache-creation ratio configurable

This commit is contained in:
Seefs
2026-02-06 19:46:59 +08:00
parent 5ff9bc3851
commit 50ee4361d0
13 changed files with 99 additions and 5 deletions

View File

@@ -101,6 +101,9 @@ var defaultCreateCacheRatio = map[string]float64{
var cacheRatioMap map[string]float64
var cacheRatioMapMutex sync.RWMutex
var createCacheRatioMap map[string]float64
var createCacheRatioMapMutex sync.RWMutex
// GetCacheRatioMap returns the cache ratio map
func GetCacheRatioMap() map[string]float64 {
cacheRatioMapMutex.RLock()
@@ -119,6 +122,17 @@ func CacheRatio2JSONString() string {
return string(jsonBytes)
}
// CreateCacheRatio2JSONString converts the create cache ratio map to a JSON string
func CreateCacheRatio2JSONString() string {
createCacheRatioMapMutex.RLock()
defer createCacheRatioMapMutex.RUnlock()
jsonBytes, err := json.Marshal(createCacheRatioMap)
if err != nil {
common.SysLog("error marshalling create cache ratio: " + err.Error())
}
return string(jsonBytes)
}
// UpdateCacheRatioByJSONString updates the cache ratio map from a JSON string
func UpdateCacheRatioByJSONString(jsonStr string) error {
cacheRatioMapMutex.Lock()
@@ -131,6 +145,18 @@ func UpdateCacheRatioByJSONString(jsonStr string) error {
return err
}
// UpdateCreateCacheRatioByJSONString updates the create cache ratio map from a JSON string
func UpdateCreateCacheRatioByJSONString(jsonStr string) error {
createCacheRatioMapMutex.Lock()
defer createCacheRatioMapMutex.Unlock()
createCacheRatioMap = make(map[string]float64)
err := json.Unmarshal([]byte(jsonStr), &createCacheRatioMap)
if err == nil {
InvalidateExposedDataCache()
}
return err
}
// GetCacheRatio returns the cache ratio for a model
func GetCacheRatio(name string) (float64, bool) {
cacheRatioMapMutex.RLock()
@@ -143,7 +169,9 @@ func GetCacheRatio(name string) (float64, bool) {
}
func GetCreateCacheRatio(name string) (float64, bool) {
ratio, ok := defaultCreateCacheRatio[name]
createCacheRatioMapMutex.RLock()
defer createCacheRatioMapMutex.RUnlock()
ratio, ok := createCacheRatioMap[name]
if !ok {
return 1.25, false // Default to 1.25 if not found
}
@@ -159,3 +187,13 @@ func GetCacheRatioCopy() map[string]float64 {
}
return copyMap
}
func GetCreateCacheRatioCopy() map[string]float64 {
createCacheRatioMapMutex.RLock()
defer createCacheRatioMapMutex.RUnlock()
copyMap := make(map[string]float64, len(createCacheRatioMap))
for k, v := range createCacheRatioMap {
copyMap[k] = v
}
return copyMap
}

View File

@@ -42,10 +42,11 @@ func GetExposedData() gin.H {
return cloneGinH(c.data)
}
newData := gin.H{
"model_ratio": GetModelRatioCopy(),
"completion_ratio": GetCompletionRatioCopy(),
"cache_ratio": GetCacheRatioCopy(),
"model_price": GetModelPriceCopy(),
"model_ratio": GetModelRatioCopy(),
"completion_ratio": GetCompletionRatioCopy(),
"cache_ratio": GetCacheRatioCopy(),
"create_cache_ratio": GetCreateCacheRatioCopy(),
"model_price": GetModelPriceCopy(),
}
exposedData.Store(&exposedCache{
data: newData,

View File

@@ -362,6 +362,11 @@ func InitRatioSettings() {
cacheRatioMap = defaultCacheRatio
cacheRatioMapMutex.Unlock()
// Initialize createCacheRatioMap (5m cache creation ratio)
createCacheRatioMapMutex.Lock()
createCacheRatioMap = defaultCreateCacheRatio
createCacheRatioMapMutex.Unlock()
// initialize imageRatioMap
imageRatioMapMutex.Lock()
imageRatioMap = defaultImageRatio