mirror of
https://github.com/shadoll/moma.git
synced 2026-08-28 11:33:25 +00:00
- Added core caching functionality with `Cache` class supporting in-memory and file-based caching. - Introduced `CacheManager` for high-level cache operations and statistics. - Created various cache key generation strategies: `FilepathMethodStrategy`, `APIRequestStrategy`, `SimpleKeyStrategy`, and `CustomStrategy`. - Developed decorators for easy method caching: `cached`, `cached_method`, `cached_api`, and `cached_property`. - Implemented type definitions for cache entries and statistics. - Added comprehensive tests for cache operations, strategies, and decorators to ensure functionality and backward compatibility.
108 lines
2.4 KiB
Python
108 lines
2.4 KiB
Python
"""Unified caching subsystem for Renamer.
|
|
|
|
This module provides a flexible caching system with:
|
|
- Multiple cache key generation strategies
|
|
- Decorators for easy method caching
|
|
- Cache management and statistics
|
|
- Thread-safe operations
|
|
- In-memory and file-based caching with TTL
|
|
|
|
Usage Examples:
|
|
# Using decorators
|
|
from renamer.cache import cached, cached_api
|
|
|
|
class MyExtractor:
|
|
def __init__(self, file_path, cache, settings):
|
|
self.file_path = file_path
|
|
self.cache = cache
|
|
self.settings = settings
|
|
|
|
@cached(ttl=3600)
|
|
def extract_data(self):
|
|
# Automatically cached using FilepathMethodStrategy
|
|
return expensive_operation()
|
|
|
|
@cached_api("tmdb", ttl=21600)
|
|
def fetch_movie_data(self, movie_id):
|
|
# Cached API response
|
|
return api_call(movie_id)
|
|
|
|
# Using cache manager
|
|
from renamer.cache import Cache, CacheManager
|
|
|
|
cache = Cache()
|
|
manager = CacheManager(cache)
|
|
|
|
# Get statistics
|
|
stats = manager.get_stats()
|
|
print(f"Total cache size: {stats['total_size_mb']} MB")
|
|
|
|
# Clear all cache
|
|
manager.clear_all()
|
|
|
|
# Clear specific prefix
|
|
manager.clear_by_prefix("tmdb_")
|
|
"""
|
|
|
|
from .core import Cache
|
|
from .managers import CacheManager
|
|
from .strategies import (
|
|
CacheKeyStrategy,
|
|
FilepathMethodStrategy,
|
|
APIRequestStrategy,
|
|
SimpleKeyStrategy,
|
|
CustomStrategy
|
|
)
|
|
from .decorators import (
|
|
cached,
|
|
cached_method,
|
|
cached_api,
|
|
cached_property
|
|
)
|
|
from .types import CacheEntry, CacheStats
|
|
|
|
__all__ = [
|
|
# Core cache
|
|
'Cache',
|
|
'CacheManager',
|
|
|
|
# Strategies
|
|
'CacheKeyStrategy',
|
|
'FilepathMethodStrategy',
|
|
'APIRequestStrategy',
|
|
'SimpleKeyStrategy',
|
|
'CustomStrategy',
|
|
|
|
# Decorators
|
|
'cached',
|
|
'cached_method',
|
|
'cached_api',
|
|
'cached_property',
|
|
|
|
# Types
|
|
'CacheEntry',
|
|
'CacheStats',
|
|
|
|
# Convenience functions
|
|
'create_cache',
|
|
]
|
|
|
|
|
|
def create_cache(cache_dir=None):
|
|
"""Create a Cache instance with Manager (convenience function).
|
|
|
|
Args:
|
|
cache_dir: Optional cache directory path
|
|
|
|
Returns:
|
|
tuple: (Cache instance, CacheManager instance)
|
|
|
|
Example:
|
|
cache, manager = create_cache()
|
|
stats = manager.get_stats()
|
|
print(f"Cache has {stats['total_files']} files")
|
|
"""
|
|
cache = Cache(cache_dir)
|
|
manager = CacheManager(cache)
|
|
return cache, manager
|