Skip to content

Commit bcb5fe9

Browse files
committed
fix: pickle doc objects directly
This seems to be missed out and causes performance regression in IRL usage when get_doc is called on cached doc already.
1 parent abd0187 commit bcb5fe9

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

frappe/__init__.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1089,17 +1089,20 @@ def _respond(doc, from_redis=False):
10891089
if not key:
10901090
key = get_document_cache_key(doc.doctype, doc.name)
10911091

1092-
local.document_cache[key] = doc
1092+
_set_document_in_cache(key, doc)
1093+
1094+
return doc
10931095

1096+
1097+
def _set_document_in_cache(key: str, doc: "Document") -> None:
10941098
# Avoid setting in local.cache since we're already using local.document_cache above
10951099
# Try pickling the doc object as-is first, else fallback to doc.as_dict()
1100+
local.document_cache[key] = doc
10961101
try:
10971102
cache().hset("document_cache", key, doc, cache_locally=False)
10981103
except Exception:
10991104
cache().hset("document_cache", key, doc.as_dict(), cache_locally=False)
11001105

1101-
return doc
1102-
11031106

11041107
def can_cache_doc(args) -> str | None:
11051108
"""
@@ -1174,13 +1177,10 @@ def get_doc(*args, **kwargs) -> "Document":
11741177

11751178
doc = frappe.model.document.get_doc(*args, **kwargs)
11761179

1177-
# Replace cache
1180+
# Replace cache if stale one exists
11781181
if key := can_cache_doc(args):
1179-
if key in local.document_cache:
1180-
local.document_cache[key] = doc
1181-
11821182
if cache().hexists("document_cache", key):
1183-
cache().hset("document_cache", key, doc.as_dict())
1183+
_set_document_in_cache(key, doc)
11841184

11851185
return doc
11861186

0 commit comments

Comments
 (0)