Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
d2aaf0b4
Commit
d2aaf0b4
authored
Jan 03, 2026
by
song
Browse files
refactor(service): 将 AccountUsageService 的包级缓存改为依赖注入
parent
0452f320
Changes
4
Hide whitespace changes
Inline
Side-by-side
backend/cmd/server/wire_gen.go
View file @
d2aaf0b4
...
...
@@ -91,7 +91,8 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
rateLimitService
:=
service
.
NewRateLimitService
(
accountRepository
,
usageLogRepository
,
configConfig
,
geminiQuotaService
)
claudeUsageFetcher
:=
repository
.
NewClaudeUsageFetcher
()
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
)
geminiTokenProvider
:=
service
.
NewGeminiTokenProvider
(
accountRepository
,
geminiTokenCache
,
geminiOAuthService
)
gatewayCache
:=
repository
.
NewGatewayCache
(
redisClient
)
...
...
backend/internal/service/account_usage_service.go
View file @
d2aaf0b4
...
...
@@ -69,13 +69,29 @@ type windowStatsCache struct {
timestamp
time
.
Time
}
var
(
apiCacheMap
=
sync
.
Map
{}
// 缓存 API 响应
windowStatsCacheMap
=
sync
.
Map
{}
// 缓存窗口统计
// antigravityUsageCache 缓存 Antigravity 额度数据
type
antigravityUsageCache
struct
{
usageInfo
*
UsageInfo
timestamp
time
.
Time
}
const
(
apiCacheTTL
=
10
*
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 窗口期统计
type
WindowStats
struct
{
Requests
int64
`json:"requests"`
...
...
@@ -138,6 +154,7 @@ type AccountUsageService struct {
usageFetcher
ClaudeUsageFetcher
geminiQuotaService
*
GeminiQuotaService
antigravityQuotaFetcher
*
AntigravityQuotaFetcher
cache
*
UsageCache
}
// NewAccountUsageService 创建AccountUsageService实例
...
...
@@ -147,6 +164,7 @@ func NewAccountUsageService(
usageFetcher
ClaudeUsageFetcher
,
geminiQuotaService
*
GeminiQuotaService
,
antigravityQuotaFetcher
*
AntigravityQuotaFetcher
,
cache
*
UsageCache
,
)
*
AccountUsageService
{
return
&
AccountUsageService
{
accountRepo
:
accountRepo
,
...
...
@@ -154,6 +172,7 @@ func NewAccountUsageService(
usageFetcher
:
usageFetcher
,
geminiQuotaService
:
geminiQuotaService
,
antigravityQuotaFetcher
:
antigravityQuotaFetcher
,
cache
:
cache
,
}
}
...
...
@@ -181,7 +200,7 @@ func (s *AccountUsageService) GetUsage(ctx context.Context, accountID int64) (*U
var
apiResp
*
ClaudeUsageResponse
// 1. 检查 API 缓存(10 分钟)
if
cached
,
ok
:=
apiCache
Map
.
Load
(
accountID
);
ok
{
if
cached
,
ok
:=
s
.
cache
.
apiCache
.
Load
(
accountID
);
ok
{
if
cache
,
ok
:=
cached
.
(
*
apiUsageCache
);
ok
&&
time
.
Since
(
cache
.
timestamp
)
<
apiCacheTTL
{
apiResp
=
cache
.
response
}
...
...
@@ -194,7 +213,7 @@ func (s *AccountUsageService) GetUsage(ctx context.Context, accountID int64) (*U
return
nil
,
err
}
// 缓存 API 响应
apiCache
Map
.
Store
(
accountID
,
&
apiUsageCache
{
s
.
cache
.
apiCache
.
Store
(
accountID
,
&
apiUsageCache
{
response
:
apiResp
,
timestamp
:
time
.
Now
(),
})
...
...
@@ -252,14 +271,6 @@ func (s *AccountUsageService) getGeminiUsage(ctx context.Context, account *Accou
return
usage
,
nil
}
// antigravityUsageCache 缓存 Antigravity 额度数据
type
antigravityUsageCache
struct
{
usageInfo
*
UsageInfo
timestamp
time
.
Time
}
var
antigravityCacheMap
=
sync
.
Map
{}
// getAntigravityUsage 获取 Antigravity 账户额度
func
(
s
*
AccountUsageService
)
getAntigravityUsage
(
ctx
context
.
Context
,
account
*
Account
)
(
*
UsageInfo
,
error
)
{
if
s
.
antigravityQuotaFetcher
==
nil
||
!
s
.
antigravityQuotaFetcher
.
CanFetch
(
account
)
{
...
...
@@ -268,7 +279,7 @@ func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *
}
// 1. 检查缓存(10 分钟)
if
cached
,
ok
:=
antigravityCache
Map
.
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
{
// 重新计算 RemainingSeconds
usage
:=
cache
.
usageInfo
...
...
@@ -289,7 +300,7 @@ func (s *AccountUsageService) getAntigravityUsage(ctx context.Context, account *
}
// 4. 缓存结果
antigravityCache
Map
.
Store
(
account
.
ID
,
&
antigravityUsageCache
{
s
.
cache
.
antigravityCache
.
Store
(
account
.
ID
,
&
antigravityUsageCache
{
usageInfo
:
result
.
UsageInfo
,
timestamp
:
time
.
Now
(),
})
...
...
@@ -308,7 +319,7 @@ func (s *AccountUsageService) addWindowStats(ctx context.Context, account *Accou
// 检查窗口统计缓存(1 分钟)
var
windowStats
*
WindowStats
if
cached
,
ok
:=
windowStatsCache
Map
.
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
{
windowStats
=
cache
.
stats
}
...
...
@@ -336,7 +347,7 @@ func (s *AccountUsageService) addWindowStats(ctx context.Context, account *Accou
}
// 缓存窗口统计(1 分钟)
windowStatsCache
Map
.
Store
(
account
.
ID
,
&
windowStatsCache
{
s
.
cache
.
windowStatsCache
.
Store
(
account
.
ID
,
&
windowStatsCache
{
stats
:
windowStats
,
timestamp
:
time
.
Now
(),
})
...
...
backend/internal/service/antigravity_gateway_service.go
View file @
d2aaf0b4
...
...
@@ -322,9 +322,6 @@ func (s *AntigravityGatewayService) Forward(ctx context.Context, c *gin.Context,
originalModel
:=
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
if
s
.
tokenProvider
==
nil
{
...
...
backend/internal/service/wire.go
View file @
d2aaf0b4
...
...
@@ -114,4 +114,5 @@ var ProviderSet = wire.NewSet(
ProvideDeferredService
,
NewAntigravityQuotaFetcher
,
NewUserAttributeService
,
NewUsageCache
,
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment