If you depending on a external source to return static data you can implement
cachetools to cache data from preventing the overhead to make the request everytime you make a request to Flask.
This is useful when your upstream data does not change often. This is configurable with
ttl so whenever the first one’s threshold is met, the application will fetch new data whenever the request has been made to your application.
Let’s build a basic flask application that will return the data from our
data.txt file to the client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Create the local file with some data:
Start the server:
Make the request:
Change the data inside the file:
Make the request again:
As the ttl is set to 60, wait for 60 seconds so that the item kan expire from the cache and try again:
As you can see the cache expired and a new request has been made to read the file again and load it in cache, and then return to the client.
Please feel free to show support by, sharing this post, making a donation, subscribing or reach out to me if you want me to demo and write up on any specific tech topic.