简体   繁体   中英

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 %}
    {% 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. By default it always just return True for current user if he is logged in.

Answering you question: if you don't have custom is_authenticated method implementation then there is not a single thing to minimize overhead. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM