[英]Cache flask-login current_user.is_authenticated
If I have a template with multiples validations of {% if current_user.is_authenticated %}
it makes sense to use something like this to minimize the overhead and then just check current_user_is_authenticated
?如果我有一个具有
{% if current_user.is_authenticated %}
多重验证的模板,那么使用这样的东西来最小化开销然后只检查current_user_is_authenticated
是有意义的吗?
{% if current_user.is_authenticated %}
{% set current_user_is_authenticated = True %}
{% else %}
{% set current_user_is_authenticated = False %}
{% endif %}
First of all it is up to you how you implement is_authenticated
method on the user class.首先,如何在用户 class 上实现
is_authenticated
方法取决于您。 By default it always just return True
for current user if he is logged in.默认情况下,如果当前用户已登录,它总是只为当前用户返回
True
。
Answering you question: if you don't have custom is_authenticated
method implementation then there is not a single thing to minimize overhead.回答您的问题:如果您没有自定义
is_authenticated
方法实现,那么就没有一件事可以最大限度地减少开销。 However if you query db, make external api calls etc. in is_authenticated
it would be some kind of optimization to store its return value in variable when using it multiple times in the template.但是,如果您查询数据库,在
is_authenticated
中进行外部 api 调用等,在模板中多次使用它时将其返回值存储在变量中将是某种优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.