diff --git a/server.py b/server.py
index 9d048af..821ec97 100644
--- a/server.py
+++ b/server.py
@@ -12,6 +12,7 @@ from flask import request
from flask import send_file
from flask import send_from_directory
from flask import url_for
+from flask_cache import Cache
from config import database
import top
@@ -23,6 +24,7 @@ db = MySQLdb.connect(database["host"],
cur = db.cursor()
app = Flask(__name__)
+cache = Cache(app, config={'CACHE_TYPE': 'simple'})
app.config.update(
JSONIFY_PRETTYPRINT_REGULAR=False
)
@@ -71,12 +73,19 @@ def get_connection(connection_id):
return jsonify(geojson)
+def make_cache_key(*args, **kwargs): return request.url
+
+
@app.route('/api/top/', methods=["GET"])
+@cache.cached(timeout=50, key_prefix=make_cache_key)
def get_top():
if not request.args \
- or 'type' not in request.args:
+ or 'type' not in request.args \
+ or 'pageSize' not in request.args \
+ or 'pageNumber' not in request.args:
+ print(request.args)
abort(400)
- return jsonify(top.helloworld(cur, request.args["type"]))
+ return jsonify(top.helloworld(cur, request.args))
@app.route('/')
diff --git a/top.py b/top.py
index de3c552..f6ddda7 100644
--- a/top.py
+++ b/top.py
@@ -1,3 +1,5 @@
+cache = {}
+
topSQL = {
"shortestConnections": """
SELECT
@@ -12,7 +14,7 @@ FROM connections
WHERE start != goal
ORDER BY length ASC
-LIMIT 10
+LIMIT %s OFFSET %s
""",
"farAway": """
SELECT *
@@ -25,11 +27,16 @@ FROM (SELECT
WHERE start != goal
ORDER BY length ASC) AS x
GROUP BY ref
-ORDER BY length DESC"""
+ORDER BY length DESC
+LIMIT %s OFFSET %s
+"""
}
-def helloworld(cursor, top):
- cursor.execute(topSQL[top])
+def helloworld(cursor, args):
+ sql = topSQL[args["type"]]
+ limit = int(args["pageSize"])
+ offset = (int(args["pageNumber"]) - 1) * limit
+ cursor.execute(sql, (limit, offset))
return cursor.fetchall()
diff --git a/www/map.html b/www/map.html
index d143d00..34fbb30 100644
--- a/www/map.html
+++ b/www/map.html
@@ -11,6 +11,8 @@
integrity="sha256-t2/7smZfgrST4FS1DT0bs/KotCM74XlcqZN5Vu7xlrw=" crossorigin="anonymous"/>
+