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

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-12-10 21:35:28 +01:00
parent a106bad2db
commit ff481ba6e7
8 changed files with 15 additions and 33 deletions

View File

@ -19,8 +19,7 @@ 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
session_keys = cache.keys(KEY_PREFIX + "*") cache.delete_pattern(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,8 +16,7 @@ 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
session_keys = cache.keys(KEY_PREFIX + "*") cache.delete_pattern(KEY_PREFIX + "*")
cache.delete_many(session_keys)
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -37,8 +37,7 @@ 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
keys = cache.keys(user_app_cache_key("*")) cache.delete_pattern(user_app_cache_key("*"))
cache.delete_many(keys)
@receiver(user_logged_in) @receiver(user_logged_in)

View File

@ -130,9 +130,8 @@ 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"""
keys = cache.keys("flow_*") count = cache.delete_pattern("flow_*")
cache.delete_many(keys) LOGGER.debug("Cleared flow cache", keys=count)
LOGGER.debug("Cleared flow cache", keys=len(keys))
return Response(status=204) return Response(status=204)
@permission_required( @permission_required(

View File

@ -7,13 +7,6 @@ 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
@ -23,14 +16,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 = delete_cache_prefix(f"{cache_key(instance)}*") total = cache.delete_pattern(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 = 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) 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 += delete_cache_prefix(f"{prefix}*") total += cache.delete_pattern(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

@ -198,8 +198,7 @@ class FlowExecutorView(APIView):
next_binding = self.plan.next(self.request) next_binding = self.plan.next(self.request)
except Exception as exc: # pylint: disable=broad-except except Exception as exc: # pylint: disable=broad-except
self._logger.warning("f(exec): found incompatible flow plan, invalidating run", exc=exc) self._logger.warning("f(exec): found incompatible flow plan, invalidating run", exc=exc)
keys = cache.keys("flow_*") cache.delete_pattern("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.")
@ -317,8 +316,7 @@ 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
keys = cache.keys("flow_*") cache.delete_pattern("flow_*")
cache.delete_many(keys)
return self._initiate_plan() return self._initiate_plan()
return plan return plan

View File

@ -128,12 +128,10 @@ 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"""
keys = cache.keys("policy_*") count = cache.delete_pattern("policy_*")
cache.delete_many(keys) LOGGER.debug("Cleared Policy cache", keys=count)
LOGGER.debug("Cleared Policy cache", keys=len(keys))
# Also delete user application cache # Also delete user application cache
keys = cache.keys(user_app_cache_key("*")) cache.delete_pattern(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,10 +19,7 @@ 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}*"
keys = cache.keys(prefix) total += cache.delete_pattern(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
keys = cache.keys(user_app_cache_key("*")) or [] cache.delete_pattern(user_app_cache_key("*"))
cache.delete_many(keys)