Skip to content

Commit

Permalink
⚡ Add class caching to DeferLoad
Browse files Browse the repository at this point in the history
Resolves openwallet-foundation#3360

Signed-off-by: ff137 <[email protected]>
  • Loading branch information
ff137 committed Nov 28, 2024
1 parent 27edc6e commit 7ae9138
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions acapy_agent/utils/classloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,21 @@ 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."""
return (self.resolved)(*args, **kwargs)
return self.resolved(*args, **kwargs)

@property
def resolved(self):
"""Accessor for the resolved class instance."""
if not self._inst:
self._inst = ClassLoader.load_class(self._cls_path)
return self._inst
if self._cls_path not in DeferLoad._class_cache:
DeferLoad._class_cache[self._cls_path] = ClassLoader.load_class(
self._cls_path
)
return DeferLoad._class_cache[self._cls_path]

0 comments on commit 7ae9138

Please sign in to comment.