diff --git a/app.py b/app.py index 11c6af3..a8e6350 100644 --- a/app.py +++ b/app.py @@ -2,9 +2,19 @@ from flask import Flask, render_template, jsonify, request from utils.weather import get_today_snowfall from utils.geocode import get_city_name from config import MAX_SNOW_INCHES +from flask_caching import Cache 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("/") def index(): return render_template("index.html") @@ -16,8 +26,7 @@ def snowfall_api(): if lat is None or lon is None: return jsonify({"error": "Missing lat/lon"}), 400 - inches, snowing, tz_name = get_today_snowfall(lat, lon) - city_name = get_city_name(lat, lon) + inches, snowing, tz_name, city_name = get_snowfall_for_location(lat, lon) from datetime import datetime import pytz diff --git a/requirements.txt b/requirements.txt index 40d2675..33e8c9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ requests gunicorn pytz timezonefinder +Flask-Caching \ No newline at end of file