perf: finer cache eviction on db.set_value

Instead of nuking everything, just clear matching prefix
This commit is contained in:
Ankush Menat 2023-06-03 19:48:02 +05:30 committed by Ankush Menat
parent 356a2587e2
commit 98b4693dcf
3 changed files with 9 additions and 7 deletions

View file

@ -1115,9 +1115,12 @@ def get_document_cache_key(doctype: str, name: str):
return f"document_cache::{doctype}::{name}"
def clear_document_cache(doctype, name):
def clear_document_cache(doctype: str, name: str | None = None) -> None:
def clear_in_redis():
cache().delete_value(get_document_cache_key(doctype, name))
if name is not None:
cache().delete_value(get_document_cache_key(doctype, name))
else:
cache().delete_keys(get_document_cache_key(doctype, ""))
clear_in_redis()
if hasattr(db, "after_commit"):

View file

@ -125,8 +125,9 @@ def clear_doctype_cache(doctype=None):
clear_controller_cache(doctype)
cache = frappe.cache()
for key in ("is_table", "doctype_modules", "document_cache"):
for key in ("is_table", "doctype_modules"):
cache.delete_value(key)
cache.delete_keys("document_cache")
def clear_single(dt):
for name in doctype_cache_keys:

View file

@ -920,10 +920,8 @@ class Database:
if isinstance(dn, str):
frappe.clear_document_cache(dt, dn)
else:
# TODO: Fix this; doesn't work rn - gavin@frappe.io
# frappe.cache().hdel_keys(dt, "document_cache")
# Workaround: clear all document caches
frappe.cache().delete_value("document_cache")
# No way to guess which documents are modified, clear all of them
frappe.clear_document_cache(dt)
for column, value in to_update.items():
query = query.set(column, value)