perf(cache): add proper file caching at the import instantiation

This commit is contained in:
Sam Chau
2025-09-04 00:17:55 +09:30
parent 88f11b65aa
commit cf67a1c985
3 changed files with 19 additions and 5 deletions

View File

@@ -12,7 +12,8 @@ logger = logging.getLogger(__name__)
def compile_format_to_api_structure(
format_yaml: Dict[str, Any],
arr_type: str
arr_type: str,
patterns: Dict[str, str] = None
) -> Dict[str, Any]:
"""
Compile a format from YAML to Arr API structure.
@@ -20,12 +21,15 @@ def compile_format_to_api_structure(
Args:
format_yaml: Format data from YAML file
arr_type: 'radarr' or 'sonarr'
patterns: Pre-loaded regex patterns (if None, will load from disk)
Returns:
Compiled format ready for API
"""
target_app = TargetApp.RADARR if arr_type.lower() == 'radarr' else TargetApp.SONARR
patterns = load_regex_patterns()
# Only load patterns if not provided
if patterns is None:
patterns = load_regex_patterns()
compiled = {
'name': format_yaml.get('name', 'Unknown')

View File

@@ -22,6 +22,11 @@ class FormatStrategy(ImportStrategy):
Returns:
Dictionary with 'formats' key containing compiled formats
"""
from ..utils import load_regex_patterns
# Load all regex patterns once at the start
patterns = load_regex_patterns()
formats = []
failed = []
import_logger = get_import_logger()
@@ -35,7 +40,7 @@ class FormatStrategy(ImportStrategy):
format_yaml = load_yaml(f"custom_format/{filename}.yml")
# Compile to API structure
compiled = compile_format_to_api_structure(format_yaml, self.arr_type)
compiled = compile_format_to_api_structure(format_yaml, self.arr_type, patterns)
# Add unique suffix if needed
if self.import_as_unique:

View File

@@ -22,6 +22,11 @@ class ProfileStrategy(ImportStrategy):
Returns:
Dictionary with 'profiles' and 'formats' keys
"""
from ..utils import load_regex_patterns
# Load all regex patterns once at the start
patterns = load_regex_patterns()
profiles = []
all_formats = []
processed_formats: Set[str] = set()
@@ -49,7 +54,7 @@ class ProfileStrategy(ImportStrategy):
try:
format_yaml = load_yaml(f"custom_format/{format_name}.yml")
compiled_format = compile_format_to_api_structure(format_yaml, self.arr_type)
compiled_format = compile_format_to_api_structure(format_yaml, self.arr_type, patterns)
if self.import_as_unique:
compiled_format['name'] = self.add_unique_suffix(compiled_format['name'])
@@ -72,7 +77,7 @@ class ProfileStrategy(ImportStrategy):
for lang_format in language_formats:
lang_name = lang_format.get('name', 'Language format')
compiled_lang = compile_format_to_api_structure(lang_format, self.arr_type)
compiled_lang = compile_format_to_api_structure(lang_format, self.arr_type, patterns)
if self.import_as_unique:
compiled_lang['name'] = self.add_unique_suffix(compiled_lang['name'])