From ec6acaf8ea46c5ec2d8ae2fc702f48894e141cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9DNyymix=E2=80=9D?= Date: Mon, 14 Apr 2025 18:52:27 +0300 Subject: [PATCH] Add management commands --- .../management/commands/clear_album_cache.py | 21 ++++++++++++ gallery/management/commands/list_sessions.py | 33 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 gallery/management/commands/clear_album_cache.py create mode 100644 gallery/management/commands/list_sessions.py diff --git a/gallery/management/commands/clear_album_cache.py b/gallery/management/commands/clear_album_cache.py new file mode 100644 index 0000000..a4ccd1f --- /dev/null +++ b/gallery/management/commands/clear_album_cache.py @@ -0,0 +1,21 @@ +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." + )) \ No newline at end of file diff --git a/gallery/management/commands/list_sessions.py b/gallery/management/commands/list_sessions.py new file mode 100644 index 0000000..9a5237d --- /dev/null +++ b/gallery/management/commands/list_sessions.py @@ -0,0 +1,33 @@ +import json + +from django.conf import settings +from django.core.management.base import BaseCommand +from django_redis import get_redis_connection + + +class Command(BaseCommand): + help = "List active Django sessions from Redis" + + def add_arguments(self, parser): + parser.add_argument('--details', action='store_true', help='Print session data') + + def handle(self, *args, **options): + cache_prefix = getattr(settings, 'SESSION_CACHE_ALIAS', 'default') + redis_conn = get_redis_connection(cache_prefix) + + # Redis-avaimet istuntoja varten (django-redis käyttää 'session:' prefixiä) + keys = redis_conn.keys('session:*') + count = len(keys) + + self.stdout.write(f"Aktiivisia sessioita Redisissä: {count}") + + if options['details'] and count > 0: + self.stdout.write("\nSession ID:t ja sisällöt:\n" + "-"*40) + for key in keys: + session_data = redis_conn.get(key) + try: + decoded = session_data.decode("utf-8") + except Exception: + decoded = str(session_data) + + self.stdout.write(f"{key.decode('utf-8')}: {decoded}\n")