uchill/backend/apps/users/cache_utils.py

43 lines
1.4 KiB
Python

"""
Утилиты для управления кешем.
"""
from django.core.cache import cache
def invalidate_user_cache(user_id: int, cache_prefix: str = None):
"""
Инвалидировать кеш для конкретного пользователя.
Args:
user_id: ID пользователя
cache_prefix: Префикс кеша (например, 'dashboard', 'profile')
"""
if cache_prefix:
cache_key = f'{cache_prefix}_{user_id}'
cache.delete(cache_key)
else:
# Удаляем все кеши для пользователя
prefixes = ['mentor_dashboard', 'client_dashboard', 'parent_dashboard', 'profile']
for prefix in prefixes:
cache.delete(f'{prefix}_{user_id}')
def invalidate_dashboard_cache(user_id: int, role: str = None):
"""
Инвалидировать кеш дашборда для пользователя.
Args:
user_id: ID пользователя
role: Роль пользователя ('mentor', 'client', 'parent')
"""
if role:
cache_key = f'{role}_dashboard_{user_id}'
cache.delete(cache_key)
else:
# Удаляем все возможные кеши дашборда
for role_name in ['mentor', 'client', 'parent']:
cache.delete(f'{role_name}_dashboard_{user_id}')