adds flask caching
This commit is contained in:
13
app.py
13
app.py
@@ -2,9 +2,19 @@ from flask import Flask, render_template, jsonify, request
|
|||||||
from utils.weather import get_today_snowfall
|
from utils.weather import get_today_snowfall
|
||||||
from utils.geocode import get_city_name
|
from utils.geocode import get_city_name
|
||||||
from config import MAX_SNOW_INCHES
|
from config import MAX_SNOW_INCHES
|
||||||
|
from flask_caching import Cache
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Cache config: 10-minute default timeout
|
||||||
|
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache", "CACHE_DEFAULT_TIMEOUT": 600})
|
||||||
|
|
||||||
|
@cache.memoize(600) # cache each request for 10 minutes
|
||||||
|
def get_snowfall_for_location(lat, lon):
|
||||||
|
inches, snowing, tz_name = get_today_snowfall(lat, lon)
|
||||||
|
city_name = get_city_name(lat, lon)
|
||||||
|
return inches, snowing, tz_name, city_name
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
return render_template("index.html")
|
return render_template("index.html")
|
||||||
@@ -16,8 +26,7 @@ def snowfall_api():
|
|||||||
if lat is None or lon is None:
|
if lat is None or lon is None:
|
||||||
return jsonify({"error": "Missing lat/lon"}), 400
|
return jsonify({"error": "Missing lat/lon"}), 400
|
||||||
|
|
||||||
inches, snowing, tz_name = get_today_snowfall(lat, lon)
|
inches, snowing, tz_name, city_name = get_snowfall_for_location(lat, lon)
|
||||||
city_name = get_city_name(lat, lon)
|
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import pytz
|
import pytz
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ requests
|
|||||||
gunicorn
|
gunicorn
|
||||||
pytz
|
pytz
|
||||||
timezonefinder
|
timezonefinder
|
||||||
|
Flask-Caching
|
||||||
Reference in New Issue
Block a user