feat: Enhance caching mechanism with MD5 hashing for instance identifiers

This commit is contained in:
sHa
2025-12-30 09:08:14 +00:00
parent dff245c37f
commit 67cf6a0c8b
5 changed files with 6 additions and 4 deletions

BIN
dist/renamer-0.5.7-py3-none-any.whl vendored Normal file

Binary file not shown.

View File

@@ -1,6 +1,6 @@
[project] [project]
name = "renamer" name = "renamer"
version = "0.5.6" version = "0.5.7"
description = "Terminal-based media file renamer and metadata viewer" description = "Terminal-based media file renamer and metadata viewer"
readme = "README.md" readme = "README.md"
requires-python = ">=3.11" requires-python = ">=3.11"

View File

@@ -31,6 +31,8 @@ def cached_method(ttl_seconds: int = 3600) -> Callable:
# Use instance identifier (file_path for extractors) # Use instance identifier (file_path for extractors)
instance_id = getattr(self, 'file_path', str(id(self))) instance_id = getattr(self, 'file_path', str(id(self)))
if isinstance(instance_id, Path):
instance_id = hashlib.md5(str(instance_id).encode('utf-8')).hexdigest()
# Create hash from args and kwargs (excluding self) # Create hash from args and kwargs (excluding self)
param_str = json.dumps((args, kwargs), sort_keys=True, default=str) param_str = json.dumps((args, kwargs), sort_keys=True, default=str)

View File

@@ -5,6 +5,7 @@ from .mediainfo_extractor import MediaInfoExtractor
from .fileinfo_extractor import FileInfoExtractor from .fileinfo_extractor import FileInfoExtractor
from .tmdb_extractor import TMDBExtractor from .tmdb_extractor import TMDBExtractor
from .default_extractor import DefaultExtractor from .default_extractor import DefaultExtractor
import hashlib
class MediaExtractor: class MediaExtractor:
@@ -13,8 +14,8 @@ class MediaExtractor:
@classmethod @classmethod
def create(cls, file_path: Path, cache=None, ttl_seconds: int = 21600): def create(cls, file_path: Path, cache=None, ttl_seconds: int = 21600):
"""Factory method that returns cached object if available, else creates new.""" """Factory method that returns cached object if available, else creates new."""
cache_key = f"extractor_{hashlib.md5(str(file_path).encode('utf-8')).hexdigest()}"
if cache: if cache:
cache_key = f"extractor_{file_path}"
cached_obj = cache.get_object(cache_key) cached_obj = cache.get_object(cache_key)
if cached_obj: if cached_obj:
print(f"Loaded MediaExtractor object from cache for {file_path.name}") print(f"Loaded MediaExtractor object from cache for {file_path.name}")
@@ -25,7 +26,6 @@ class MediaExtractor:
# Cache the object # Cache the object
if cache: if cache:
cache_key = f"extractor_{file_path}"
cache.set_object(cache_key, instance, ttl_seconds) cache.set_object(cache_key, instance, ttl_seconds)
print(f"Cached MediaExtractor object for {file_path.name}") print(f"Cached MediaExtractor object for {file_path.name}")

2
uv.lock generated
View File

@@ -342,7 +342,7 @@ wheels = [
[[package]] [[package]]
name = "renamer" name = "renamer"
version = "0.5.6" version = "0.5.7"
source = { editable = "." } source = { editable = "." }
dependencies = [ dependencies = [
{ name = "langcodes" }, { name = "langcodes" },