Refactor caching logic

This commit is contained in:
Nyymix 2025-05-16 21:27:44 +03:00
parent 4c6109a1d6
commit 01dec1e140
7 changed files with 165 additions and 122 deletions

View file

@ -8,11 +8,17 @@ from django.urls import reverse
from django.views import View
from django.views.generic import DetailView, ListView, TemplateView
from config.cache_durations import *
from gallery.cache import cached_or_set
from ..models import Album, Photo, Redir
class AlbumsList(ListView):
"""Displays a paginated list of public albums."""
"""
Displays a paginated list of public albums.
Queryset is cached per page for ALBUM_LIST_PAGE_DURATION.
"""
model = Album
template_name = 'gallery/album_list.html'
paginate_by = 30
@ -20,10 +26,11 @@ class AlbumsList(ListView):
def get_queryset(self):
page = self.request.GET.get('page', 1)
key = f'album_list_queryset_page_{page}'
queryset = cache.get(key)
if not queryset:
queryset = (
return cached_or_set(
key,
ALBUM_LIST_PAGE_DURATION,
lambda: (
Album.objects.filter(is_public=True)
.select_related('cover')
.annotate(
@ -32,14 +39,11 @@ class AlbumsList(ListView):
)
.order_by('-album_date')
)
cache.set(key, queryset, 60 * 5)
return queryset
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Canonical_url
page = self.request.GET.get('page')
if page and page != '1':
canonical_url = f"{self.request.build_absolute_uri(reverse('gallery:albums_url'))}?page={page}"
@ -58,7 +62,8 @@ class AlbumDetail(DetailView):
template_name = 'gallery/album_detail.html'
def get_object(self, queryset=None):
return get_object_or_404(Album, slug=self.kwargs.get('album_slug'))
# return get_object_or_404(Album, slug=self.kwargs.get('album_slug'))
return get_object_or_404(Album, slug__iexact=self.kwargs.get('album_slug'))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)