diff --git a/models/utils/request.js b/models/utils/request.js index 3535457..97a7707 100644 --- a/models/utils/request.js +++ b/models/utils/request.js @@ -126,15 +126,26 @@ async function getKey (keys = Config.steam.apiKey) { if (i.length === 1) { return { retKeys: i, retKey: key } } - let count = await redis.get(`${redisUseKey}${now}:${key}`) || 0 + const keyNowUses = await redis.keys(`${redisUseKey}${now}:*`) + if (keyNowUses.length === 0) { + return { retKeys: i, retKey: key } + } + const keyUses = await redis.mGet(keyNowUses) + const keyUseMap = new Map() + for (let i = 0; i < keyNowUses.length; i++) { + keyUseMap.set(keyNowUses[i].split(':').pop(), Number(keyUses[i])) + } + let count = 0 + // 获取使用次数最少的key for (const k of i) { - if (k === key) { - continue + const use = keyUseMap.get(`${k}`) + if (!use) { + key = k + break } - const c = await redis.get(`${redisUseKey}${now}:${k}`) || 0 - if (c < count) { + if (use < count) { + count = use key = k - count = c } } return { retKeys: i, retKey: key }