Add cache

This commit is contained in:
Nyymix 2025-04-15 09:28:43 +03:00
parent 25971efbfb
commit 97ca2964cb
4 changed files with 74 additions and 21 deletions

View file

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load static %}
{% load stats %}
{% load gallery_stats %}
<!-- Title -->
{% block title %} About {% endblock %}

View file

@ -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

View file

@ -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

View file

@ -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