perf: Speedup get_doc by another ~1.5x (#28807)

* perf: Reduce impact of forced cache replacement on every doc access

* fix: clear cache before processing users

Note: This is just an artifact of testing model, this won't have any real
effect on execution in real system.

Basically `enqueue_on_commit` is not respected in tests and it can't be
practically supported either.
This commit is contained in:
Ankush Menat 2024-12-17 16:48:43 +05:30 committed by GitHub
parent b861a014ac
commit 30ec033747
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 8 deletions

View file

@ -1380,13 +1380,7 @@ def get_doc(*args: Any, **kwargs: Any) -> "Document":
"""
import frappe.model.document
doc = frappe.model.document.get_doc(*args, **kwargs)
# Replace cache if stale one exists
if not kwargs.get("for_update") and (key := can_cache_doc(args)) and cache.exists(key):
_set_document_in_cache(key, doc)
return doc
return frappe.model.document.get_doc(*args, **kwargs)
def get_last_doc(

View file

@ -26,6 +26,7 @@ class RoleProfile(Document):
self.name = self.role_profile
def on_update(self):
self.clear_cache()
self.queue_action(
"update_all_users",
now=frappe.flags.in_test or frappe.flags.in_install,

View file

@ -20,7 +20,7 @@ if typing.TYPE_CHECKING:
@frappe.whitelist()
def getdoc(doctype, name, user=None):
def getdoc(doctype, name):
"""
Loads a doclist for a given document. This method is called directly from the client.
Requries "doctype", "name" as form variables.
@ -38,6 +38,11 @@ def getdoc(doctype, name, user=None):
doc.check_permission("read")
# Replace cache if stale one exists
# PERF: This should be eventually removed completely when we are sure about caching correctness
if (key := frappe.can_cache_doc((doctype, name))) and frappe.cache.exists(key):
frappe._set_document_in_cache(key, doc)
run_onload(doc)
doc.apply_fieldlevel_read_permissions()