FastAPI Caching with Middleware(Redis)
While working on a project, I had a requirement to cache my large responses because they could take upto 5–10 seconds to resolve and also would not change for a few hours.
As any other respectable developer, I started to look into stack overflow on how to do this. I found quite a few caching libraries, quite a few of them were not supported and while integrating them, I felt I could not get a lot of freedom on how to use them and follow best practices. So I decided to make a package of my own which follows best practices of REST API development using Cache-Control header and leverages the already good FastAPI middleware.
Cache Fastapi
You can find the package here.
The installation process being fairly simple(you can find it here) lets get to how it works
Working
How are we leveraging middleware here then?
Well at the start of the lifecycle of the application, the user sends the middleware a list of endpoints.
NB: For now I like the list approach as it gives you one point where you can see all your cached endpoints. Might shift to decorator approach later!
Every request has to pass through the middleware function before reaching your router and also has to pass back again after exiting the router.
At entry point the middleware looks for the header Cache-Control
Some Cache-Control
best practices can be found here
What is the middleware looking for?
Well if you want to start simple you can just provide nothing.
If your endpoint is provided in the list, the middleware by default will cache it for 60 secs.
Now lets get customising!
Send your header as
Cache-Control: max-age=120
Well now your endpoint response is cached for 120 sec (2 mins)
Easy right! Just remember to send your time in seconds.
Well what if you want a fresh response and don’t necessarily want to delete your cache, send:
Cache-Control: no-cache
And on the contrary if you can to make a request but don’t want to cache this response, send:
Cache-Control: no-store
Quite simple and verbose right.
And in you response you get the same header
Cache-Control: max-age=110
This means you are getting a cached response and new response will only be generated after 110 seconds.
Conclusion:
Well that’s pretty much it. Lightweight and verbose!
Let me know what improvements you want to see or new features. I consider this in early alpha so nothing is out of reach and of course, the open source community feel free to pick it apart. I’d be waiting for your PRs.