mirror of
https://github.com/shadoll/moma.git
synced 2026-08-28 11:33:25 +00:00
refactor: Remove old decorators and integrate caching into the new cache subsystem
- Deleted the `renamer.decorators` package, including `caching.py` and `__init__.py`, to streamline the codebase. - Updated tests to reflect changes in import paths for caching decorators. - Added a comprehensive changelog to document major refactoring efforts and future plans. - Introduced an engineering guide detailing architecture, core components, and development setup.
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
# Decorators package
|
||||
# Import from new unified cache module
|
||||
from renamer.cache import cached_method, cached, cached_api, cached_property
|
||||
|
||||
# Keep backward compatibility
|
||||
__all__ = ['cached_method', 'cached', 'cached_api', 'cached_property']
|
||||
@@ -1,57 +0,0 @@
|
||||
"""Caching decorators for extractors."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Optional
|
||||
from renamer.cache import Cache
|
||||
|
||||
|
||||
# Global cache instance
|
||||
_cache = Cache()
|
||||
|
||||
|
||||
def cached_method(ttl_seconds: int = 3600) -> Callable:
|
||||
"""Decorator to cache method results with TTL.
|
||||
|
||||
Caches the result of a method call using a global file-based cache.
|
||||
The cache key includes class name, method name, instance identifier, and parameters hash.
|
||||
|
||||
Args:
|
||||
ttl_seconds: Time to live for cached results in seconds (default 1 hour)
|
||||
|
||||
Returns:
|
||||
The decorated method with caching
|
||||
"""
|
||||
def decorator(func: Callable) -> Callable:
|
||||
def wrapper(self, *args, **kwargs) -> Any:
|
||||
# Generate cache key: class_name.method_name.instance_id.param_hash
|
||||
class_name = self.__class__.__name__
|
||||
method_name = func.__name__
|
||||
|
||||
# Use instance identifier (file_path for extractors)
|
||||
instance_id = getattr(self, 'file_path', str(id(self)))
|
||||
# If instance_id contains path separators, hash it to avoid creating subdirs
|
||||
if '/' in str(instance_id) or '\\' in str(instance_id):
|
||||
instance_id = hashlib.md5(str(instance_id).encode('utf-8')).hexdigest()
|
||||
|
||||
# Create hash from args and kwargs only if they exist (excluding self)
|
||||
if args or kwargs:
|
||||
param_str = json.dumps((args, kwargs), sort_keys=True, default=str)
|
||||
param_hash = hashlib.md5(param_str.encode('utf-8')).hexdigest()
|
||||
cache_key = f"{class_name}.{method_name}.{instance_id}.{param_hash}"
|
||||
else:
|
||||
cache_key = f"{class_name}.{method_name}.{instance_id}"
|
||||
|
||||
# Try to get from cache
|
||||
cached_result = _cache.get_object(cache_key)
|
||||
if cached_result is not None:
|
||||
return cached_result
|
||||
|
||||
# Compute result and cache it
|
||||
result = func(self, *args, **kwargs)
|
||||
_cache.set_object(cache_key, result, ttl_seconds)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
return decorator
|
||||
@@ -235,19 +235,22 @@ class TestCacheManager:
|
||||
manager.compact_cache()
|
||||
|
||||
|
||||
class TestBackwardCompatibility:
|
||||
"""Test backward compatibility with old import paths."""
|
||||
|
||||
def test_import_from_decorators(self):
|
||||
"""Test importing from renamer.decorators still works."""
|
||||
from renamer.decorators import cached_method
|
||||
assert cached_method is not None
|
||||
class TestCachePackageImports:
|
||||
"""Test cache package import paths."""
|
||||
|
||||
def test_import_cache_from_package(self):
|
||||
"""Test importing Cache from renamer.cache package."""
|
||||
from renamer.cache import Cache as PackageCache
|
||||
assert PackageCache is not None
|
||||
|
||||
def test_import_decorators_from_cache(self):
|
||||
"""Test importing decorators from renamer.cache."""
|
||||
from renamer.cache import cached_method, cached, cached_api, cached_property
|
||||
assert cached_method is not None
|
||||
assert cached is not None
|
||||
assert cached_api is not None
|
||||
assert cached_property is not None
|
||||
|
||||
def test_create_cache_convenience_function(self):
|
||||
"""Test the create_cache convenience function."""
|
||||
from renamer.cache import create_cache
|
||||
|
||||
Reference in New Issue
Block a user