-
Notifications
You must be signed in to change notification settings - Fork 137
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
Is there a way to return a cached response when using a Retry adapter and force_refresh=True? #924
Comments
Yes! I think the |
Oh, I missed the part about needing to use Let me think about the best way to handle that. |
Instead of from requests_cache import CachedSession, EXPIRE_IMMEDIATELY
backend = SQLiteCache(use_cache_dir=True)
session = CachedSession(
backend=backend,
expire_after=EXPIRE_IMMEDIATELY,
stale_if_error=True,
) |
Thanks for the response! I don't think this works for me with the Retry HTTPAdapter. It appears that a successful request/response is not cached with this method. (snipping some private data)
And then if the API route is modified to only return a 500 error it does not return any stale cached data.
I think I was able to do what I'm trying to do by creating my own CacheSession object and using a tenacity retry on get calls. The decorator will retry the function if an exception is raised and modify the kwargs to be
I think maybe my use case is not the intended usage in that I want to prefer to get data from the API every time and only read from cache when there is an error. |
I am wondering if there is a way to return a cached response when one is available when the request raises a requests.exceptions.RetryError and force_refresh=True. For my use case I need/always want to make the request and only use a cached response if the request fails.
Example:
The text was updated successfully, but these errors were encountered: