mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 21:17:30 +00:00
feat: 大规模性能优化 - Redis Pipeline 批量操作、索引系统、连接池优化
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
const redis = require('../models/redis')
|
||||
const logger = require('../utils/logger')
|
||||
const { getCachedConfig, setCachedConfig, deleteCachedConfig } = require('../utils/performanceOptimizer')
|
||||
|
||||
class ClaudeCodeHeadersService {
|
||||
constructor() {
|
||||
@@ -41,6 +42,9 @@ class ClaudeCodeHeadersService {
|
||||
'sec-fetch-mode',
|
||||
'accept-encoding'
|
||||
]
|
||||
|
||||
// Headers 缓存 TTL(60秒)
|
||||
this.headersCacheTtl = 60000
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,6 +151,9 @@ class ClaudeCodeHeadersService {
|
||||
|
||||
await redis.getClient().setex(key, 86400 * 7, JSON.stringify(data)) // 7天过期
|
||||
|
||||
// 更新内存缓存,避免延迟
|
||||
setCachedConfig(key, extractedHeaders, this.headersCacheTtl)
|
||||
|
||||
logger.info(`✅ Stored Claude Code headers for account ${accountId}, version: ${version}`)
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to store Claude Code headers for account ${accountId}:`, error)
|
||||
@@ -154,18 +161,27 @@ class ClaudeCodeHeadersService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取账号的 Claude Code headers
|
||||
* 获取账号的 Claude Code headers(带内存缓存)
|
||||
*/
|
||||
async getAccountHeaders(accountId) {
|
||||
const cacheKey = `claude_code_headers:${accountId}`
|
||||
|
||||
// 检查内存缓存
|
||||
const cached = getCachedConfig(cacheKey)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
try {
|
||||
const key = `claude_code_headers:${accountId}`
|
||||
const data = await redis.getClient().get(key)
|
||||
const data = await redis.getClient().get(cacheKey)
|
||||
|
||||
if (data) {
|
||||
const parsed = JSON.parse(data)
|
||||
logger.debug(
|
||||
`📋 Retrieved Claude Code headers for account ${accountId}, version: ${parsed.version}`
|
||||
)
|
||||
// 缓存到内存
|
||||
setCachedConfig(cacheKey, parsed.headers, this.headersCacheTtl)
|
||||
return parsed.headers
|
||||
}
|
||||
|
||||
@@ -183,8 +199,10 @@ class ClaudeCodeHeadersService {
|
||||
*/
|
||||
async clearAccountHeaders(accountId) {
|
||||
try {
|
||||
const key = `claude_code_headers:${accountId}`
|
||||
await redis.getClient().del(key)
|
||||
const cacheKey = `claude_code_headers:${accountId}`
|
||||
await redis.getClient().del(cacheKey)
|
||||
// 删除内存缓存
|
||||
deleteCachedConfig(cacheKey)
|
||||
logger.info(`🗑️ Cleared Claude Code headers for account ${accountId}`)
|
||||
} catch (error) {
|
||||
logger.error(`❌ Failed to clear Claude Code headers for account ${accountId}:`, error)
|
||||
|
||||
Reference in New Issue
Block a user