from contextlib import contextmanager from functools import wraps import logging import threading from django.core.cache import cache from django.views.decorators.cache import cache_page from django.views.decorators.vary import vary_on_headers logger = logging.getLogger(__name__) _cache_state = threading.local() def _flush_cache_patterns(*patterns): try: if hasattr(cache, 'delete_pattern'): total_deleted = 0 for pattern in patterns: deleted = cache.delete_pattern(pattern) total_deleted += deleted or 0 logger.info(f"Cache cleared for patterns {patterns} (deleted {total_deleted} keys)") return total_deleted else: cache.clear() logger.info(f"Cache cleared (fallback clear for patterns: {patterns})") return 1 except Exception as e: logger.error(f"Cache invalidation failed for patterns {patterns}: {e}") return 0 def invalidate_cache_patterns(*patterns): """ Safely invalidates Redis cache keys matching given glob patterns. If called inside a `defer_cache_invalidation()` context, collects the patterns and flushes them once on exit instead of hitting Redis repeatedly. """ if getattr(_cache_state, 'deferring', False): deferred = getattr(_cache_state, 'patterns', None) if deferred is not None: deferred.update(patterns) return 0 return _flush_cache_patterns(*patterns) @contextmanager def defer_cache_invalidation(): """ Context manager to batch/defer cache invalidations during bulk DB operations so Redis is only contacted once at the end of the block. """ was_deferring = getattr(_cache_state, 'deferring', False) if not was_deferring: _cache_state.deferring = True _cache_state.patterns = set() try: yield finally: if not was_deferring: patterns = tuple(getattr(_cache_state, 'patterns', ())) _cache_state.deferring = False _cache_state.patterns = set() if patterns: _flush_cache_patterns(*patterns) def smart_cached_view(timeout=60 * 60 * 2, key_prefix=''): """ Smart cache decorator for API views that: 1. Caches responses per Accept-Language header using Django's cache_page. 2. Bypasses cache when: - Cache-Control / Pragma: no-cache headers are present (e.g., Pull-to-refresh from mobile / web). - Query parameters: no_cache=true, refresh=true, is_bookmark=true, in_playlist=true are passed. """ def decorator(view_func): cached_func = cache_page(timeout, key_prefix=key_prefix)( vary_on_headers('Accept-Language')(view_func) ) @wraps(view_func) def _wrapped_view(request, *args, **kwargs): # 1. Check HTTP headers for no-cache directives cache_control = (request.META.get('HTTP_CACHE_CONTROL') or '').lower() pragma = (request.META.get('HTTP_PRAGMA') or '').lower() if 'no-cache' in cache_control or 'no-store' in cache_control or 'max-age=0' in cache_control or 'no-cache' in pragma: return view_func(request, *args, **kwargs) # 2. Check query params for explicit bypass query_params = getattr(request, 'GET', {}) no_cache_param = (query_params.get('no_cache') or query_params.get('refresh') or '').lower() if no_cache_param in ('1', 'true'): return view_func(request, *args, **kwargs) # 3. Check bookmark & playlist specific bypasses is_bookmark = (query_params.get('is_bookmark') or query_params.get('is_bookmarked') or '').lower() in_playlist = (query_params.get('in_playlist') or '').lower() if is_bookmark in ('1', 'true') or in_playlist in ('1', 'true'): return view_func(request, *args, **kwargs) response = cached_func(request, *args, **kwargs) response['Cache-Control'] = 'private, no-cache' return response return _wrapped_view return decorator