Cache data for 30 seconds

Signed-off-by: Markus Birth <markus@birth-online.de>
This commit is contained in:
2025-08-03 03:04:47 +01:00
parent 4776088f9d
commit 6b43939404

17
main.py
View File

@@ -1,6 +1,7 @@
from microdot import Microdot
from microdot.utemplate import Template
import tflcountdown as tfl
import time
# utemplate doc: https://github.com/pfalcon/utemplate
@@ -21,12 +22,22 @@ LINE_IDS = {
app = Microdot()
tflc = tfl.TflCountdown(API_KEY)
cached_data = {}
cached_time = -1
@app.route("/")
async def index(request):
response = tflc.get_countdown(["1597", "1598", "11333", "11334", "R0199"])
data = tflc.parse_countdown(response.text)
print(repr(data))
global cached_data, cached_time
now = time.time()
if now - cached_time > 30:
response = tflc.get_countdown(["1597", "1598", "11333", "11334", "R0199"])
data = tflc.parse_countdown(response.text)
print(repr(data))
cached_data = data
cached_time = now
else:
print("Cache hit!")
data = cached_data
return Template("index.html").render(data), {"Content-Type": "text/html"}
app.run(port=5001, debug=True)