This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2020-05-24 00:06:54 +00:00
|
|
|
"""passbook policy signals"""
|
|
|
|
from django.core.cache import cache
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from structlog import get_logger
|
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save)
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def invalidate_policy_cache(sender, instance, **_):
|
|
|
|
"""Invalidate Policy cache when policy is updated"""
|
|
|
|
from passbook.policies.models import Policy, PolicyBinding
|
|
|
|
|
|
|
|
if isinstance(instance, Policy):
|
|
|
|
LOGGER.debug("Invalidating policy cache", policy=instance)
|
|
|
|
total = 0
|
|
|
|
for binding in PolicyBinding.objects.filter(policy=instance):
|
2020-05-23 20:01:38 +00:00
|
|
|
prefix = (
|
|
|
|
f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*"
|
|
|
|
)
|
2020-05-24 00:06:54 +00:00
|
|
|
keys = cache.keys(prefix)
|
|
|
|
total += len(keys)
|
|
|
|
cache.delete_many(keys)
|
|
|
|
LOGGER.debug("Deleted keys", len=total)
|