fix: Avoid data races by accessing request specific cache

Imagine:
| client 1 | client 2 |
| ---      | ---      |
| rd(x)    |          |
|          |  wr(x)   |
| inv(x)   |          |
| rd(x)    |          |  <- This will end up using request specific cache!
This commit is contained in:
Ankush Menat 2025-01-02 13:41:59 +05:30
parent 764c3134cb
commit cd47bee65e

View file

@ -74,7 +74,7 @@ class RedisWrapper(redis.Redis):
with suppress(redis.exceptions.ConnectionError):
self.set(name=key, value=pickle.dumps(val, protocol=DEFAULT_PICKLE_PROTOCOL), ex=expires_in_sec)
def get_value(self, key, generator=None, user=None, expires=False, shared=False):
def get_value(self, key, generator=None, user=None, expires=False, shared=False, *, use_local_cache=True):
"""Return cache value. If not found and generator function is
given, call the generator.
@ -86,7 +86,7 @@ class RedisWrapper(redis.Redis):
key = self.make_key(key, user, shared)
local_cache = frappe.local.cache
if key in local_cache:
if key in local_cache and use_local_cache:
val = local_cache[key]
else:
@ -441,7 +441,7 @@ class _ClientCache:
except KeyError:
pass # cache miss
val = self.redis.get_value(key, shared=True)
val = self.redis.get_value(key, shared=True, use_local_cache=False)
# TODO: distinguish between None result and miss
if val is None: