From f332852415dc5059a8efa6d3f6436a0c419bb76e Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 6 Jan 2025 15:43:03 +0530 Subject: [PATCH] fix: start tracking keys set by us immediately --- frappe/tests/test_client_cache.py | 4 +++- frappe/utils/redis_wrapper.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/frappe/tests/test_client_cache.py b/frappe/tests/test_client_cache.py index 264c221380..39cc314f45 100644 --- a/frappe/tests/test_client_cache.py +++ b/frappe/tests/test_client_cache.py @@ -31,7 +31,9 @@ class TestClientCache(IntegrationTestCase): # frappe.cache is our "another client" val = frappe.generate_hash() frappe.cache.set_value(TEST_KEY, val) - time.sleep(0.5) + # This is almost instant, but obviously not as fast as running the next instruction in + # current thread. So we wait. + time.sleep(0.1) with self.assertRedisCallCounts(1): self.assertEqual(frappe.client_cache.get_value(TEST_KEY), val) diff --git a/frappe/utils/redis_wrapper.py b/frappe/utils/redis_wrapper.py index 151aeed418..7f8166d1d3 100644 --- a/frappe/utils/redis_wrapper.py +++ b/frappe/utils/redis_wrapper.py @@ -462,6 +462,12 @@ class _ClientCache: key = self.redis.make_key(key) self.redis.set_value(key, val, shared=True) self.local_cache[key] = (val, time.monotonic() + self.local_ttl) + # XXX: We need to tell redis that we indeed read this key we just wrote + # This is an edge case: + # - Client A writes a key and reads it again from local cache + # - Client B overwrites this key, but since client A never "read" it from Redis, Redis + # doesn't send invalidation. + _ = self.redis.get_value(key, shared=True, use_local_cache=False) def delete_value(self, key): key = self.redis.make_key(key)