New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for caching GET requests #31
Comments
Heh, I was just looking into this! I thought that an interceptor could be used or a transformer that'll immediately resolve with a response, but no such luck yet. Felix looks interesting for sure... |
👍 The node version isn't super useful for me until we have a plugable caching layer. |
Any progress in browser? The cache working? |
Bump... need this or I am going to swap out axios for superagent or request for our production app... causes huge slowdowns |
@mzabriskie I've added support for ttls in mzabriskie/felix#4 and can tackle this if you like my implementation there. |
Glad to see this issue being revived, it would save me from having to write a bunch of boilerplate 😄 |
I think right now caching can be implemented via interceptors, although it's not very clean: cache(axios, options);
function cache(instance, options) {
instance.interceptors.request.use(function(config) {
if (!config.method === 'get') {
return config;
}
return cache.get(generateOptions(config, options))
.then(createCachedError, function() { return config; });
});
instance.interceptors.response.use(null, function(error) {
if (error.cachedResult) {
return Promise.resolve(error.cachedResult);
}
return Promise.reject(error);
});
} A cleaner way to do this could be defining the default adapter (http, xhr) in the instance defaults and then using a custom adapter like this: axios.get('url', { adapter: cachingAdapter });
function cachingAdapter(resolve, reject, config) {
cache.get(generateOptions(config, options)).then(function(result) {
resolve(createResponse(result));
}, function() {
axios.defaults.adapter(resolve, reject, config);
});
} I also think that adapters should return promises instead of getting the resolve and reject methods, but that's not an issue right now. I think Axios should leave caching out of the core and allow an implementation like this. What do you think? |
I'm closing this issue due to inactivity. Feel free to reopen it if you consider it requires further discussion ;) |
Is there a solution for this? It seems odd that there is no obvious way onto caching requests... |
@rubennorte what is options in cache(axios, options); ? |
Depending on your use case, you can just override // Create intercepting get function which returns cached promise,
// hence multiple requests to the same URL will be resolved by
// a single promise.
function cachingGet (get) {
const cache = new Map()
return function cachedGet (url) {
const key = url
if (cache.has(key)) {
return cache.get(key)
} else {
const request = get(...arguments)
cache.set(key, request)
return request
}
}
}
const instance = axios.create(config)
instance.get = cachingGet(instance.get) |
maybe a better solution would be using adpater pattern ? |
Made a thing for this https://github.com/RasCarlito/axios-cache-adapter :) By default it uses an in memory custom store for the cache. Example: import axios from 'axios'
import { setupCache } from 'axios-cache-adapter'
const cache = setupCache(/* options */)
const api = axios.create({
adapter: cache.adapter
})
api.get('some-url').then(response => /* Do something awesome with response.data \o/ */) Cheers 🍻 |
I have made a set of extensions for axios, including caching GET request, which depends on axios custom adapter mechanism and lru-cache, and the most important thing is the test coverage has reached 100% 🎉. https://github.com/kuitos/axios-extensions Example import axios from 'axios';
import { cacheAdapterEnhancer } from 'axios-extensions';
const http = axios.create({
baseURL: '/',
headers: { 'Cache-Control': 'no-cache' },
// cache will be enabled by default
adapter: cacheAdapterEnhancer(axios.defaults.adapter)
});
http.get('/users'); // make real http request
http.get('/users'); // use the response from the cache of previous request, without real http request made
http.get('/users', { cache: false }); // disable cache manually and the the real http request invoked btw, I have made another powerful extension, which can throttle the requests in the threshold time. import axios from 'axios';
import { throttleAdapterEnhancer } from 'axios-extensions';
const http = axios.create({
baseURL: '/',
headers: { 'Cache-Control': 'no-cache' },
adapter: throttleAdapterEnhancer(axios.defaults.adapter, 2 * 1000)
});
http.get('/users'); // make real http request
http.get('/users'); // responsed from the cache
http.get('/users'); // responsed from the cache for more information u can check the api doc Hope that can be useful for u and feel free to ask any questions🙂 |
To people just want to disable cache I used: |
How about this in TypeScript [can easily be a JS thing] -
|
Request URL: http://myurl it causing issue when request with 'Cache-Control' : 'no-cache' in header . My Code: const instance = axios.create({baseURL: url}); instance.defaults.headers['ApiKey'] = key; I have also tried your solution passing config in single request . but no luck. what will be the solution? |
Will implement using https://github.com/mzabriskie/felix
The text was updated successfully, but these errors were encountered: