updating and refactoring codebase for improved performance and maintainability
This commit is contained in:
@@ -27,6 +27,22 @@ from ProviderCore.base import Provider, SearchResult
|
||||
|
||||
_EXTERNAL_PLUGIN_ENV_VARS: tuple[str, ...] = ("MM_PLUGIN_PATH", "MEDEIA_PLUGIN_PATH")
|
||||
|
||||
# Plugin instance cache keyed by (name, config_fingerprint)
|
||||
_plugin_instance_cache: Dict[Tuple[str, str], Optional[Provider]] = {}
|
||||
_plugin_cache_lock = __import__("threading").Lock()
|
||||
|
||||
|
||||
def _config_fingerprint(config: Optional[Dict[str, Any]]) -> str:
|
||||
"""Create a stable fingerprint of config for caching purposes."""
|
||||
if config is None:
|
||||
return "none"
|
||||
try:
|
||||
import json
|
||||
normalized = json.dumps(config, sort_keys=True, default=str)
|
||||
return hashlib.md5(normalized.encode()).hexdigest()[:16]
|
||||
except Exception:
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _class_supports_method(
|
||||
plugin_class: Type[Provider],
|
||||
@@ -603,14 +619,26 @@ def get_plugin(name: str, config: Optional[Dict[str, Any]] = None) -> Optional[P
|
||||
debug(f"[plugin] Unknown plugin: {name}")
|
||||
return None
|
||||
|
||||
# Check cache first
|
||||
cache_key = (str(name).strip().lower(), _config_fingerprint(config))
|
||||
with _plugin_cache_lock:
|
||||
if cache_key in _plugin_instance_cache:
|
||||
return _plugin_instance_cache[cache_key]
|
||||
|
||||
try:
|
||||
plugin = info.plugin_class(config)
|
||||
if not plugin.validate():
|
||||
debug(f"[plugin] Plugin '{name}' is not available")
|
||||
with _plugin_cache_lock:
|
||||
_plugin_instance_cache[cache_key] = None
|
||||
return None
|
||||
with _plugin_cache_lock:
|
||||
_plugin_instance_cache[cache_key] = plugin
|
||||
return plugin
|
||||
except Exception as exc:
|
||||
debug(f"[plugin] Error initializing '{name}': {exc}")
|
||||
with _plugin_cache_lock:
|
||||
_plugin_instance_cache[cache_key] = None
|
||||
return None
|
||||
|
||||
|
||||
@@ -860,6 +888,13 @@ def resolve_inline_filters(
|
||||
return filters
|
||||
|
||||
|
||||
def clear_plugin_cache() -> None:
|
||||
"""Clear the plugin instance cache. Useful for testing or config reloads."""
|
||||
global _plugin_instance_cache
|
||||
with _plugin_cache_lock:
|
||||
_plugin_instance_cache.clear()
|
||||
|
||||
|
||||
__all__ = [
|
||||
"PluginInfo",
|
||||
"Provider",
|
||||
@@ -879,4 +914,5 @@ __all__ = [
|
||||
"selection_auto_stage_for_table",
|
||||
"plugin_inline_query_choices",
|
||||
"is_known_plugin_name",
|
||||
"clear_plugin_cache",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user