From 6c397ea2911738317bad18938fffd88732d8cfac Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 3 May 2023 17:40:36 +0300 Subject: [PATCH] cache posts in redis --- files/routes/comments.py | 3 +++ files/routes/posts.py | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/files/routes/comments.py b/files/routes/comments.py index 69e42b98d..b32f7b034 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -387,6 +387,9 @@ def comment(v:User): g.db.flush() + if c.parent_submission: + cache.delete(f'post_{c.parent_submission}') + if v.client: return c.json(db=g.db) return {"comment": render_template("comments.html", v=v, comments=[c])} diff --git a/files/routes/posts.py b/files/routes/posts.py index c7e4a4261..7df8949bd 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -95,6 +95,10 @@ def post_id(pid, anything=None, v=None, sub=None): if g.is_api_or_xhr: abort(451, "Must be 18+ to view") return render_template("errors/nsfw.html", v=v) + if not v and not request.values.get("sort"): + result = cache.get(f'post_{p.id}') + if result: return result + if p.new: defaultsortingcomments = 'new' elif v: defaultsortingcomments = v.defaultsortingcomments else: defaultsortingcomments = "hot" @@ -173,10 +177,15 @@ def post_id(pid, anything=None, v=None, sub=None): and not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or p.author_id == v.id)): template = "submission_banned.html" - return render_template(template, v=v, p=p, ids=list(ids), + result = render_template(template, v=v, p=p, ids=list(ids), sort=sort, render_replies=True, offset=offset, sub=p.subr, fart=get_setting('fart_mode')) + if not v and not request.values.get("sort"): + cache.set(f'post_{p.id}', result) + + return result + @app.get("/view_more///") @limiter.limit(DEFAULT_RATELIMIT) @auth_desired_with_logingate