调整定时任务执行时间,优化价格更新逻辑

- 修改 InitCronJobs 函数中的定时任务执行时间,OpenRouter价格获取任务和其他厂商价格更新任务分别调整为每天06:04和06:30执行。
- 在 UpdateOtherPrices 函数中新增清除倍率缓存的逻辑,确保价格更新后缓存数据的准确性。
- 优化 GetPriceRates 函数,移除不必要的字段查询,提升查询效率。
This commit is contained in:
wood chen 2025-03-22 09:40:04 +08:00
parent 9c4684b0e2
commit b11cf228e3
4 changed files with 10 additions and 5 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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,