Cache data for 30 seconds
Signed-off-by: Markus Birth <markus@birth-online.de>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from microdot import Microdot
|
from microdot import Microdot
|
||||||
from microdot.utemplate import Template
|
from microdot.utemplate import Template
|
||||||
import tflcountdown as tfl
|
import tflcountdown as tfl
|
||||||
|
import time
|
||||||
|
|
||||||
# utemplate doc: https://github.com/pfalcon/utemplate
|
# utemplate doc: https://github.com/pfalcon/utemplate
|
||||||
|
|
||||||
@@ -21,12 +22,22 @@ LINE_IDS = {
|
|||||||
|
|
||||||
app = Microdot()
|
app = Microdot()
|
||||||
tflc = tfl.TflCountdown(API_KEY)
|
tflc = tfl.TflCountdown(API_KEY)
|
||||||
|
cached_data = {}
|
||||||
|
cached_time = -1
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
async def index(request):
|
async def index(request):
|
||||||
response = tflc.get_countdown(["1597", "1598", "11333", "11334", "R0199"])
|
global cached_data, cached_time
|
||||||
data = tflc.parse_countdown(response.text)
|
now = time.time()
|
||||||
print(repr(data))
|
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"}
|
return Template("index.html").render(data), {"Content-Type": "text/html"}
|
||||||
|
|
||||||
app.run(port=5001, debug=True)
|
app.run(port=5001, debug=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user