Skip to content
Snippets Groups Projects
Commit 7d4274e6 authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Caching tweaks

parent db38cea0
No related branches found
No related tags found
No related merge requests found
...@@ -46,8 +46,8 @@ def map_embed(mode): ...@@ -46,8 +46,8 @@ def map_embed(mode):
elif mode == "p": elif mode == "p":
return render_template('predictions.html', agency=agency, config=app.config, embed=True) return render_template('predictions.html', agency=agency, config=app.config, embed=True)
@app.route('/ajax') @app.route('/ajax')
@cache_for(seconds=5)
def ajax(): def ajax():
""" Handle all async requests (from JS). """ """ Handle all async requests (from JS). """
query = request.args.get('query') query = request.args.get('query')
...@@ -62,6 +62,7 @@ def ajax(): ...@@ -62,6 +62,7 @@ def ajax():
return render_template(template_name, agency=agency, config=app.config) return render_template(template_name, agency=agency, config=app.config)
except TemplateNotFound: except TemplateNotFound:
return abort(404) return abort(404)
@cache_for(hours=1)
def routes(): def routes():
""" Serve the route configuration (routes, stops) """ """ Serve the route configuration (routes, stops) """
from models import Route, RouteStop, Stop from models import Route, RouteStop, Stop
...@@ -69,11 +70,12 @@ def ajax(): ...@@ -69,11 +70,12 @@ def ajax():
.filter(Agency.tag==agency_tag).all() .filter(Agency.tag==agency_tag).all()
stops = db.session.query(Stop).options(joinedload(Stop.routes))\ stops = db.session.query(Stop).options(joinedload(Stop.routes))\
.filter(Stop.routes.any(Route.id.in_([r.id for r in routes]))).all() .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}, "routes": {r.tag: r.serialize() for r in routes},
"stops": {s.id: s.serialize() for s in stops} "stops": {s.id: s.serialize() for s in stops}
} })
@cache_for(seconds=5)
def vehicles(): def vehicles():
""" Serve the current vehicle locations and arrival predictions. """ """ Serve the current vehicle locations and arrival predictions. """
from models import Route, VehicleLocation, Prediction from models import Route, VehicleLocation, Prediction
...@@ -105,11 +107,12 @@ def ajax(): ...@@ -105,11 +107,12 @@ def ajax():
Prediction.prediction >= now)\ Prediction.prediction >= now)\
.group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\ .group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
.all() .all()
return { return jsonify({
"locations": {v.vehicle: v.serialize() for v in vehicle_locations}, "locations": {v.vehicle: v.serialize() for v in vehicle_locations},
"predictions": {p.id: p.serialize() for p in predictions} "predictions": {p.id: p.serialize() for p in predictions}
} })
@cache_for(seconds=15)
def predictions(): def predictions():
""" Serve arrival predictions only. """ """ Serve arrival predictions only. """
from models import Prediction from models import Prediction
...@@ -132,16 +135,16 @@ def ajax(): ...@@ -132,16 +135,16 @@ def ajax():
predictions = predictions\ predictions = predictions\
.group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\ .group_by(Prediction.id, Prediction.vehicle, Prediction.stop_id)\
.all() .all()
return { return jsonify({
"predictions": {p.vehicle: p.serialize() for p in predictions}, "predictions": {p.vehicle: p.serialize() for p in predictions},
} })
if query == "routes": if query == "routes":
return jsonify(routes()) return routes()
elif query == "vehicles": elif query == "vehicles":
return jsonify(vehicles()) return vehicles()
elif query == "predictions": elif query == "predictions":
return jsonify(predictions()) return predictions()
elif query == "modal": elif query == "modal":
modal_name = request.args.get('modal_name') modal_name = request.args.get('modal_name')
return modal(modal_name) return modal(modal_name)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment