Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Add class caching to DeferLoad #3361

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
Loading