接口缓存这块儿其实挺简单的,localStorage就能搞定大部分场景啦~有时候列表数据没必要反复请求,存个几分钟完全够用,用户体验也能提升不少呢
const getCachedData = (key, expireTime = 5 * 60 * 1000) => {
const cache = localStorage.getItem(key)
if (cache) {
const { data, timestamp } = JSON.parse(cache)
if (Date.now() - timestamp < expireTime) {
return data
}
}
return null
}
登录/注册