Revert "*: use cache.delete_pattern instead of getting keys and deleting them"

This reverts commit ff481ba6e7.

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>

# Conflicts:
#	authentik/flows/views/executor.py
#	authentik/policies/signals.py
This commit is contained in:
Jens Langhammer 2021-12-12 20:41:34 +01:00
parent 5ef385f0bb
commit 597ce1eb42
8 changed files with 33 additions and 15 deletions

View file

@ -19,7 +19,8 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
from django.contrib.sessions.backends.cache import KEY_PREFIX from django.contrib.sessions.backends.cache import KEY_PREFIX
from django.core.cache import cache from django.core.cache import cache
cache.delete_pattern(KEY_PREFIX + "*") session_keys = cache.keys(KEY_PREFIX + "*")
cache.delete_many(session_keys)
def fix_duplicates(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): def fix_duplicates(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):

View file

@ -16,7 +16,8 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
from django.contrib.sessions.backends.cache import KEY_PREFIX from django.contrib.sessions.backends.cache import KEY_PREFIX
from django.core.cache import cache from django.core.cache import cache
cache.delete_pattern(KEY_PREFIX + "*") session_keys = cache.keys(KEY_PREFIX + "*")
cache.delete_many(session_keys)
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -37,7 +37,8 @@ def post_save_application(sender: type[Model], instance, created: bool, **_):
if not created: # pragma: no cover if not created: # pragma: no cover
return return
# Also delete user application cache # Also delete user application cache
cache.delete_pattern(user_app_cache_key("*")) keys = cache.keys(user_app_cache_key("*"))
cache.delete_many(keys)
@receiver(user_logged_in) @receiver(user_logged_in)

View file

@ -130,8 +130,9 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
@action(detail=False, methods=["POST"]) @action(detail=False, methods=["POST"])
def cache_clear(self, request: Request) -> Response: def cache_clear(self, request: Request) -> Response:
"""Clear flow cache""" """Clear flow cache"""
count = cache.delete_pattern("flow_*") keys = cache.keys("flow_*")
LOGGER.debug("Cleared flow cache", keys=count) cache.delete_many(keys)
LOGGER.debug("Cleared flow cache", keys=len(keys))
return Response(status=204) return Response(status=204)
@permission_required( @permission_required(

View file

@ -7,6 +7,13 @@ from structlog.stdlib import get_logger
LOGGER = 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(post_save)
@receiver(pre_delete) @receiver(pre_delete)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -16,14 +23,14 @@ def invalidate_flow_cache(sender, instance, **_):
from authentik.flows.planner import cache_key from authentik.flows.planner import cache_key
if isinstance(instance, Flow): if isinstance(instance, Flow):
total = cache.delete_pattern(f"{cache_key(instance)}*") total = delete_cache_prefix(f"{cache_key(instance)}*")
LOGGER.debug("Invalidating Flow cache", flow=instance, len=total) LOGGER.debug("Invalidating Flow cache", flow=instance, len=total)
if isinstance(instance, FlowStageBinding): if isinstance(instance, FlowStageBinding):
total = cache.delete_pattern(f"{cache_key(instance.target)}*") total = delete_cache_prefix(f"{cache_key(instance.target)}*")
LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance, len=total) LOGGER.debug("Invalidating Flow cache from FlowStageBinding", binding=instance, len=total)
if isinstance(instance, Stage): if isinstance(instance, Stage):
total = 0 total = 0
for binding in FlowStageBinding.objects.filter(stage=instance): for binding in FlowStageBinding.objects.filter(stage=instance):
prefix = cache_key(binding.target) prefix = cache_key(binding.target)
total += cache.delete_pattern(f"{prefix}*") total += delete_cache_prefix(f"{prefix}*")
LOGGER.debug("Invalidating Flow cache from Stage", stage=instance, len=total) LOGGER.debug("Invalidating Flow cache from Stage", stage=instance, len=total)

View file

@ -205,7 +205,8 @@ class FlowExecutorView(APIView):
self._logger.warning( self._logger.warning(
"f(exec): found incompatible flow plan, invalidating run", exc=exc "f(exec): found incompatible flow plan, invalidating run", exc=exc
) )
cache.delete_pattern("flow_*") keys = cache.keys("flow_*")
cache.delete_many(keys)
return self.stage_invalid() return self.stage_invalid()
if not next_binding: if not next_binding:
self._logger.debug("f(exec): no more stages, flow is done.") self._logger.debug("f(exec): no more stages, flow is done.")
@ -331,7 +332,8 @@ class FlowExecutorView(APIView):
# from the cache. If there are errors, just delete all cached flows # from the cache. If there are errors, just delete all cached flows
_ = plan.has_stages _ = plan.has_stages
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
cache.delete_pattern("flow_*") keys = cache.keys("flow_*")
cache.delete_many(keys)
return self._initiate_plan() return self._initiate_plan()
return plan return plan

View file

@ -128,10 +128,12 @@ class PolicyViewSet(
@action(detail=False, methods=["POST"]) @action(detail=False, methods=["POST"])
def cache_clear(self, request: Request) -> Response: def cache_clear(self, request: Request) -> Response:
"""Clear policy cache""" """Clear policy cache"""
count = cache.delete_pattern("policy_*") keys = cache.keys("policy_*")
LOGGER.debug("Cleared Policy cache", keys=count) cache.delete_many(keys)
LOGGER.debug("Cleared Policy cache", keys=len(keys))
# Also delete user application cache # Also delete user application cache
cache.delete_pattern(user_app_cache_key("*")) keys = cache.keys(user_app_cache_key("*"))
cache.delete_many(keys)
return Response(status=204) return Response(status=204)
@permission_required("authentik_policies.view_policy") @permission_required("authentik_policies.view_policy")

View file

@ -19,7 +19,10 @@ def invalidate_policy_cache(sender, instance, **_):
total = 0 total = 0
for binding in PolicyBinding.objects.filter(policy=instance): for binding in PolicyBinding.objects.filter(policy=instance):
prefix = f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*" prefix = f"policy_{binding.policy_binding_uuid.hex}_{binding.policy.pk.hex}*"
total += cache.delete_pattern(prefix) keys = cache.keys(prefix)
total += len(keys)
cache.delete_many(keys)
LOGGER.debug("Invalidating policy cache", policy=instance, keys=total) LOGGER.debug("Invalidating policy cache", policy=instance, keys=total)
# Also delete user application cache # Also delete user application cache
cache.delete_pattern(user_app_cache_key("*")) keys = cache.keys(user_app_cache_key("*")) or []
cache.delete_many(keys)