react ant design项目,使用umi-request实现http缓存

//'useCache'当'useCache'为true时,GET请求将在ttl毫秒内缓存。
  //缓存键为“ url +参数+方法”。
  useCache:false//默认

  //'ttl'缓存持续时间(毫秒),0为无穷大
  ttl:60000//'maxCache'是要缓存的最大请求数,0表示无穷大。
  maxCache:0// 根据http协议,GET请求用于从服务器获取数据,当服务器数据更新不频繁时,有必要缓存响应数据。
  // 对于某些需要使用其他方法要求缓存数据的情况,
  validateCache: (url, options) => { return options.method.toLowerCase() === 'get' },

github地址

import {extend} from 'umi-request';

/**
 * 配置request请求时的默认参数
 */
const request = extend({
  errorHandler, // 默认错误处理
  // credentials: 'include', // 默认请求是否带上cookie
  prefix: '/rest', //前缀
});

/**
 * request拦截器, 改变url 或 options.
 */
request.interceptors.request.use((url: string, options: any) => {
  const token = localStorage.getItem('auth');
  const headers = {
    Accept: 'application/json',
    Authorization: '',
  };
  if (token) {
    headers.Authorization = token;
    return {
      url: `${basicUrl}${url}`,
      options: {
        interceptors: true,
        headers,
        useCache: true,
        ttl: 60000,
        maxCache: 0,
        timeout:5000,
        ...options,
      },
    };
  }
  return {
    url: `${basicUrl}${url}`,
    options: {...options, interceptors: true,},
  };
});
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值