Skip to content

Commit

Permalink
Merge pull request #38 from story-x/main
Browse files Browse the repository at this point in the history
fix:key判断
  • Loading branch information
XasYer authored Jan 6, 2025
2 parents e3b9b45 + ef46015 commit f97bea6
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions models/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,47 +108,31 @@ export async function post (url, options = {}) {
}

async function getKey (keys = Config.steam.apiKey) {
const i = []
const retKeys = []
const now = moment().format('YYYY-MM-DD')
if (keys.length > 1) {
for (const key of keys) {
if (!await redis.exists(`${redis429Key}${key}`)) {
i.push(key)
retKeys.push(key)
}
}
if (i.length === 0) {
i.push(keys[0])
if (retKeys.length === 0) {
retKeys.push(keys[0])
}
} else {
i.push(...keys)
retKeys.push(...keys)
}
let key = i[0]
if (i.length === 1) {
return { retKeys: i, retKey: key }
}
const keyNowUses = await redis.keys(`${redisUseKey}${now}:*`)
if (keyNowUses.length === 0) {
return { retKeys: i, retKey: key }

if (retKeys.length <= 1) {
return { retKeys, retKey: retKeys[0] }
}

const keyNowUses = retKeys.map(k => `${redisUseKey}${now}:${k}`)
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) {
const use = keyUseMap.get(`${k}`)
if (!use) {
key = k
break
}
if (use < count) {
count = use
key = k
}
}
return { retKeys: i, retKey: key }
const count = Math.min(...keyUses.map(i => i || 0))
const keyIndex = keyUses.findIndex(k => Number(k) === count) || 0
const key = retKeys[keyIndex]
return { retKeys, retKey: key }
}

function incr (key, day = 3) {
Expand Down

0 comments on commit f97bea6

Please sign in to comment.