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 maxsize
and 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.
Example
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:
1 2 |
|
Start the server:
1
|
|
Make the request:
1 2 |
|
Change the data inside the file:
1
|
|
Make the request again:
1 2 |
|
As the ttl is set to 60, wait for 60 seconds so that the item kan expire from the cache and try again:
1 2 |
|
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.
Thank You
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.