export default class UrlCache {
static cacheUrls: Map<any, number> = new Map()
/**
* @function 处理缓存url
* @param url
* */
static filterRepeatUrl(url: RequestInfo): boolean {
let currentTimeStamp = (new Date()).getTime();
let isCache = false
if (this.cacheUrls.size > 0) {
for (let [key, value] of this.cacheUrls) {
if (key === url) {
isCache = true
if ((currentTimeStamp - value) / 1000 > 3) {
this.cacheUrls.set(url, currentTimeStamp)
return true
}
}
}
}
if (!isCache) {
this.cacheUrls.set(url, currentTimeStamp)
return true
}
return false
}
static clearCache() {
this.cacheUrls.clear()
}
}
将请求所有的接口缓存,并存储请求的时间戳,在3秒内,重复请求,即会返回false,在统一请求网络库处,可以取消请求,从而达到防重复请求。
网友评论