mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
examples: updated runkit example
This commit is contained in:
parent
766b016866
commit
4cfa8d005e
@ -1,53 +1,51 @@
|
|||||||
|
//@ts-check
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
|
|
||||||
const { create: createAxios } = require('axios').default;
|
const { create: createAxios } = require('axios').default;
|
||||||
const { setupCache } = require('axios-cache-interceptor');
|
const { setupCache } = require('axios-cache-interceptor');
|
||||||
|
const { log } = console;
|
||||||
|
|
||||||
async function main() {
|
(async () => {
|
||||||
const axios = setupCache(
|
const axios = setupCache(
|
||||||
// creating axios instance
|
// creating axios instance
|
||||||
createAxios({
|
createAxios({ baseURL: 'https://registry.npmjs.org/' }),
|
||||||
baseUrl: 'https://registry.npmjs.org/'
|
|
||||||
}),
|
|
||||||
|
|
||||||
// configuring the cache
|
// configuring the cache
|
||||||
{
|
{ ttl: 99999, interpretHeader: true }
|
||||||
ttl: 99999,
|
|
||||||
|
|
||||||
// Parse the Cache-Control header to determine the cache strategy
|
|
||||||
interpretHeader: true
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const fetchedResponse = await axios.get('/axios-cache-interceptor');
|
const fetchedResponse = await axios.get('/axios-cache-interceptor');
|
||||||
|
// fetchedResponse.cached == false
|
||||||
|
|
||||||
|
//
|
||||||
|
// The next request won't do a network request, because the response is already cached
|
||||||
|
//
|
||||||
|
|
||||||
// This won't made a network request, because the response is already cached
|
|
||||||
const cachedResponse = await axios.get('/axios-cache-interceptor');
|
const cachedResponse = await axios.get('/axios-cache-interceptor');
|
||||||
|
// cachedResponse.cached == true
|
||||||
|
|
||||||
console.log('First request was cached?');
|
log(`First request was ${fetchedResponse.cached ? 'cached' : 'fetched'}`);
|
||||||
console.log(fetchedResponse.cached, '\n');
|
log(`Second request was ${cachedResponse.cached ? 'cached' : 'fetched'}`);
|
||||||
|
|
||||||
console.log('Second request was cached?');
|
//
|
||||||
console.log(cachedResponse.cached, '\n');
|
// The interpretHeader option used a different strategy, see the received Cache-Control header
|
||||||
|
// (server may return undefined if this is the first request in a while :))
|
||||||
|
//
|
||||||
|
|
||||||
console.log('The interpretHeader option used a different strategy', '\n');
|
log(`Fetched response Cache-Control: ${fetchedResponse.headers['cache-control']}`);
|
||||||
console.log('See the received Cache-Control header');
|
log(`Fetched response Age: ${fetchedResponse.headers['age']}`);
|
||||||
console.log(fetchedResponse.headers['cache-control'], '\n');
|
|
||||||
console.log('And also the received Age header');
|
|
||||||
console.log(fetchedResponse.headers['age'], '\n');
|
|
||||||
|
|
||||||
const cacheInformation = await axios.storage.get(fetchedResponse.id);
|
const cacheInformation = await axios.storage.get(fetchedResponse.id);
|
||||||
|
|
||||||
console.log(
|
//
|
||||||
'As you can see, the TTL used was the maxAge cache directive minus the Age header',
|
// As you can see, the TTL used was the maxAge cache directive minus the Age header
|
||||||
'\n'
|
//
|
||||||
);
|
|
||||||
console.log('See the time to live in the cache: ');
|
|
||||||
console.log(cacheInformation.ttl, '\n');
|
|
||||||
|
|
||||||
console.log(
|
log(`Cache TTL info: ${cacheInformation.ttl}`);
|
||||||
"If you disable the interpretHeader option you'll see that the TTL will be the default (99999)\n"
|
|
||||||
);
|
//
|
||||||
|
// If you disable the interpretHeader option you'll see that the TTL will be the default (99999)\n
|
||||||
|
//
|
||||||
|
|
||||||
// Remove the old cache by brute force
|
// Remove the old cache by brute force
|
||||||
await axios.storage.remove(fetchedResponse.id);
|
await axios.storage.remove(fetchedResponse.id);
|
||||||
@ -61,8 +59,5 @@ async function main() {
|
|||||||
|
|
||||||
const refetchedInformation = await axios.storage.get(refetchedResponse.id);
|
const refetchedInformation = await axios.storage.get(refetchedResponse.id);
|
||||||
|
|
||||||
console.log('Third request TTL:');
|
log(`Third request TTL: ${refetchedInformation.ttl}`);
|
||||||
console.log(refetchedInformation.ttl);
|
})().catch(console.error);
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user