mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-06-05 07:18:48 +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>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section>
|
<section class="block content">
|
||||||
{{ dead_key_count }}
|
<h2>{% trans "Outdated cache keys" %}</h2>
|
||||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
<div class="box">
|
||||||
{% csrf_token %}
|
<p>
|
||||||
<button type="submit" class="button">go</button>
|
{% blocktrans trimmed %}
|
||||||
</form>
|
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.
|
||||||
<form name="erase-keys" method="POST" action="{% url 'settings-redis' %}">
|
{% endblocktrans %}
|
||||||
{% csrf_token %}
|
</p>
|
||||||
<input type="hidden" name="dry_run" value="True">
|
|
||||||
<button type="submit" class="button">dry run</button>
|
{% if outdated_identified is not None %}
|
||||||
</form>
|
<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>
|
||||||
|
|
||||||
|
<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 %}
|
{% else %}
|
||||||
|
|
||||||
<div class="notification is-danger is-flex is-align-items-start">
|
<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 class="icon icon-warning is-size-4 pr-3" aria-hidden="true"></span>
|
||||||
<span>
|
<span>
|
||||||
|
|
|
@ -21,12 +21,7 @@ class RedisStatus(View):
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
"""See workers and active tasks"""
|
"""See workers and active tasks"""
|
||||||
data = {"errors": []}
|
data = view_data()
|
||||||
try:
|
|
||||||
data["info"] = r.info
|
|
||||||
# pylint: disable=broad-except
|
|
||||||
except Exception as err:
|
|
||||||
data["errors"].append(err)
|
|
||||||
|
|
||||||
return TemplateResponse(request, "settings/redis.html", data)
|
return TemplateResponse(request, "settings/redis.html", data)
|
||||||
|
|
||||||
|
@ -34,19 +29,39 @@ class RedisStatus(View):
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""Erase invalid keys"""
|
"""Erase invalid keys"""
|
||||||
dry_run = request.POST.get("dry_run")
|
dry_run = request.POST.get("dry_run")
|
||||||
patterns = [":*:*"] # this pattern is a django cache with no prefix
|
erase_cache = request.POST.get("erase_cache")
|
||||||
for user_id in models.User.objects.filter(
|
data_key = "cache" if erase_cache else "outdated"
|
||||||
is_deleted=True, local=True
|
|
||||||
).values_list("id", flat=True):
|
if erase_cache:
|
||||||
patterns.append(f"{user_id}-*")
|
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
|
deleted_count = 0
|
||||||
for pattern in patterns:
|
for pattern in patterns:
|
||||||
deleted_count += erase_keys(pattern, dry_run=dry_run)
|
deleted_count += erase_keys(pattern, dry_run=dry_run)
|
||||||
|
|
||||||
|
data = view_data()
|
||||||
if dry_run:
|
if dry_run:
|
||||||
return HttpResponse(f"{deleted_count} keys identified for deletion")
|
data[f"{data_key}_identified"] = deleted_count
|
||||||
return HttpResponse(f"{deleted_count} keys deleted")
|
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):
|
def erase_keys(pattern, count=1000, dry_run=False):
|
||||||
|
|
Loading…
Reference in a new issue