From 2ee3fc0c670e032ac2ad1c5b8ad3044c98872eba Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 3 Sep 2018 09:38:29 +0200 Subject: [PATCH] Start playing with caching --- app.py | 15 ++++++++++++++- config.py | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 7c2883b..1da742d 100644 --- a/app.py +++ b/app.py @@ -820,6 +820,12 @@ def paginated_query(db, q, limit=25, sort_key="_id"): def index(): if is_api_request(): return jsonify(**ME) + logged_in = session.get("logged_in", False) + if not logged_in: + cached = DB.cache.find_one({"path": request.path, "type": "html"}) + if cached: + app.logger.info("from cache") + return cached['response_data'] q = { "box": Box.OUTBOX.value, @@ -846,13 +852,20 @@ def index(): DB.activities, q, limit=25 - len(pinned) ) - return render_template( + resp = render_template( "index.html", outbox_data=outbox_data, older_than=older_than, newer_than=newer_than, pinned=pinned, ) + if not logged_in: + DB.cache.update_one( + {"path": request.path, "type": "html"}, + {"$set": {"response_data": resp, "date": datetime.now(timezone.utc)}}, + upsert=True, + ) + return resp @app.route("/with_replies") diff --git a/config.py b/config.py index a5940ce..dfb553c 100644 --- a/config.py +++ b/config.py @@ -109,6 +109,8 @@ def create_indexes(): ("activity.object.id", pymongo.ASCENDING), ("meta.deleted", pymongo.ASCENDING), ]) + DB.cache.create_index([("path", pymongo.ASCENDING), ("type", pymongo.ASCENDING)]) + DB.cache.create_index("date", expireAfterSeconds=60) # Index for the block query DB.activities.create_index(