fix: limit # of keys by maxsize

Picking 1024 keys assuming 4kb avg size. In practice most things are
smaller so 1024 should be good enough!

This will likely only affect multitenant deploy with many sites.
This commit is contained in:
Ankush Menat 2025-01-02 13:17:22 +05:30
parent 590e7ff185
commit 796e51df62

View file

@ -410,9 +410,10 @@ class _TrackedConnection(redis.Connection):
class _ClientCache:
def __init__(self) -> None:
def __init__(self, maxsize: int = 1024) -> None:
self.monitor = RedisWrapper.from_url(frappe.conf.get("redis_cache"))
self.monitor_id = self.monitor.client_id()
self.maxsize = maxsize or 1024 # Expect 1024 * 4kb objects ~ 4MB
self.redis: RedisWrapper = RedisWrapper.from_url(
frappe.conf.get("redis_cache"),
@ -434,9 +435,15 @@ class _ClientCache:
pass # cache miss
val = self.redis.get_value(key, shared=True)
# TODO: distinguish between none result and miss
if val is not None:
self.local_cache[key] = val
# TODO: distinguish between None result and miss
if val is None:
return None
if len(self.local_cache) >= self.maxsize:
with suppress(RuntimeError):
self.local_cache.pop(next(iter(self.local_cache)), None)
self.local_cache[key] = val
return val
def set_value(self, key, val):