mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-23 15:38:08 +00:00
Option to clear Django cache
This commit is contained in:
parent
4bab19c126
commit
6c252c1311
2 changed files with 87 additions and 24 deletions
|
@ -33,19 +33,67 @@
|
|||
</div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
{{ dead_key_count }}
|
||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="button">go</button>
|
||||
</form>
|
||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="dry_run" value="True">
|
||||
<button type="submit" class="button">dry run</button>
|
||||
</form>
|
||||
<section class="block content">
|
||||
<h2>{% trans "Outdated cache keys" %}</h2>
|
||||
<div class="box">
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
This will scan for keys in the Django redis cache that use no prefix (the current prefix is <code>{{ prefix }}</code>), and identify Activity Streams for users with deleted accounts.
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
{% if outdated_identified is not None %}
|
||||
<p class="notification has-text-weight-bold">
|
||||
{% blocktrans trimmed with keys=outdated_identified|intcomma %}
|
||||
{{ keys }} identified
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
{% if outdated_identified > 0 %}
|
||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="button is-danger">{% trans "Erase outdated keys" %}</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="dry_run" value="True">
|
||||
<button type="submit" class="button">
|
||||
{% trans "Scan for outdated keys" %}
|
||||
</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="block content">
|
||||
<h2>{% trans "Clear Django cache" %}</h2>
|
||||
<div class="box">
|
||||
<p>
|
||||
{% blocktrans trimmed %}
|
||||
This is <strong>NOT recommended</strong> and should only be used if something has gone very wrong with your cache. All sessions will be cleared and users will be logged out of their accounts.
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
|
||||
{% if cache_deleted %}
|
||||
<p class="notification has-text-weight-bold">
|
||||
{% blocktrans trimmed with keys=cache_deleted|intcomma %}
|
||||
{{ keys }} keys deleted
|
||||
{% endblocktrans %}
|
||||
</p>
|
||||
{% else %}
|
||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="erase_cache" value="True">
|
||||
<button type="submit" class="button is-danger">{% trans "Erase cache" %}</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{% else %}
|
||||
|
||||
<div class="notification is-danger is-flex is-align-items-start">
|
||||
<span class="icon icon-warning is-size-4 pr-3" aria-hidden="true"></span>
|
||||
<span>
|
||||
|
|
|
@ -21,12 +21,7 @@ class RedisStatus(View):
|
|||
|
||||
def get(self, request):
|
||||
"""See workers and active tasks"""
|
||||
data = {"errors": []}
|
||||
try:
|
||||
data["info"] = r.info
|
||||
# pylint: disable=broad-except
|
||||
except Exception as err:
|
||||
data["errors"].append(err)
|
||||
data = view_data()
|
||||
|
||||
return TemplateResponse(request, "settings/redis.html", data)
|
||||
|
||||
|
@ -34,19 +29,39 @@ class RedisStatus(View):
|
|||
def post(self, request):
|
||||
"""Erase invalid keys"""
|
||||
dry_run = request.POST.get("dry_run")
|
||||
patterns = [":*:*"] # this pattern is a django cache with no prefix
|
||||
for user_id in models.User.objects.filter(
|
||||
is_deleted=True, local=True
|
||||
).values_list("id", flat=True):
|
||||
patterns.append(f"{user_id}-*")
|
||||
erase_cache = request.POST.get("erase_cache")
|
||||
data_key = "cache" if erase_cache else "outdated"
|
||||
|
||||
if erase_cache:
|
||||
patterns = [f"{settings.CACHE_KEY_PREFIX}:*:*"]
|
||||
else:
|
||||
patterns = [":*:*"] # this pattern is a django cache with no prefix
|
||||
for user_id in models.User.objects.filter(
|
||||
is_deleted=True, local=True
|
||||
).values_list("id", flat=True):
|
||||
patterns.append(f"{user_id}-*")
|
||||
|
||||
deleted_count = 0
|
||||
for pattern in patterns:
|
||||
deleted_count += erase_keys(pattern, dry_run=dry_run)
|
||||
|
||||
data = view_data()
|
||||
if dry_run:
|
||||
return HttpResponse(f"{deleted_count} keys identified for deletion")
|
||||
return HttpResponse(f"{deleted_count} keys deleted")
|
||||
data[f"{data_key}_identified"] = deleted_count
|
||||
else:
|
||||
data[f"{data_key}_deleted"] = deleted_count
|
||||
return TemplateResponse(request, "settings/redis.html", data)
|
||||
|
||||
|
||||
def view_data():
|
||||
"""Helper function to load basic info for the view"""
|
||||
data = {"errors": [], "prefix": settings.CACHE_KEY_PREFIX}
|
||||
try:
|
||||
data["info"] = r.info
|
||||
# pylint: disable=broad-except
|
||||
except Exception as err:
|
||||
data["errors"].append(err)
|
||||
return data
|
||||
|
||||
|
||||
def erase_keys(pattern, count=1000, dry_run=False):
|
||||
|
|
Loading…
Reference in a new issue