IncreaseTokenUsageCount 增加Token的使用次数
(key string)
| 389 | |
| 390 | // IncreaseTokenUsageCount 增加Token的使用次数 |
| 391 | func IncreaseTokenUsageCount(key string) error { |
| 392 | if key == "" { |
| 393 | return errors.New("key 不能为空") |
| 394 | } |
| 395 | |
| 396 | // 获取当前日期 |
| 397 | currentDate := common.GetTimeString()[:10] // YYYY-MM-DD格式 |
| 398 | |
| 399 | // 更新数据库 |
| 400 | err := DB.Model(&Token{}).Where(commonKeyCol+" = ?", key).Updates(map[string]interface{}{ |
| 401 | "total_usage_count": gorm.Expr("total_usage_count + 1"), |
| 402 | "daily_usage_count": gorm.Expr("CASE WHEN last_usage_date = ? THEN daily_usage_count + 1 ELSE 1 END", currentDate), |
| 403 | "last_usage_date": currentDate, |
| 404 | "accessed_time": common.GetTimestamp(), |
| 405 | }).Error |
| 406 | |
| 407 | // 更新缓存 |
| 408 | if common.RedisEnabled && err == nil { |
| 409 | gopool.Go(func() { |
| 410 | // 重新缓存Token信息 |
| 411 | token, getErr := GetTokenByKey(key, true) // 从DB获取最新数据 |
| 412 | if getErr == nil { |
| 413 | _ = cacheSetToken(*token) |
| 414 | } |
| 415 | }) |
| 416 | } |
| 417 | |
| 418 | return err |
| 419 | } |
| 420 | |
| 421 | // CheckRateLimit 检查令牌的访问频率限制 |
| 422 | func CheckRateLimit(token *Token) error { |
no test coverage detected