muistox/gallery/management/commands/clear_album_cache.py

21 lines
No EOL
611 B
Python

from django.core.cache import cache
from django.core.management.base import BaseCommand
from gallery.models import Album
class Command(BaseCommand):
help = "Clears photo count and view count caches for all albums"
def handle(self, *args, **options):
albums = Album.objects.only("pk", "name")
cleared = 0
for album in albums:
cache.delete(f'album_{album.pk}_photo_count')
cache.delete(f'album_{album.pk}_photo_views')
cleared += 1
self.stdout.write(self.style.SUCCESS(
f"Cleared cache for {cleared} albums."
))