Add management commands
This commit is contained in:
parent
f4a0edd091
commit
ec6acaf8ea
2 changed files with 54 additions and 0 deletions
21
gallery/management/commands/clear_album_cache.py
Normal file
21
gallery/management/commands/clear_album_cache.py
Normal file
|
@ -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."
|
||||
))
|
33
gallery/management/commands/list_sessions.py
Normal file
33
gallery/management/commands/list_sessions.py
Normal file
|
@ -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")
|
Loading…
Add table
Reference in a new issue