- Published on
How to Cache Data with Python Flask
- Authors
- Name
- Ruan Bekker
- @ruanbekker
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:
from flask import Flask
from cachetools import cached, TTLCache
app = Flask(__name__)
cache = TTLCache(maxsize=100, ttl=60)
@cached(cache)
def read_data():
data = open('data.txt', 'r').read()
return data
@app.route('/')
def main():
get_data = read_data()
return get_data
if __name__ == '__main__':
app.run()
Create the local file with some data:
$ touch data.txt
$ echo "version1" > data.txt
Start the server:
$ python app.py
Make the request:
$ curl http://localhost:5000/
version1
Change the data inside the file:
$ echo "version2" > data.txt
Make the request again:
$ curl http://localhost:5000/
version1
As the ttl is set to 60, wait for 60 seconds so that the item kan expire from the cache and try again:
$ curl http://localhost:5000/
version2
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
Thanks for reading, feel free to check out my website, and subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon