implement cloudflare caching instead of redis caching

pull/143/head
Aevann 2023-05-03 23:41:41 +03:00
parent e296505dff
commit a606d9ed67
4 changed files with 24 additions and 13 deletions

View File

@ -61,6 +61,7 @@ def after_request(response:Response):
if response.status_code < 400:
if hasattr(g, 'v') and g.v:
user_id = g.v.id
_set_cloudflare_cookie(response)
_commit_and_close_db()
if request.method == "POST" and not request.path.startswith('/casino/twentyone/'):
@ -76,6 +77,26 @@ def teardown_request(error):
_rollback_and_close_db()
stdout.flush()
def _set_cloudflare_cookie(response:Response) -> None:
if not g.desires_auth: return
if IS_LOCALHOST: return
logged_in = bool(getattr(g, 'v', None))
if not logged_in and request.cookies.get("logged_in"):
response.delete_cookie(
"logged_in",
domain=f'.{SITE}',
samesite="Lax",
)
elif logged_in and not request.cookies.get("logged_in"):
response.set_cookie(
"logged_in",
"True",
max_age=SESSION_LIFETIME,
samesite="Lax",
domain=f'.{SITE}',
)
def _commit_and_close_db() -> bool:
if not getattr(g, 'db', None): return False
g.db.commit()

View File

@ -387,9 +387,6 @@ 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])}

View File

@ -124,6 +124,7 @@ def log_failed_admin_login_attempt(account:User, type:str):
def on_login(account, redir=None):
session.permanent = True
session["lo_user"] = account.id
g.v = new_user.id
session["login_nonce"] = account.login_nonce
check_for_alts(account, include_current_session=True)
@ -348,6 +349,7 @@ def sign_up_post(v:Optional[User]):
session.permanent = True
session["lo_user"] = new_user.id
g.v = new_user.id
check_for_alts(new_user, include_current_session=True)
send_notification(new_user.id, WELCOME_MSG)

View File

@ -95,10 +95,6 @@ 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"
@ -177,15 +173,10 @@ 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"
result = render_template(template, v=v, p=p, ids=list(ids),
return 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/<int:pid>/<sort>/<offset>")
@limiter.limit(DEFAULT_RATELIMIT)
@auth_desired_with_logingate