mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-18 13:54:46 +08:00
refactor(service): 将 AccountUsageService 的包级缓存改为依赖注入
This commit is contained in:
@@ -91,7 +91,8 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|||||||
rateLimitService := service.NewRateLimitService(accountRepository, usageLogRepository, configConfig, geminiQuotaService)
|
rateLimitService := service.NewRateLimitService(accountRepository, usageLogRepository, configConfig, geminiQuotaService)
|
||||||
claudeUsageFetcher := repository.NewClaudeUsageFetcher()
|
claudeUsageFetcher := repository.NewClaudeUsageFetcher()
|
||||||
antigravityQuotaFetcher := service.NewAntigravityQuotaFetcher(proxyRepository)
|
antigravityQuotaFetcher := service.NewAntigravityQuotaFetcher(proxyRepository)
|
||||||
accountUsageService := service.NewAccountUsageService(accountRepository, usageLogRepository, claudeUsageFetcher, geminiQuotaService, antigravityQuotaFetcher)
|
usageCache := service.NewUsageCache()
|
||||||
|
accountUsageService := service.NewAccountUsageService(accountRepository, usageLogRepository, claudeUsageFetcher, geminiQuotaService, antigravityQuotaFetcher, usageCache)
|
||||||
geminiTokenCache := repository.NewGeminiTokenCache(redisClient)
|
geminiTokenCache := repository.NewGeminiTokenCache(redisClient)
|
||||||
geminiTokenProvider := service.NewGeminiTokenProvider(accountRepository, geminiTokenCache, geminiOAuthService)
|
geminiTokenProvider := service.NewGeminiTokenProvider(accountRepository, geminiTokenCache, geminiOAuthService)
|
||||||
gatewayCache := repository.NewGatewayCache(redisClient)
|
gatewayCache := repository.NewGatewayCache(redisClient)
|
||||||
|
|||||||
@@ -69,13 +69,29 @@ type windowStatsCache struct {
|
|||||||
timestamp time.Time
|
timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// antigravityUsageCache 缓存 Antigravity 额度数据
|
||||||
apiCacheMap = sync.Map{} // 缓存 API 响应
|
type antigravityUsageCache struct {
|
||||||
windowStatsCacheMap = sync.Map{} // 缓存窗口统计
|
usageInfo *UsageInfo
|
||||||
|
timestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
apiCacheTTL = 10 * time.Minute
|
apiCacheTTL = 10 * time.Minute
|
||||||
windowStatsCacheTTL = 1 * time.Minute
|
windowStatsCacheTTL = 1 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UsageCache 封装账户使用量相关的缓存
|
||||||
|
type UsageCache struct {
|
||||||
|
apiCache sync.Map // accountID -> *apiUsageCache
|
||||||
|
windowStatsCache sync.Map // accountID -> *windowStatsCache
|
||||||
|
antigravityCache sync.Map // accountID -> *antigravityUsageCache
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUsageCache 创建 UsageCache 实例
|
||||||
|
func NewUsageCache() *UsageCache {
|
||||||
|
return &UsageCache{}
|
||||||
|
}
|
||||||
|
|
||||||
// WindowStats 窗口期统计
|
// WindowStats 窗口期统计
|
||||||
type WindowStats struct {
|
type WindowStats struct {
|
||||||
Requests int64 `json:"requests"`
|
Requests int64 `json:"requests"`
|
||||||
@@ -138,6 +154,7 @@ type AccountUsageService struct {
|
|||||||
usageFetcher ClaudeUsageFetcher
|
usageFetcher ClaudeUsageFetcher
|
||||||
geminiQuotaService *GeminiQuotaService
|
geminiQuotaService *GeminiQuotaService
|
||||||
antigravityQuotaFetcher *AntigravityQuotaFetcher
|
antigravityQuotaFetcher *AntigravityQuotaFetcher
|
||||||
|
cache *UsageCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAccountUsageService 创建AccountUsageService实例
|
// NewAccountUsageService 创建AccountUsageService实例
|
||||||
@@ -147,6 +164,7 @@ func NewAccountUsageService(
|
|||||||
usageFetcher ClaudeUsageFetcher,
|
usageFetcher ClaudeUsageFetcher,
|
||||||
geminiQuotaService *GeminiQuotaService,
|
geminiQuotaService *GeminiQuotaService,
|
||||||
antigravityQuotaFetcher *AntigravityQuotaFetcher,
|
antigravityQuotaFetcher *AntigravityQuotaFetcher,
|
||||||
|
cache *UsageCache,
|
||||||
) *AccountUsageService {
|
) *AccountUsageService {
|
||||||
return &AccountUsageService{
|
return &AccountUsageService{
|
||||||
accountRepo: accountRepo,
|
accountRepo: accountRepo,
|
||||||
@@ -154,6 +172,7 @@ func NewAccountUsageService(
|
|||||||
usageFetcher: usageFetcher,
|
usageFetcher: usageFetcher,
|
||||||
geminiQuotaService: geminiQuotaService,
|
geminiQuotaService: geminiQuotaService,
|
||||||
antigravityQuotaFetcher: antigravityQuotaFetcher,
|
antigravityQuotaFetcher: antigravityQuotaFetcher,
|
||||||
|
cache: cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +200,7 @@ func (s *AccountUsageService) GetUsage(ctx context.Context, accountID int64) (*U
|
|||||||
var apiResp *ClaudeUsageResponse
|
var apiResp *ClaudeUsageResponse
|
||||||
|
|
||||||
// 1. 检查 API 缓存(10 分钟)
|
// 1. 检查 API 缓存(10 分钟)
|
||||||
if cached, ok := apiCacheMap.Load(accountID); ok {
|
if cached, ok := s.cache.apiCache.Load(accountID); ok {
|
||||||
if cache, ok := cached.(*apiUsageCache); ok && time.Since(cache.timestamp) < apiCacheTTL {
|
if cache, ok := cached.(*apiUsageCache); ok && time.Since(cache.timestamp) < apiCacheTTL {
|
||||||
apiResp = cache.response
|
apiResp = cache.response
|
||||||
}
|
}
|
||||||
@@ -194,7 +213,7 @@ func (s *AccountUsageService) GetUsage(ctx context.Context, accountID int64) (*U
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// 缓存 API 响应
|
// 缓存 API 响应
|
||||||
apiCacheMap.Store(accountID, &apiUsageCache{
|
s.cache.apiCache.Store(accountID, &apiUsageCache{
|
||||||
response: apiResp,
|
response: apiResp,
|
||||||
timestamp: time.Now(),
|
timestamp: time.Now(),
|
||||||
})
|
})
|
||||||
@@ -252,14 +271,6 @@ func (s *AccountUsageService) getGeminiUsage(ctx context.Context, account *Accou
|
|||||||
return usage, nil
|
return usage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// antigravityUsageCache 缓存 Antigravity 额度数据
|
|
||||||
type antigravityUsageCache struct {
|
|
||||||
usageInfo *UsageInfo
|
|
||||||
timestamp time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
var antigravityCacheMap = sync.Map{}
|
|
||||||
|
|
||||||
// getAntigravityUsage 获取 Antigravity 账户额度
|
// getAntigravityUsage 获取 Antigravity 账户额度
|
||||||
func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *Account) (*UsageInfo, error) {
|
func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *Account) (*UsageInfo, error) {
|
||||||
if s.antigravityQuotaFetcher == nil || !s.antigravityQuotaFetcher.CanFetch(account) {
|
if s.antigravityQuotaFetcher == nil || !s.antigravityQuotaFetcher.CanFetch(account) {
|
||||||
@@ -268,7 +279,7 @@ func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 1. 检查缓存(10 分钟)
|
// 1. 检查缓存(10 分钟)
|
||||||
if cached, ok := antigravityCacheMap.Load(account.ID); ok {
|
if cached, ok := s.cache.antigravityCache.Load(account.ID); ok {
|
||||||
if cache, ok := cached.(*antigravityUsageCache); ok && time.Since(cache.timestamp) < apiCacheTTL {
|
if cache, ok := cached.(*antigravityUsageCache); ok && time.Since(cache.timestamp) < apiCacheTTL {
|
||||||
// 重新计算 RemainingSeconds
|
// 重新计算 RemainingSeconds
|
||||||
usage := cache.usageInfo
|
usage := cache.usageInfo
|
||||||
@@ -289,7 +300,7 @@ func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. 缓存结果
|
// 4. 缓存结果
|
||||||
antigravityCacheMap.Store(account.ID, &antigravityUsageCache{
|
s.cache.antigravityCache.Store(account.ID, &antigravityUsageCache{
|
||||||
usageInfo: result.UsageInfo,
|
usageInfo: result.UsageInfo,
|
||||||
timestamp: time.Now(),
|
timestamp: time.Now(),
|
||||||
})
|
})
|
||||||
@@ -308,7 +319,7 @@ func (s *AccountUsageService) addWindowStats(ctx context.Context, account *Accou
|
|||||||
|
|
||||||
// 检查窗口统计缓存(1 分钟)
|
// 检查窗口统计缓存(1 分钟)
|
||||||
var windowStats *WindowStats
|
var windowStats *WindowStats
|
||||||
if cached, ok := windowStatsCacheMap.Load(account.ID); ok {
|
if cached, ok := s.cache.windowStatsCache.Load(account.ID); ok {
|
||||||
if cache, ok := cached.(*windowStatsCache); ok && time.Since(cache.timestamp) < windowStatsCacheTTL {
|
if cache, ok := cached.(*windowStatsCache); ok && time.Since(cache.timestamp) < windowStatsCacheTTL {
|
||||||
windowStats = cache.stats
|
windowStats = cache.stats
|
||||||
}
|
}
|
||||||
@@ -336,7 +347,7 @@ func (s *AccountUsageService) addWindowStats(ctx context.Context, account *Accou
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 缓存窗口统计(1 分钟)
|
// 缓存窗口统计(1 分钟)
|
||||||
windowStatsCacheMap.Store(account.ID, &windowStatsCache{
|
s.cache.windowStatsCache.Store(account.ID, &windowStatsCache{
|
||||||
stats: windowStats,
|
stats: windowStats,
|
||||||
timestamp: time.Now(),
|
timestamp: time.Now(),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -322,9 +322,6 @@ func (s *AntigravityGatewayService) Forward(ctx context.Context, c *gin.Context,
|
|||||||
|
|
||||||
originalModel := claudeReq.Model
|
originalModel := claudeReq.Model
|
||||||
mappedModel := s.getMappedModel(account, claudeReq.Model)
|
mappedModel := s.getMappedModel(account, claudeReq.Model)
|
||||||
if mappedModel != claudeReq.Model {
|
|
||||||
log.Printf("Antigravity model mapping: %s -> %s (account: %s)", claudeReq.Model, mappedModel, account.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取 access_token
|
// 获取 access_token
|
||||||
if s.tokenProvider == nil {
|
if s.tokenProvider == nil {
|
||||||
|
|||||||
@@ -114,4 +114,5 @@ var ProviderSet = wire.NewSet(
|
|||||||
ProvideDeferredService,
|
ProvideDeferredService,
|
||||||
NewAntigravityQuotaFetcher,
|
NewAntigravityQuotaFetcher,
|
||||||
NewUserAttributeService,
|
NewUserAttributeService,
|
||||||
|
NewUsageCache,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user