From 29913773a7d1197c8693fecceb8fce2859dbb5c2 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Thu, 21 Mar 2019 11:29:11 +0100 Subject: [PATCH] invalidate cache when policy is saved --- passbook/core/signals.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/passbook/core/signals.py b/passbook/core/signals.py index 5ca9b243b..53b30cb6f 100644 --- a/passbook/core/signals.py +++ b/passbook/core/signals.py @@ -1,10 +1,15 @@ """passbook core signals""" +from logging import getLogger +from django.core.cache import cache from django.core.signals import Signal +from django.db.models.signals import post_save from django.dispatch import receiver from passbook.core.exceptions import PasswordPolicyInvalid +LOGGER = getLogger(__name__) + user_signed_up = Signal(providing_args=['request', 'user']) invitation_created = Signal(providing_args=['request', 'invitation']) invitation_used = Signal(providing_args=['request', 'invitation', 'user']) @@ -24,3 +29,14 @@ def password_policy_checker(sender, password, **kwargs): passing, messages = policy_engine.result if not passing: raise PasswordPolicyInvalid(*messages) + +@receiver(post_save) +# pylint: disable=unused-argument +def invalidate_policy_cache(sender, instance, **kwargs): + """Invalidate Policy cache when policy is updated""" + from passbook.core.models import Policy + if isinstance(instance, Policy): + LOGGER.debug("Invalidating cache for %s", instance.pk) + keys = cache.keys("%s#*" % instance.pk) + cache.delete_many(keys) + LOGGER.debug("Deleted %d keys", len(keys))