diff --git a/src/models/redis.js b/src/models/redis.js index a4a03b10..4d62bda7 100644 --- a/src/models/redis.js +++ b/src/models/redis.js @@ -192,8 +192,8 @@ class RedisClient { cacheCreateTokens = 0, cacheReadTokens = 0, model = 'unknown', - ephemeral5mTokens = 0, // 新增:5分钟缓存 tokens - ephemeral1hTokens = 0 // 新增:1小时缓存 tokens + ephemeral5mTokens = 0, // 新增:5分钟缓存 tokens + ephemeral1hTokens = 0 // 新增:1小时缓存 tokens ) { const key = `usage:${keyId}` const now = new Date() diff --git a/src/services/apiKeyService.js b/src/services/apiKeyService.js index 48759aa8..cf0d9e4a 100644 --- a/src/services/apiKeyService.js +++ b/src/services/apiKeyService.js @@ -484,12 +484,12 @@ class ApiKeyService { // 提取详细的缓存创建数据 let ephemeral5mTokens = 0 let ephemeral1hTokens = 0 - + if (usageObject.cache_creation && typeof usageObject.cache_creation === 'object') { ephemeral5mTokens = usageObject.cache_creation.ephemeral_5m_input_tokens || 0 ephemeral1hTokens = usageObject.cache_creation.ephemeral_1h_input_tokens || 0 } - + // 记录API Key级别的使用统计 - 这个必须执行 await redis.incrementTokenUsage( keyId, @@ -499,8 +499,8 @@ class ApiKeyService { cacheCreateTokens, cacheReadTokens, model, - ephemeral5mTokens, // 传递5分钟缓存 tokens - ephemeral1hTokens // 传递1小时缓存 tokens + ephemeral5mTokens, // 传递5分钟缓存 tokens + ephemeral1hTokens // 传递1小时缓存 tokens ) // 记录费用统计 diff --git a/src/utils/cacheMonitor.js b/src/utils/cacheMonitor.js index ab24bdfc..ece5e478 100644 --- a/src/utils/cacheMonitor.js +++ b/src/utils/cacheMonitor.js @@ -81,7 +81,7 @@ class CacheMonitor { const totalRequests = stats.totalHits + stats.totalMisses stats.averageHitRate = - totalRequests > 0 ? ((stats.totalHits / totalRequests) * 100).toFixed(2) + '%' : '0%' + totalRequests > 0 ? `${((stats.totalHits / totalRequests) * 100).toFixed(2)}%` : '0%' return stats } @@ -95,7 +95,7 @@ class CacheMonitor { for (const [name, monitor] of this.monitors) { try { - const cache = monitor.cache + const { cache } = monitor const beforeSize = cache.cache.size // 执行常规清理 @@ -251,8 +251,8 @@ class CacheMonitor { estimateMemoryUsage() { let totalBytes = 0 - for (const [name, monitor] of this.monitors) { - const cache = monitor.cache.cache + for (const [, monitor] of this.monitors) { + const { cache } = monitor.cache for (const [key, item] of cache) { // 粗略估算:key 长度 + value 序列化长度 totalBytes += key.length * 2 // UTF-16 @@ -275,7 +275,7 @@ class CacheMonitor { logger.error('🚨 EMERGENCY CLEANUP INITIATED') for (const [name, monitor] of this.monitors) { - const cache = monitor.cache + const { cache } = monitor const beforeSize = cache.cache.size // 清理一半的缓存项(LRU 会保留最近使用的) diff --git a/src/utils/lruCache.js b/src/utils/lruCache.js index 0fd5f008..993089ba 100644 --- a/src/utils/lruCache.js +++ b/src/utils/lruCache.js @@ -94,7 +94,7 @@ class LRUCache { * 清空缓存 */ clear() { - const size = this.cache.size + const { size } = this.cache this.cache.clear() this.hits = 0 this.misses = 0