😍Cache dữ liệu theo axios và fetch

Cache Axios

import axios from 'axios';
// store data here
const areaDataCache = {};

export const getAreaData = async (postcode) => {
  // if cache doesn't contain data
  if (!areaDataCache[postcode]) {
    // load data and add it to cache
    const { data } = await axios.get(`https://api.zippopotam.us/GB/${postcode}`);
    areaDataCache[postcode] = data.places
  }
  // cached data
  return areaDataCache[postcode];
};

Cache Fetch

fetch(API_URL, { cache: 'force-cache' })

Last updated

Was this helpful?