簡體   English   中英

緩存燒瓶登錄 current_user.is_authenticated

[英]Cache flask-login 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 %}

首先,如何在用戶 class 上實現is_authenticated方法取決於您。 默認情況下,如果當前用戶已登錄,它總是只為當前用戶返回True

回答您的問題:如果您沒有自定義is_authenticated方法實現,那么就沒有一件事可以最大限度地減少開銷。 但是,如果您查詢數據庫,在is_authenticated中進行外部 api 調用等,在模板中多次使用它時將其返回值存儲在變量中將是某種優化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM