From 1470ad2a6618ba902da4d7bd194b7b5d8fd93445 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 23 Jan 2025 17:23:39 +0530 Subject: [PATCH] perf: Cache plain link validation for 30 minutes Very often you're picking same documents again and again, there's no need to validate them. Also, document is JUST selected using search_link, so it's 99% guaranteed to be valid. The real purpose of this function is to provide "fetch from" feature, not link validation like the name suggests. It will get validated server side anyway. --- frappe/client.py | 6 +++++- frappe/public/js/frappe/form/controls/link.js | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/frappe/client.py b/frappe/client.py index f3d56067f8..591ef09631 100644 --- a/frappe/client.py +++ b/frappe/client.py @@ -427,7 +427,11 @@ def validate_link(doctype: str, docname: str, fields=None): values.name = frappe.db.get_value(doctype, docname, cache=True) fields = frappe.parse_json(fields) - if not values.name or not fields: + if not values.name: + return values + + if not fields: + frappe.local.response_headers.set("Cache-Control", "private,max-age=1800,stale-while-revalidate=7200") return values try: diff --git a/frappe/public/js/frappe/form/controls/link.js b/frappe/public/js/frappe/form/controls/link.js index 5b59e09ee2..dea3afd659 100644 --- a/frappe/public/js/frappe/form/controls/link.js +++ b/frappe/public/js/frappe/form/controls/link.js @@ -685,11 +685,16 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat // to avoid unnecessary request if (value) { return frappe - .xcall("frappe.client.validate_link", { - doctype: options, - docname: value, - fields: columns_to_fetch, - }) + .xcall( + "frappe.client.validate_link", + { + doctype: options, + docname: value, + fields: columns_to_fetch, + }, + "GET", + { cache: !columns_to_fetch.length } + ) .then((response) => { if (!this.docname || !columns_to_fetch.length) { return response.name;