Skip to content

Commit

Permalink
熔断机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ljxi committed Jun 11, 2024
1 parent 4c231e7 commit eaeeb3e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/components/IPinfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,27 @@ const copy = (ip: string) => {
}
async function queryIp(ip: string) {
try {
const rsp = await fetch(import.meta.env.VITE_API_URL + "ip.ajax?ip=" + ip, {
method: "GET",
mode: "cors",
redirect: "follow",
referrerPolicy: "no-referrer"
});
let resp = await rsp.json();
return resp['data']
} catch (error) {
throw "查询IP信息失败"
}
const rsp = await fetch(import.meta.env.VITE_API_URL + "ip.ajax?ip=" + ip, {
method: "GET",
mode: "cors",
redirect: "follow",
referrerPolicy: "no-referrer"
});
let resp = await rsp.json();
return resp['data']
}
let failure = false
async function cachedQuery(ip: string) {
let ret = JSON.parse(localStorage.getItem("cache_ip_"+ip) || "{}")
if (!ret.ip || new Date().getTime() / 1000 - ret.time > 60 * 60 * 24){
try {
if(failure) throw ""
ret = await queryIp(ip)
} catch (error) {
failure = true
throw "查询IP信息失败"
}
ret['time'] = new Date().getTime() / 1000
localStorage.setItem("cache_ip_"+ip, JSON.stringify(ret))
}
Expand Down

0 comments on commit eaeeb3e

Please sign in to comment.