Replies: 5 comments
-
I'm using aio cache with a redis backend for the exact same reason.
There's no built-in.
Le jeu. 24 oct. 2019 à 8:58 PM, Zach Rachlin <notifications@github.com> a
écrit :
… *Description*
Hi! I'm coming from Flask and am very new to FastAPI.
I'm wondering if there is built-in way to cache the results of API
requests so that they can be returned automatically when requested again?
Some of the routes I plan to make call external APIs and do some data
processing on the results, so they take a few seconds to finish. With
Flask, I've been using the Flask-Caching
<https://pythonhosted.org/Flask-Caching/> extension, which enables me to
put a decorator above the route/view to denote that I want to cache (or
memoize if there are input arguments) its result. So it gets called the
first time the request is made, but then all subsequent requests with the
same arguments/parameters return the cached result.
I'd love to figure out how to make this happen with FastAPI. I've been
looking through the documentation for something related to caching, and the
only mention I could find was the following on the sub-dependency page
<https://fastapi.tiangolo.com/tutorial/dependencies/sub-dependencies/>:
Using the same dependency multiple times
If one of your dependencies is declared multiple times for the same path
operation, for example, multiple dependencies have a common sub-dependency,
FastAPI will know to call that sub-dependency only once per request.
And it will save the returned value in a "cache" and pass it to all the
"dependants" that need it in that specific request, instead of calling the
dependency multiple times for the same request.
I'm still trying to fully comprehend dependancies/sub-dependencies, so
maybe this is what I'm looking for? But this seems like it is talking about
using something multiple times in a single request, rather than in separate
requests.
Any help/direction would be greatly appreciated. Thanks!
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#651>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAINSPVGVU6HHV6GPYA23FDQQHV4JANCNFSM4JEYUEOA>
.
|
Beta Was this translation helpful? Give feedback.
-
@euri10 Ok cool, thanks. Do you have to declare all of your path operation functions as async in order to be compatible with aiocache? Or will they work as normal non-async functions since FastAPI works asynchronously behind the scenes (from what i've read)? |
Beta Was this translation helpful? Give feedback.
-
I'm just quoting this excellent post because I couldn't explain it better (
|
Beta Was this translation helpful? Give feedback.
-
@zrachlin I would think you might want to cache the external APIs' results, not necessarily the responses from your API. Otherwise, someone could, for example, use a stale/invalid authentication token, and still receive the response, because that old token input with the extra data is still on the cache. To use But if you use normal |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
Description
Hi! I'm coming from Flask and am very new to FastAPI.
I'm wondering if there is built-in way to cache the results of API requests so that they can be returned automatically when requested again? Some of the routes I plan to make call external APIs and do some data processing on the results, so they take a few seconds to finish. With Flask, I've been using the Flask-Caching extension, which enables me to put a decorator above the route/view to denote that I want to cache (or memoize if there are input arguments) its result. So it gets called the first time the request is made, but then all subsequent requests with the same arguments/parameters return the cached result.
I'd love to figure out how to make this happen with FastAPI. I've been looking through the documentation for something related to caching, and the only mention I could find was the following on the sub-dependency page:
I'm still trying to fully comprehend dependancies/sub-dependencies, so maybe this is what I'm looking for? But this seems like it is talking about using something multiple times in a single request, rather than in separate requests.
Any help/direction would be greatly appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions