I have a network monitoring application I've built using Flask.
It has repository classes that make calls to a SQL database that holds data on:
- Devices I want to monitor on my network
- The last n pings to this device
- General app settings
Every couple seconds, this app refreshes and has to read and write to/from this database.
I am trying to implement a global cache within the app to cache some of the database retrieval calls to speed up the app.
Solutions I've Considered
- Python global keyword to create an instance of a custom "Cache" class that can be read and written to from any class.
This could work, but it feels wrong and not very "clean", since I will be using a global variable all over the project.
- @cache annotation on database repository methods
This would not work since a new instance of the repository would be created each time the app is refreshed, meaning the cached results from a few seconds ago would be lost.
So to summarize, my question is: Is there a better way to maintain a global cache in a Python Flask app other than by using the global keyword?