Dispatcharr/core/signals.py
SergeantPanda 2b62928b42 feat(settings): implement Redis caching for CoreSettings and enhance network access handling
This update introduces Redis caching for grouped settings in the CoreSettings model, improving performance by reducing database queries. A new method, `get_network_access_settings`, is added to streamline access to network settings. Additionally, the `setting_flag_enabled` function is introduced to handle boolean settings more robustly. The cache is invalidated on save and delete operations, ensuring data consistency. Tests have been added to validate the caching behavior and the new settings functionality.
2026-07-18 17:33:02 +00:00

47 lines
1.9 KiB
Python

from django.db.models.signals import pre_delete, post_delete, post_save
from django.dispatch import receiver
from django.core.exceptions import ValidationError
from .models import StreamProfile, CoreSettings, NETWORK_ACCESS_KEY
@receiver(pre_delete, sender=StreamProfile)
def prevent_deletion_if_locked(sender, instance, **kwargs):
if instance.locked:
raise ValidationError("This profile is locked and cannot be deleted.")
@receiver(post_save, sender=CoreSettings)
@receiver(post_delete, sender=CoreSettings)
def handle_coresettings_cache_invalidation(sender, instance, **kwargs):
"""Drop Redis group cache whenever a CoreSettings row is saved or deleted."""
CoreSettings.invalidate_group_cache(instance.key)
@receiver(post_save, sender=CoreSettings)
def handle_network_access_update(sender, instance, **kwargs):
"""Sync developer notifications when network access settings change."""
if instance.key != NETWORK_ACCESS_KEY:
return
from django.core.cache import cache
from core.developer_notifications import sync_developer_notifications
import logging
logger = logging.getLogger(__name__)
# Invalidate all notification condition caches
try:
cache.delete_pattern('dev_notif_condition_*')
logger.info("Invalidated notification condition cache due to network access settings update")
except Exception as e:
logger.warning(f"Failed to delete cache pattern: {e}")
# Fallback: try to clear entire cache (if delete_pattern not supported)
try:
cache.clear()
except Exception:
pass
# Re-sync developer notifications to re-evaluate conditions
# (websocket notification is sent by sync_developer_notifications)
try:
sync_developer_notifications()
logger.info("Re-synced developer notifications after network access settings update")
except Exception as e:
logger.error(f"Failed to sync developer notifications: {e}")