Cache follow button template snippet

This commit is contained in:
Mouse Reeve 2022-01-05 12:55:19 -08:00
parent a5309e9973
commit 6823d5f1b7
2 changed files with 20 additions and 0 deletions

View file

@ -1,5 +1,7 @@
""" defines relationships between users """ """ defines relationships between users """
from django.apps import apps from django.apps import apps
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import models, transaction, IntegrityError from django.db import models, transaction, IntegrityError
from django.db.models import Q from django.db.models import Q
@ -36,6 +38,20 @@ class UserRelationship(BookWyrmModel):
"""the remote user needs to recieve direct broadcasts""" """the remote user needs to recieve direct broadcasts"""
return [u for u in [self.user_subject, self.user_object] if not u.local] return [u for u in [self.user_subject, self.user_object] if not u.local]
def save(self, *args, **kwargs):
"""clear the template cache"""
# invalidate the template cache
cache_keys = [
make_template_fragment_key(
"follow_button", [self.user_subject.id, self.user_object.id]
),
make_template_fragment_key(
"follow_button", [self.user_object.id, self.user_subject.id]
),
]
cache.delete_many(cache_keys)
super().save(*args, **kwargs)
class Meta: class Meta:
"""relationships should be unique""" """relationships should be unique"""

View file

@ -1,4 +1,7 @@
{% load i18n %} {% load i18n %}
{% load cache %}
{% cache None follow_button request.user.id user.id %}
{% if request.user == user or not request.user.is_authenticated %} {% if request.user == user or not request.user.is_authenticated %}
{% elif user in request.user.blocks.all %} {% elif user in request.user.blocks.all %}
{% include 'snippets/block_button.html' with blocks=True %} {% include 'snippets/block_button.html' with blocks=True %}
@ -42,3 +45,4 @@
{% endif %} {% endif %}
</div> </div>
{% endif %} {% endif %}
{% endcache %}