@@ -1089,17 +1089,20 @@ def _respond(doc, from_redis=False):
1089
1089
if not key :
1090
1090
key = get_document_cache_key (doc .doctype , doc .name )
1091
1091
1092
- local .document_cache [key ] = doc
1092
+ _set_document_in_cache (key , doc )
1093
+
1094
+ return doc
1093
1095
1096
+
1097
+ def _set_document_in_cache (key : str , doc : "Document" ) -> None :
1094
1098
# Avoid setting in local.cache since we're already using local.document_cache above
1095
1099
# Try pickling the doc object as-is first, else fallback to doc.as_dict()
1100
+ local .document_cache [key ] = doc
1096
1101
try :
1097
1102
cache ().hset ("document_cache" , key , doc , cache_locally = False )
1098
1103
except Exception :
1099
1104
cache ().hset ("document_cache" , key , doc .as_dict (), cache_locally = False )
1100
1105
1101
- return doc
1102
-
1103
1106
1104
1107
def can_cache_doc (args ) -> str | None :
1105
1108
"""
@@ -1174,13 +1177,10 @@ def get_doc(*args, **kwargs) -> "Document":
1174
1177
1175
1178
doc = frappe .model .document .get_doc (* args , ** kwargs )
1176
1179
1177
- # Replace cache
1180
+ # Replace cache if stale one exists
1178
1181
if key := can_cache_doc (args ):
1179
- if key in local .document_cache :
1180
- local .document_cache [key ] = doc
1181
-
1182
1182
if cache ().hexists ("document_cache" , key ):
1183
- cache (). hset ( "document_cache" , key , doc . as_dict () )
1183
+ _set_document_in_cache ( key , doc )
1184
1184
1185
1185
return doc
1186
1186
0 commit comments