From 28a124ca47791d4a42d02668d524c2f83d5b3d37 Mon Sep 17 00:00:00 2001 From: Daizy Date: Sat, 22 Oct 2022 13:31:34 +0530 Subject: [PATCH] perf: cache document naming rule to avoid multiple db call --- frappe/model/naming.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/frappe/model/naming.py b/frappe/model/naming.py index d9dc0ee48c..163633b54e 100644 --- a/frappe/model/naming.py +++ b/frappe/model/naming.py @@ -235,13 +235,21 @@ def set_naming_from_document_naming_rule(doc): if doc.doctype in log_types: return - # ignore_ddl if naming is not yet bootstrapped - for d in frappe.get_all( - "Document Naming Rule", - dict(document_type=doc.doctype, disabled=0), - order_by="priority desc", - ignore_ddl=True, - ): + def _get_document_naming_rule(): + # ignore_ddl if naming is not yet bootstrapped + + return frappe.get_all( + "Document Naming Rule", + {"document_type": doc.doctype, "disabled": 0}, + order_by="priority desc", + ignore_ddl=True, + ) + + document_naming_rules = frappe.cache().hget( + "document_naming_rule", doc.doctype, _get_document_naming_rule + ) + + for d in document_naming_rules: frappe.get_cached_doc("Document Naming Rule", d.name).apply(doc) if doc.name: break