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:
parent
5ef385f0bb
commit
597ce1eb42
|
@ -19,7 +19,8 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|||
from django.contrib.sessions.backends.cache import KEY_PREFIX
|
||||
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):
|
||||
|
|
|
@ -16,7 +16,8 @@ def migrate_sessions(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
|
|||
from django.contrib.sessions.backends.cache import KEY_PREFIX
|
||||
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):
|
||||
|
|
|
@ -37,7 +37,8 @@ def post_save_application(sender: type[Model], instance, created: bool, **_):
|
|||
if not created: # pragma: no cover
|
||||
return
|
||||
# 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)
|
||||
|
|
|
@ -130,8 +130,9 @@ class FlowViewSet(UsedByMixin, ModelViewSet):
|
|||
@action(detail=False, methods=["POST"])
|
||||
def cache_clear(self, request: Request) -> Response:
|
||||
"""Clear flow cache"""
|
||||
count = cache.delete_pattern("flow_*")
|
||||
LOGGER.debug("Cleared flow cache", keys=count)
|
||||
keys = cache.keys("flow_*")
|
||||
cache.delete_many(keys)
|
||||
LOGGER.debug("Cleared flow cache", keys=len(keys))
|
||||
return Response(status=204)
|
||||
|
||||
@permission_required(
|
||||
|
|
|
@ -7,6 +7,13 @@ 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
|
||||
|
@ -16,14 +23,14 @@ def invalidate_flow_cache(sender, instance, **_):
|
|||
from authentik.flows.planner import cache_key
|
||||
|
||||
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)
|
||||
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)
|
||||
if isinstance(instance, Stage):
|
||||
total = 0
|
||||
for binding in FlowStageBinding.objects.filter(stage=instance):
|
||||
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)
|
||||
|
|
|
@ -205,7 +205,8 @@ class FlowExecutorView(APIView):
|
|||
self._logger.warning(
|
||||
"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()
|
||||
if not next_binding:
|
||||
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
|
||||
_ = plan.has_stages
|
||||
except Exception: # pylint: disable=broad-except
|
||||
cache.delete_pattern("flow_*")
|
||||
keys = cache.keys("flow_*")
|
||||
cache.delete_many(keys)
|
||||
return self._initiate_plan()
|
||||
return plan
|
||||
|
||||
|
|
|
@ -128,10 +128,12 @@ class PolicyViewSet(
|
|||
@action(detail=False, methods=["POST"])
|
||||
def cache_clear(self, request: Request) -> Response:
|
||||
"""Clear policy cache"""
|
||||
count = cache.delete_pattern("policy_*")
|
||||
LOGGER.debug("Cleared Policy cache", keys=count)
|
||||
keys = cache.keys("policy_*")
|
||||
cache.delete_many(keys)
|
||||
LOGGER.debug("Cleared Policy cache", keys=len(keys))
|
||||
# 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)
|
||||
|
||||
@permission_required("authentik_policies.view_policy")
|
||||
|
|
|
@ -19,7 +19,10 @@ 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}*"
|
||||
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)
|
||||
# Also delete user application cache
|
||||
cache.delete_pattern(user_app_cache_key("*"))
|
||||
keys = cache.keys(user_app_cache_key("*")) or []
|
||||
cache.delete_many(keys)
|
||||
|
|
Reference in New Issue