Skip to content

Commit

Permalink
perf: Reuse cached user for get_user_lang (#26350)
Browse files Browse the repository at this point in the history
Saves ~1% of overhead.

User doc is almost always cached to create session so we can reuse it in
same request without additional redis call.
  • Loading branch information
ankush authored May 7, 2024
1 parent e240c6b commit c17eb87
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions frappe/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,14 @@ def get_parent_language(lang: str) -> str:
def get_user_lang(user: str | None = None) -> str:
"""Set frappe.local.lang from user preferences on session beginning or resumption"""
user = user or frappe.session.user
lang = frappe.cache.hget("lang", user)

if not lang:
# User.language => Session Defaults => frappe.local.lang => 'en'
lang = (
frappe.db.get_value("User", user, "language")
or frappe.db.get_default("lang")
or frappe.local.lang
or "en"
)

frappe.cache.hset("lang", user, lang)

return lang
# User.language => Session Defaults => frappe.local.lang => 'en'
return (
frappe.get_cached_value("User", user, "language")
or frappe.db.get_default("lang")
or frappe.local.lang
or "en"
)


def get_lang_code(lang: str) -> str | None:
Expand Down

0 comments on commit c17eb87

Please sign in to comment.