axios-cacher v1.1.4
Axios Cacher
Cache the response from endpoints and easily invalidate the cache
Install
npm install --save axios-cacherUsage
Step 1
Import
import setup from "axios-cacher";Step 2
This package exposes a setup function which can be used as follows:
const { axiosClient, invalidateThis } = setup({
// base url for your axios config
baseURL: baseURL,
// your axios config headers
headers: {
Accept: "application/vnd.api+json; version=1.0"
},
// cache configuration
cache: {
// cache expires after 5 minutes
maxAge: 5 * 60 * 1000,
// function to invalidate cache
invalidate: async (config, request) => {
if (request.clearCacheEntry) {
await config.store.removeItem(config.uuid);
}
},
}
});Step 3
You can use axiosClient in step 1 to make api calls
const response = await axiosClient.get(
url,
{ userKey: "test" }
);Any request after this from the same url will be fetched from the cache for 5 minutes(for this example) unless you invalidate it before. Pass the userKey as shown above so that you can invalidate it using the invalidateThis function.
Use the invalidate function as follows:
invalidateThis(userKey);9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago