Modern APIs with FastAPI and Python Transcripts
Chapter: Error handling and performance
Lecture: Concept: Caching data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We saw that it takes 400, 700 milliseconds, almost a second, for us to get the weather report from the API.
0:08 But how frequent does that information actually change? Is it good to get the forecast from 30 seconds ago or the current weather from
0:15 30 seconds ago? Five minutes ago? probably. If that's the case, we can cache that information to allow us respond much,
0:21 much faster, as well as to not use up our API calls, right? You saw with a free tier we only get 60 calls a minute and a million a month.
0:30 So if somebody asked for the same thing a bunch of times, let's just give them the same thing back. And we can do that by creating a cache.
0:37 And we create this thing called the weather cache and the beginning of our get report We just say, "Do we have it saved and is it not too old"?
0:43 Then give them that. Then we're gonna go do the async stuff with httpx. And before we return the weather, let's save it in our cache,
0:51 so the next time they ask for it, they ask for it within an hour, They're going to just get that one back
0:55 instead of making a new call over to the API. Again, in our weather cache, we put this in memory.
1:01 That's not ideal, because in production you typically have multiple processes,
1:05 multiple copies, of this app running over there. You'd probably put it in somewhere like Redis or
1:09 a database, but even just putting it in memory is gonna help quite a bit.

Talk Python's Mastodon Michael Kennedy's Mastodon