From 342edb76bffced042a6a7d3985bca56cc9cb950e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 20 Oct 2022 16:49:45 +0530 Subject: [PATCH] fix: only execute generator if value is not found in redis cache (#18472) (#18479) * fix: use of generator in * fix: improve docstring * fix: improve docstring * fix: directly assign value to flags Co-authored-by: Daizy (cherry picked from commit fce9ccedaa1b292a3a1a4cfc5eb6e248931d1464) Co-authored-by: Daizy Modi <54097382+DaizyModi@users.noreply.github.com> --- frappe/model/document.py | 14 +++++++++----- frappe/utils/redis_wrapper.py | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frappe/model/document.py b/frappe/model/document.py index 4812f4aa4c..269e83aca6 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -953,15 +953,19 @@ class Document(BaseDocument): from frappe.email.doctype.notification.notification import evaluate_alert if self.flags.notifications is None: - alerts = frappe.cache().hget("notifications", self.doctype) - if alerts is None: - alerts = frappe.get_all( + + def _get_notifications(): + """returns enabled notifications for the current doctype""" + + return frappe.get_all( "Notification", fields=["name", "event", "method"], filters={"enabled": 1, "document_type": self.doctype}, ) - frappe.cache().hset("notifications", self.doctype, alerts) - self.flags.notifications = alerts + + self.flags.notifications = frappe.cache().hget( + "notifications", self.doctype, _get_notifications + ) if not self.flags.notifications: return diff --git a/frappe/utils/redis_wrapper.py b/frappe/utils/redis_wrapper.py index a41e47b0c6..87637c6b5d 100644 --- a/frappe/utils/redis_wrapper.py +++ b/frappe/utils/redis_wrapper.py @@ -194,7 +194,7 @@ class RedisWrapper(redis.Redis): except redis.exceptions.ConnectionError: pass - if value: + if value is not None: value = pickle.loads(value) frappe.local.cache[_name][key] = value elif generator: