From 7d4274e61308f6e57db64e801e7ebf9ca4ddf988 Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Wed, 4 May 2016 19:01:55 -0400
Subject: [PATCH] Caching tweaks

---
 app.py | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/app.py b/app.py
index 46b9b77..f2bb92d 100644
--- a/app.py
+++ b/app.py
@@ -46,8 +46,8 @@ def map_embed(mode):
     elif mode == "p":
         return render_template('predictions.html', agency=agency, config=app.config, embed=True)
 
+
 @app.route('/ajax')
-@cache_for(seconds=5)
 def ajax():
     """ Handle all async requests (from JS). """
     query = request.args.get('query')
@@ -62,6 +62,7 @@ def ajax():
             return render_template(template_name, agency=agency, config=app.config)
         except TemplateNotFound:
             return abort(404)
+    @cache_for(hours=1)
     def routes():
         """ Serve the route configuration (routes, stops)  """
         from models import Route, RouteStop, Stop
@@ -69,11 +70,12 @@ def ajax():
             .filter(Agency.tag==agency_tag).all()
         stops = db.session.query(Stop).options(joinedload(Stop.routes))\
             .filter(Stop.routes.any(Route.id.in_([r.id for r in routes]))).all()
-        return {
+        return jsonify({
             "routes": {r.tag: r.serialize() for r in routes},
             "stops": {s.id: s.serialize() for s in stops}
-        }
+        })
 
+    @cache_for(seconds=5)
     def vehicles():
         """ Serve the current vehicle locations and arrival predictions. """
         from models import Route, VehicleLocation, Prediction
@@ -105,11 +107,12 @@ def ajax():
                 Prediction.prediction >= now)\
             .group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
             .all()
-        return {
+        return jsonify({
                 "locations": {v.vehicle: v.serialize() for v in vehicle_locations},
                 "predictions": {p.id: p.serialize() for p in predictions}
-        }
+        })
 
+    @cache_for(seconds=15)
     def predictions():
         """ Serve arrival predictions only. """
         from models import Prediction
@@ -132,16 +135,16 @@ def ajax():
         predictions = predictions\
             .group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
             .all()
-        return {
+        return jsonify({
                 "predictions": {p.vehicle: p.serialize() for p in predictions},
-        }
+        })
 
     if query == "routes":
-        return jsonify(routes())
+        return routes()
     elif query == "vehicles":
-        return jsonify(vehicles())
+        return vehicles()
     elif query == "predictions":
-        return jsonify(predictions())
+        return predictions()
     elif query == "modal":
         modal_name = request.args.get('modal_name')
         return modal(modal_name)
-- 
GitLab