MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / IncreaseTokenUsageCount

Function IncreaseTokenUsageCount

model/token.go:391–419  ·  view source on GitHub ↗

IncreaseTokenUsageCount 增加Token的使用次数

(key string)

Source from the content-addressed store, hash-verified

389
390// IncreaseTokenUsageCount 增加Token的使用次数
391func 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 检查令牌的访问频率限制
422func CheckRateLimit(token *Token) error {

Callers 1

TokenAuthFunction · 0.92

Calls 4

GetTimeStringFunction · 0.92
GetTimestampFunction · 0.92
GetTokenByKeyFunction · 0.85
cacheSetTokenFunction · 0.85

Tested by

no test coverage detected