From 4d41633e295c10a53492838d24ea069d2d3a0794 Mon Sep 17 00:00:00 2001 From: ff137 Date: Thu, 28 Nov 2024 12:16:19 +0200 Subject: [PATCH] :construction: Test caching DeferLoad Signed-off-by: ff137 --- acapy_agent/utils/classloader.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/acapy_agent/utils/classloader.py b/acapy_agent/utils/classloader.py index 096da097d1..468e4c5c77 100644 --- a/acapy_agent/utils/classloader.py +++ b/acapy_agent/utils/classloader.py @@ -233,22 +233,25 @@ def scan_subpackages(cls, package: str) -> Sequence[str]: class DeferLoad: """Helper to defer loading of a class definition.""" + _class_cache = {} # Shared cache for resolved classes + def __init__(self, cls_path: str): """Initialize the `DeferLoad` instance with a qualified class path.""" self._cls_path = cls_path - self._inst = None def __call__(self, *args, **kwargs): """Magic method to call the `DeferLoad` as a function.""" LOGGER.debug("Calling deferred class load for: %s", self._cls_path) - return (self.resolved)(*args, **kwargs) + return self.resolved(*args, **kwargs) @property def resolved(self): """Accessor for the resolved class instance.""" LOGGER.debug("Resolving deferred class load for: %s", self._cls_path) - if not self._inst: + if self._cls_path not in DeferLoad._class_cache: LOGGER.debug("Class not yet loaded, loading: %s", self._cls_path) - self._inst = ClassLoader.load_class(self._cls_path) + DeferLoad._class_cache[self._cls_path] = ClassLoader.load_class( + self._cls_path + ) LOGGER.debug("Successfully loaded class: %s", self._cls_path) - return self._inst + return DeferLoad._class_cache[self._cls_path]