From 97ca2964cb75f0a7d0ad6d8dcc8d11bfd8847d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DNyymix=E2=80=9D?= Date: Tue, 15 Apr 2025 09:28:43 +0300 Subject: [PATCH] Add cache --- gallery/templates/gallery/about.html | 2 +- gallery/templatetags/gallery_stats.py | 18 ++++++++ gallery/templatetags/stats.py | 13 ------ gallery/templatetags/top_tags.py | 62 ++++++++++++++++++++++++--- 4 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 gallery/templatetags/gallery_stats.py delete mode 100644 gallery/templatetags/stats.py diff --git a/gallery/templates/gallery/about.html b/gallery/templates/gallery/about.html index 13eaecb..f54048b 100644 --- a/gallery/templates/gallery/about.html +++ b/gallery/templates/gallery/about.html @@ -1,6 +1,6 @@ {% extends "base.html" %} {% load static %} -{% load stats %} +{% load gallery_stats %} {% block title %} About {% endblock %} diff --git a/gallery/templatetags/gallery_stats.py b/gallery/templatetags/gallery_stats.py new file mode 100644 index 0000000..0925eba --- /dev/null +++ b/gallery/templatetags/gallery_stats.py @@ -0,0 +1,18 @@ +from django import template +from django.core.cache import cache + +from gallery.models import Album, Photo + +register = template.Library() + +@register.simple_tag +def gallery_stats(): + cache_key = "gallery_stats" + data = cache.get(cache_key) + if data is None: + total_photos = Photo.objects.count() + total_albums = Album.objects.count() + public_albums = Album.objects.filter(is_public=True).count() + data = (total_albums, public_albums, total_photos) + cache.set(cache_key, data, timeout=60 * 10) # 10 min + return data \ No newline at end of file diff --git a/gallery/templatetags/stats.py b/gallery/templatetags/stats.py deleted file mode 100644 index c80e57c..0000000 --- a/gallery/templatetags/stats.py +++ /dev/null @@ -1,13 +0,0 @@ -from django import template - -from gallery.models import Album, Photo - -register = template.Library() - - -@register.simple_tag -def gallery_stats(): - total_photos = Photo.objects.count() - total_albums = Album.objects.count() - public_albums = Album.objects.filter(is_public=True).count() - return total_albums, public_albums, total_photos \ No newline at end of file diff --git a/gallery/templatetags/top_tags.py b/gallery/templatetags/top_tags.py index 9b8fc64..626a77c 100644 --- a/gallery/templatetags/top_tags.py +++ b/gallery/templatetags/top_tags.py @@ -1,4 +1,5 @@ from django import template +from django.core.cache import cache from django.db.models import F from gallery.models import Album, Photo @@ -8,37 +9,84 @@ register = template.Library() @register.simple_tag def last_albums(count=5): - albums = Album.objects.filter(is_public=True).order_by("-album_date")[:count] + cache_key = f"last_albums_{count}" + albums = cache.get(cache_key) + if albums is None: + albums = Album.objects.filter(is_public=True).order_by("-album_date")[:count] + cache.set(cache_key, albums, timeout=60 * 60) # 1h return albums @register.simple_tag def top_photos(count=5): - photos = Photo.objects.filter(album__is_public=True).order_by('-is_favorite', '-likes', '-views').select_related('album')[:count] + cache_key = f"top_photos_{count}" + photos = cache.get(cache_key) + if photos is None: + photos = ( + Photo.objects + .filter(album__is_public=True) + .order_by('-is_favorite', '-likes', '-views') + .select_related('album')[:count] + ) + cache.set(cache_key, albums, timeout=60 * 60) # 1h return photos @register.simple_tag def random_photos_landscape(count=5): - photos = Photo.objects.filter(album__is_public=True, width__gt=F('height')).order_by('?')[:count] + cache_key = f"random_photos_landscape_{count}" + photos = cache.get(cache_key) + if photos is None: + photos = ( + Photo.objects + .filter(album__is_public=True, width__gt=F('height')) + .order_by('?')[:count] + ) + cache.set(cache_key, photos, timeout=30) # 30 sec return photos @register.simple_tag def top_photos_landscape(count=5): - photos = Photo.objects.filter(album__is_public=True, width__gt=F('height')).order_by('-views').select_related('album')[:count] + cache_key = f"top_photos_landscape_{count}" + photos = cache.get(cache_key) + if photos is None: + photos = ( + Photo.objects + .filter(album__is_public=True, width__gt=F('height')) + .order_by('-views') + .select_related('album')[:count] + ) + cache.set(cache_key, photos, timeout=60 * 60) # 1h return photos @register.simple_tag def top_photos_portrait(count=5): - photos = Photo.objects.filter(album__is_public=True, height__gt=F('width')).order_by('-views').select_related('album')[:count] + cache_key = f"top_photos_portrait_{count}" + photos = cache.get(cache_key) + if photos is None: + photos = ( + Photo.objects + .filter(album__is_public=True, height__gt=F('width')) + .order_by('-views') + .select_related('album')[:count] + ) + cache.set(cache_key, photos, timeout=60 * 60) # 1h return photos @register.simple_tag def top_photos_in_album(album, count=5): - photos = Photo.objects.filter(album=album).order_by('-is_favorite', '-likes', '-views').select_related('album')[:count] + cache_key = f"top_photos_album_{album.id}_{count}" + photos = cache.get(cache_key) + if photos is None: + photos = ( + Photo.objects + .filter(album=album) + .order_by('-is_favorite', '-likes', '-views') + .select_related('album')[:count] + ) + cache.set(cache_key, photos, timeout=60 * 60) # 1h return photos -