From b11cf228e3f60e169187fd4b6738fcdadc10f5d6 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 22 Mar 2025 09:40:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4=EF=BC=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BB=B7=E6=A0=BC=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 InitCronJobs 函数中的定时任务执行时间,OpenRouter价格获取任务和其他厂商价格更新任务分别调整为每天06:04和06:30执行。 - 在 UpdateOtherPrices 函数中新增清除倍率缓存的逻辑,确保价格更新后缓存数据的准确性。 - 优化 GetPriceRates 函数,移除不必要的字段查询,提升查询效率。 --- backend/cron/main.go | 4 ++-- backend/cron/openrouter-api/update-other-price.go | 5 +++++ backend/handlers/prices.go | 3 ++- backend/handlers/rates/price_rates.go | 3 +-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/cron/main.go b/backend/cron/main.go index 070336c..42be8da 100644 --- a/backend/cron/main.go +++ b/backend/cron/main.go @@ -20,7 +20,7 @@ func InitCronJobs() { // 注册OpenRouter价格获取任务 // 每24小时执行一次 - _, err := cronScheduler.AddFunc("0 0 0 * * *", func() { + _, err := cronScheduler.AddFunc("4 6 * * *", func() { if err := openrouter_api.FetchAndSavePrices(); err != nil { log.Printf("OpenRouter价格获取任务执行失败: %v", err) } @@ -32,7 +32,7 @@ func InitCronJobs() { // 注册其他厂商价格更新任务 // 每24小时执行一次,错开时间避免同时执行 - _, err = cronScheduler.AddFunc("0 30 0 * * *", func() { + _, err = cronScheduler.AddFunc("30 6 * * *", func() { if err := openrouter_api.UpdateOtherPrices(); err != nil { log.Printf("其他厂商价格更新任务执行失败: %v", err) } diff --git a/backend/cron/openrouter-api/update-other-price.go b/backend/cron/openrouter-api/update-other-price.go index 0a19165..5c2bbd0 100644 --- a/backend/cron/openrouter-api/update-other-price.go +++ b/backend/cron/openrouter-api/update-other-price.go @@ -11,6 +11,7 @@ import ( "aimodels-prices/database" "aimodels-prices/handlers" + "aimodels-prices/handlers/rates" "aimodels-prices/models" ) @@ -229,6 +230,10 @@ func UpdateOtherPrices() error { } log.Printf("其他厂商价格数据处理完成,成功处理: %d, 跳过: %d", processedCount, skippedCount) + + // 清除倍率缓存 + rates.ClearRatesCache() + log.Println("倍率缓存已清除") return nil } diff --git a/backend/handlers/prices.go b/backend/handlers/prices.go index cfb1949..65903c2 100644 --- a/backend/handlers/prices.go +++ b/backend/handlers/prices.go @@ -103,7 +103,7 @@ func GetPrices(c *gin.Context) { c.JSON(http.StatusOK, result) } -// processPrice 处理价格的创建和更新逻辑 +// processPrice 处理价格的创建和更新逻辑,只负责处理业务逻辑 func ProcessPrice(price models.Price, existingPrice *models.Price, isAdmin bool, username string) (models.Price, bool, error) { // 如果是更新操作且存在现有记录 if existingPrice != nil { @@ -212,6 +212,7 @@ func ProcessPrice(price models.Price, existingPrice *models.Price, isAdmin bool, return price, false, err } return price, true, nil + } } diff --git a/backend/handlers/rates/price_rates.go b/backend/handlers/rates/price_rates.go index f45a344..b4350ef 100644 --- a/backend/handlers/rates/price_rates.go +++ b/backend/handlers/rates/price_rates.go @@ -34,7 +34,7 @@ func GetPriceRates(c *gin.Context) { // 使用索引优化查询,只查询需要的字段 var prices []models.Price - if err := database.DB.Select("model, model_type, billing_type, channel_type, input_price, output_price, currency, status"). + if err := database.DB.Select("model, billing_type, channel_type, input_price, output_price, currency, status"). Where("status = 'approved'"). Find(&prices).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch prices"}) @@ -61,7 +61,6 @@ func GetPriceRates(c *gin.Context) { rates = append(rates, PriceRate{ Model: price.Model, - ModelType: price.ModelType, Type: price.BillingType, ChannelType: price.ChannelType, Input: inputRate,