diff --git a/authentik/core/migrations/0018_auto_20210330_1345_squashed_0028_alter_token_intent.py b/authentik/core/migrations/0018_auto_20210330_1345_squashed_0028_alter_token_intent.py index 2d934c9bd..4f2b47817 100644 --- a/authentik/core/migrations/0018_auto_20210330_1345_squashed_0028_alter_token_intent.py +++ b/authentik/core/migrations/0018_auto_20210330_1345_squashed_0028_alter_token_intent.py @@ -19,8 +19,7 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): from django.contrib.sessions.backends.cache import KEY_PREFIX from django.core.cache import cache - session_keys = cache.keys(KEY_PREFIX + "*") - cache.delete_many(session_keys) + cache.delete_pattern(KEY_PREFIX + "*") def fix_duplicates(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): diff --git a/authentik/core/migrations/0022_authenticatedsession.py b/authentik/core/migrations/0022_authenticatedsession.py index df859a1a2..bd4ba5ee8 100644 --- a/authentik/core/migrations/0022_authenticatedsession.py +++ b/authentik/core/migrations/0022_authenticatedsession.py @@ -16,8 +16,7 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): from django.contrib.sessions.backends.cache import KEY_PREFIX from django.core.cache import cache - session_keys = cache.keys(KEY_PREFIX + "*") - cache.delete_many(session_keys) + cache.delete_pattern(KEY_PREFIX + "*") class Migration(migrations.Migration): diff --git a/authentik/core/signals.py b/authentik/core/signals.py index c114ffa65..d5a97e95c 100644 --- a/authentik/core/signals.py +++ b/authentik/core/signals.py @@ -37,8 +37,7 @@ def post_save_application(sender: type[Model], instance, created: bool, **_): if not created: # pragma: no cover return # Also delete user application cache - keys = cache.keys(user_app_cache_key("*")) - cache.delete_many(keys) + cache.delete_pattern(user_app_cache_key("*")) @receiver(user_logged_in) diff --git a/authentik/flows/api/flows.py b/authentik/flows/api/flows.py index 3f24679b7..5de95c7a9 100644 --- a/authentik/flows/api/flows.py +++ b/authentik/flows/api/flows.py @@ -130,9 +130,8 @@ class FlowViewSet(UsedByMixin, ModelViewSet): @action(detail=False, methods=["POST"]) def cache_clear(self, request: Request) -> Response: """Clear flow cache""" - keys = cache.keys("flow_*") - cache.delete_many(keys) - LOGGER.debug("Cleared flow cache", keys=len(keys)) + count = cache.delete_pattern("flow_*") + LOGGER.debug("Cleared flow cache", keys=count) return Response(status=204) @permission_required( diff --git a/authentik/flows/signals.py b/authentik/flows/signals.py index a285859a5..c1f0e217e 100644 --- a/authentik/flows/signals.py +++ b/authentik/flows/signals.py @@ -7,13 +7,6 @@ from structlog.stdlib import get_logger LOGGER = get_logger() -def delete_cache_prefix(prefix: str) -> int: - """Delete keys prefixed with `prefix` and return count of deleted keys.""" - keys = cache.keys(prefix) - cache.delete_many(keys) - return len(keys) - - @receiver(post_save) @receiver(pre_delete) # pylint: disable=unused-argument @@ -23,14 +16,14 @@ def invalidate_flow_cache(sender, instance, **_): from authentik.flows.planner import cache_key if isinstance(instance, Flow): - total = delete_cache_prefix(f"{cache_key(instance)}*") + total = cache.delete_pattern(f"{cache_key(instance)}*") LOGGER.debug("Invalidating Flow cache", flow=instance, len=total) if isinstance(instance, FlowStageBinding): - total = delete_cache_prefix(f"{cache_key(instance.target)}*") + total = cache.delete_pattern(f"{cache_key(instance.target)}*") LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance, len=total) if isinstance(instance, Stage): total = 0 for binding in FlowStageBinding.objects.filter(stage=instance): prefix = cache_key(binding.target) - total += delete_cache_prefix(f"{prefix}*") + total += cache.delete_pattern(f"{prefix}*") LOGGER.debug("Invalidating Flow cache from Stage", stage=instance, len=total) diff --git a/authentik/flows/views/executor.py b/authentik/flows/views/executor.py index f0daf70e7..9a87d5eef 100644 --- a/authentik/flows/views/executor.py +++ b/authentik/flows/views/executor.py @@ -198,8 +198,7 @@ class FlowExecutorView(APIView): next_binding = self.plan.next(self.request) except Exception as exc: # pylint: disable=broad-except self._logger.warning("f(exec): found incompatible flow plan, invalidating run", exc=exc) - keys = cache.keys("flow_*") - cache.delete_many(keys) + cache.delete_pattern("flow_*") return self.stage_invalid() if not next_binding: self._logger.debug("f(exec): no more stages, flow is done.") @@ -317,8 +316,7 @@ class FlowExecutorView(APIView): # from the cache. If there are errors, just delete all cached flows _ = plan.has_stages except Exception: # pylint: disable=broad-except - keys = cache.keys("flow_*") - cache.delete_many(keys) + cache.delete_pattern("flow_*") return self._initiate_plan() return plan diff --git a/authentik/policies/api/policies.py b/authentik/policies/api/policies.py index 6b6920a2f..ae793be7b 100644 --- a/authentik/policies/api/policies.py +++ b/authentik/policies/api/policies.py @@ -128,12 +128,10 @@ class PolicyViewSet( @action(detail=False, methods=["POST"]) def cache_clear(self, request: Request) -> Response: """Clear policy cache""" - keys = cache.keys("policy_*") - cache.delete_many(keys) - LOGGER.debug("Cleared Policy cache", keys=len(keys)) + count = cache.delete_pattern("policy_*") + LOGGER.debug("Cleared Policy cache", keys=count) # Also delete user application cache - keys = cache.keys(user_app_cache_key("*")) - cache.delete_many(keys) + cache.delete_pattern(user_app_cache_key("*")) return Response(status=204) @permission_required("authentik_policies.view_policy") diff --git a/authentik/policies/signals.py b/authentik/policies/signals.py index c9baf9624..7678f293d 100644 --- a/authentik/policies/signals.py +++ b/authentik/policies/signals.py @@ -19,10 +19,7 @@ def invalidate_policy_cache(sender, instance, **_): total = 0 for binding in PolicyBinding.objects.filter(policy=instance): prefix = f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*" - keys = cache.keys(prefix) - total += len(keys) - cache.delete_many(keys) + total += cache.delete_pattern(prefix) LOGGER.debug("Invalidating policy cache", policy=instance, keys=total) # Also delete user application cache - keys = cache.keys(user_app_cache_key("*")) or [] - cache.delete_many(keys) + cache.delete_pattern(user_app_cache_key("*"))